2011-07-27 14 views
0

データベース内のすべての製品のフォームを出力する各ループ用のPHPがあります。私は助けを必要と何PHPループで使用するためのjqueryグローバル変数

jqueryの

<script type="text/javascript"> 
     // When the document is ready 

     $(function() { 


      $('#foo').change(function() { 

       var form = $(this).parents('form'); 

       // Size is whatever the value the user has selected 
       var size = $(this).val(); 

       var price, 
        itemId; 

       // Determine the correct price and item ID based on the selected size 
       switch (size) { 
       <?php 
        $var = 1; 
        foreach($subitem['sizes'] as $size) { 
         echo "\n"; 
         echo "\t\t\t\t\t\tcase '".$size."':\n"; 
         echo "\t\t\t\t\t\t\tprice = '".$subitem['price']."';\n"; 
         echo "\t\t\t\t\t\t\titemId = '".$subitem['prodid']."-".$var."';\n"; 
         echo "\t\t\t\t\t\t\tbreak;\n\n"; 

        $var++; 
        } 
        echo "\t\t\t\t\t}"; 
       ?> 

       form.find('.price').text(price); 

       form.find('[name=my-item-price]').val(price); 

       // Update the item ID 
       form.find('[name=my-item-id]').val(itemId); 


      }); 
     }); 
    </script> 

のこのビットは、これは、フォームと同じ時間出力され、基本的にフォームの一部です。これは、ユーザーが選択リストからオプションを選択したときに、価格を含む非表示のフォーム要素を変更した場合に発生します。

私がする必要があるのは、価格をハードコーディングするのではなく、価格をデータベースから取得することです。

selectリストにデータを入れるために4つのレコードがループするので、switch文で価格をエコーするのは不可能です。私がやって考えたことは、データベースから価格が移入され、いくつかのグローバル変数を持っていたし、そしてちょうどswitch文で

をそのグローバル変数を使用して、私は私がここに

//Create field for item name based upon record count  
if(count($subitem['sizes']) > 1) 
{ 
グローバル変数を初期化すると考えていました

PHPとjQueryを使って私のコード

foreach($items AS $subitem) 
{ 
    list($prodid, $item ,$size, $description, $price) = $subitem; 

    //Create field for item name based upon record count  
    if(count($subitem['sizes']) > 1) 
    { 
     $item_name_field = "<ul><li><select name=\"my-item-name\" id=\"foo\">\n"; 
     foreach($subitem['sizes'] as $size) 
     { 
      $item_name_field .= "<option value=\"{$size}\">{$size}</option>\n"; 
     } 
     $item_name_field .= "</select></li></ul>\n"; 
    } 
    else 
    { 
     $item_name_field = "<input type=\"hidden\" name=\"my-item-name\" value=\"{$subitem['item']}\" />"; 
    } 

    //Creat the form 
    if ($count % NUMCOLS == 0) { echo "<tr>"; } //new row 
    echo "<td>\n"; 
    echo "<form method=\"post\" action=\"\" class=\"jcart\">\n"; 
    echo " <fieldset>\n"; 
    echo "  <input type=\"hidden\" name=\"jcartToken\" value=\"{$_SESSION['jcartToken']}\" />\n"; 
    echo "  <input type=\"hidden\" name=\"my-item-id\" value=\"{$subitem['prodid']}\" />\n"; 
    echo "  <input type=\"hidden\" name=\"my-item-price\" value=\"{$subitem['price']}\" />\n"; 
    echo "  <input type=\"hidden\" name=\"my-item-url\" value=\"http://yahoo.com\" />\n"; 
    echo "  {$item_name_field}\n"; 
    echo "  <ul>\n"; 
    echo "   <li>Price: $<span class=\"price\">{$subitem['price']}</span></li>\n"; 
    echo "   <li><label>Qty: <input type=\"text\" name=\"my-item-qty\" value=\"1\" size=\"3\" /></label></li>\n"; 
    echo "  </ul>\n"; 
    echo "  <input type=\"submit\" name=\"my-add-button\" value=\"add to cart\" class=\"button\" />\n"; 
    echo " </fieldset>\n"; 
    echo "</form>\n"; 
    echo "</td>\n"; 
    ?> 

    <script type="text/javascript"> 
     // When the document is ready 
     $(function() { 


      $('#foo').change(function() { 

       var form = $(this).parents('form'); 

       // Size is whatever the value the user has selected 
       var size = $(this).val(); 

       var price, 
        itemId; 

       // Determine the correct price and item ID based on the selected size 
       switch (size) { 
       case 'Small': 
        price = '10.00'; 
        itemId = '1-a'; 
        break; 
       case 'Medium': 
        price = '20.00'; 
        itemId = '1-b'; 
        break; 
       case 'Large': 
        price = '30.00'; 
        itemId = '1-c'; 
        break; 
       case 'X-Large': 
        price = '30.00'; 
        itemId = '1-c'; 
        break; 
       } 

       form.find('.price').text(price); 

       form.find('[name=my-item-price]').val(price); 

       // Update the item ID 
       form.find('[name=my-item-id]').val(itemId); 


      }); 
     }); 
    </script> 
    <?php 
    $count++; 
    if ($count % NUMCOLS == 0) { echo "</tr>\n"; } # end row 
    //$counter++; //Doesn't appear this is used 
} 

編集:私はそれは一種の、作業を取得するために管理してきましたが、価格はアーカンソー

すべて同じ。

私がする必要があるのは、各サイズの価格をサイズに割り当てることです。

<script type="text/javascript"> 
     // When the document is ready 

     $(function() { 


      $('#foo').change(function() { 

       var form = $(this).parents('form'); 

       // Size is whatever the value the user has selected 
       var size = $(this).val(); 

       var price, 
        itemId; 

       // Determine the correct price and item ID based on the selected size 
       switch (size) { 
       <?php 
        $var = 1; 
        foreach($subitem['sizes'] as $size) { 
         echo "\n"; 
         echo "\t\t\t\t\t\tcase '".$size."':\n"; 
         echo "\t\t\t\t\t\t\tprice = '".$subitem['price']."';\n"; 
         echo "\t\t\t\t\t\t\titemId = '".$subitem['prodid']."-".$var."';\n"; 
         echo "\t\t\t\t\t\t\tbreak;\n\n"; 

        $var++; 
        } 
        echo "\t\t\t\t\t}"; 
       ?> 

       form.find('.price').text(price); 

       form.find('[name=my-item-price]').val(price); 

       // Update the item ID 
       form.find('[name=my-item-id]').val(itemId); 


      }); 
     }); 
    </script> 

答えて

2

あなたはマークアップと一緒にJSONオブジェクト形式のPHPコードをレンダリングし、私はロジックを見ることができますが、それは私がそれ欲しいもののために動作しませんでした

var item_prices_by_size = { "Small": { "Price": "10.00", "ItemId": "1-a" }, 
           "Medium": { "Price": "30.00", "ItemId": "1-b" } 
           }; 

      $(function() { 


       $('#foo').change(function() { 

        var form = $(this).parents('form'); 

        // Size is whatever the value the user has selected 
        var size = $(this).val(); 

// Determine the correct price and item ID based on the selected size 
        var price = item_prices_by_size[size].Price, 
         itemId = item_prices_by_size[size].ItemId; 

        form.find('.price').text(price); 

        form.find('[name=my-item-price]').val(price); 

        // Update the item ID 
        form.find('[name=my-item-id]').val(itemId); 


       }); 
      }); 
+0

以下のようにJSでそれを使用することができますする。私は別の方法を使用してそれを働かせましたが、私は変更する価格を得ることができません – AdRock

+0

私は私が働いているものを追加しましたが、私は彼も価格を更新する必要があります – AdRock

+0

これはあなたが言ったように動作します。私がする必要があるのは、データベースから価格を設定することです – AdRock

関連する問題