【问题标题】:How Do i Loop Js with mysql while loop我如何用mysql while循环循环Js
【发布时间】:2014-10-28 21:07:18
【问题描述】:

嘿,伙计们,我需要问的是如何使用 while 循环来循环 javascript 因为我的代码是这样的

    $i=0;
    while($row = mysqli_fetch_array($result)) {
    echo '<div class="grid_1_of_4 images_1_of_4" >
                <form method="post" action="......">
                <a class = "popup-link" href = "'.$row['shade_enlarged_img'].'" alt = "" title = "'.$row['shade_desc'].'">
                 <img src="'.$row['shade_img'].'" alt="" title = "click to enlarge"/> </a>
                 <h2>'.$row['shade_desc'].'</h2>
                 <p style="font-size:0.95em"><b>Code: '.$row['shade_code'].'</b></p>
                 <p>Category: '.$row['categories'].'</p>';
                 $code=$row['shade_code'];
                 $result_quantity = mysqli_query($con,"SELECT ...........);
                 $num_of_rows=mysqli_num_rows($result_quantity);
                 $count=0;?>
                                     <script>
                function showUser(str) {
                  if (str=="") {
                    document.getElementById("txtHint<?php echo $i; ?>").innerHTML="";
                    return;
                  } 
                  if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                  } else { // code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  var url = "getprice.php?brand=<?php echo $row['brand_name']; ?>&category=<?php echo $row['categories'];?>&q="+str;
                  xmlhttp.onreadystatechange=function() {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                      document.getElementById("txtHint<?php echo $i; ?>").innerHTML=xmlhttp.responseText;
                    }
                  }
                  xmlhttp.open("GET",url,true);
                  xmlhttp.send();
                }
                </script>       <?php
                 echo '<p style="font-size:0.71em">Available Packaging: <select name="product_qty" onchange="showUser(this.value)" style="margin-left:4px; font-size:1.02em;">
                 <option value=""></option>';
                 while($row_quantity = mysqli_fetch_array($result_quantity)) {
                 echo '<option value="'.$row_quantity['quantity'].'">'.$row_quantity['quantity'].' '.$row_quantity['quantity_unit'].'</option>';
                }
                 echo '</select></p>'; 


                 echo '<div id="txtHint'.$i.'"><b>Person info will be listed here.</b></div>'; 
                 echo '</p>';
                 echo '<p style="font-size:0.71em">Quantity: <input type="number" style="display:inline; width:50px;" name="number_of_units" min="1"></p>';
                 echo '<button class="button"><span class="cart-button"><img src="images/cart.jpg" alt="" />Add to Cart</span> </button>
                 <input type="hidden" name="product_code" value="'.$row['shade_code'].'" />

                 </form>
            </div>'; $i++;

代码运行良好,但由于我通过 mysqldatabase 的 while 循环调用所有项目,所以当我运行此代码时,这只适用于第一个项目,其余项目不起作用.. 观看此图像以快速查看http://imgur.com/2qM0FzX

【问题讨论】:

  • 用while循环回显它
  • 实际上商品有 3 种类型,这 3 种类型有 3 个价格,所以它只是变得奇怪
  • 在循环外呼应$row_quantity['price'] 在最后一行很奇怪。你确定它应该是$row_quantity['price']吗?这将是循环完成时的最后一个值。
  • 对不起,我刚刚编辑了现在看到的代码!

标签: javascript php mysql loops while-loop


【解决方案1】:

我认为#products 会有问题,因为你不能一遍又一遍地拥有相同的id="products",否则jQuery 不知道要切换哪个。实际上,与#priceInput 相同。尝试添加自动递增的$i,如下所示。

    <?php
    $i = 0;
while($row = mysqli_fetch_array($result)) { ?>
    <div class="grid_1_of_4 images_1_of_4" >
        <form method="post" action="page.php">
            <a class = "popup-link" href = "<?php echo $row['shade_enlarged_img']; ?>" alt = "" title = "<?php echo $row['shade_desc']; ?>">
             <img src="<?php echo $row['shade_img']; ?>" alt="" title = "click to enlarge"/> </a>
             <h2><?php echo $row['shade_desc']; ?></h2>
             <p style="font-size:0.95em"><b>Code: <?php echo $row['shade_code']; ?></b></p>
             <p>Category: <?php echo $row['categories']; ?></p>

             <script>
            $(function () {
                $('#products<?php echo $i; ?>').change(function () {
                    $('#priceInput<?php echo $i; ?>').val($('#products<?php echo $i; ?> option:selected').attr('data-price'));
                });
            });
            </script>
             <p style="font-size:0.71em">Available Packaging: <select id="products<?php echo $i; ?>" name="product_qty" style="margin-left:4px; font-size:1.02em;" onChange="ShowInfo('<?php echo $i; ?>','<?php echo $row['brand_name']; ?>','<?php echo $row['categories']; ?>')">
             <?php
             $code                  =   $row['shade_code'];
             $result_quantity       =   mysqli_query($con,"SELECT ............");
             $num_of_rows           =   mysqli_num_rows($result_quantity);
             $count                 =   0;
             while($row_quantity    =   mysqli_fetch_array($result_quantity)) { ?>
                    <option value="<?php echo $row_quantity['quantity']; ?>" data-price="<?php echo $row_quantity['price']; ?>"><?php echo $row_quantity['quantity'].' '.$row_quantity['quantity_unit']; ?></option><?php 
            } ?>

            </select></p>
            <div id="txtHint<?php echo $i; ?>"><b>Person info will be listed here.</b></div>
            Price :<input type="text" name="price" value="" id="priceInput<?php echo $i; ?>" disabled="disabled"/>
                <p style="font-size:0.71em">Quantity: <input type="number" style="display:inline; width:50px;" name="number_of_units" min="1"></p>
                <button class="button"><span class="cart-button"><img src="images/cart.jpg" alt="" />Add to Cart</span> </button>
            </form>
        </div><?php
        $i++;
} ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script>
 function ShowInfo(IdNum,RowBrand,RowCat) {

        // Get the value of the selected dropdown
        var SelVal  =   $('#products'+IdNum).val();

        $.ajax({
            type : 'GET',
            url:"getprice.php?brand="+RowBrand+"&category="+RowCat+"&q="+SelVal;
            success: function(result){
                    $('#txtHint'+IdNum).html(result);
                }
        });
    }
</script>

【讨论】:

  • 这就像一个魅力!谢谢。
  • 没问题。我花了一分钟才弄清楚你的问题是什么......干杯。
  • 如果我在 Ajax 中尝试这个,先生还有 1 个查询,我应该怎么做才能再次看到我的代码我用 Ajax 尝试了这个,但它不起作用。
  • 不要在底部循环 Ajax 的东西。这是循环完成后的一次性写入。重复在每个&lt;select&gt; 上的onChange="ShowInfo('1','Brand','Category')" 中。我没有测试过,但应该很接近!
【解决方案2】:

试试.=,它将继续添加您的 mysqli_fetch_array 查询的最新结果。

然后将您分配给 $just_a_variable 的总和值回显到页面或返回到 ajax 等。

<?php
    $just_a_variable = '';    
    $just_a_variable .= '<p style="font-size:0.71em">Available Packaging: <select id="products" name="product_qty" style="margin-left:4px; font-size:1.02em;">';
    while($row_quantity = mysqli_fetch_array($result_quantity)) {
        $just_a_variable .= '<option value="'.$row_quantity['quantity'].'" data-price="'.$row_quantity['price'].'">'.$row_quantity['quantity'].' '.$row_quantity['quantity_unit'].'</option>';
    }
    $just_a_variable .= '</select></p>';
    $just_a_variable .= ' Price :<input type="text" name="price" value="'.$row_quantity['price'].'" id="priceInput" disabled="disabled"/>';

    echo $just_a_variable;
?>

【讨论】:

  • 没有还是一样的结果!
  • imgur.com/2qM0FzX 看到这张图片它给出了与第一个作品相同的结果,但第二个第三个等等没有!
【解决方案3】:

这是因为 mysql_fetch_array 函数一次返回一行。如果你想要所有的行,你应该遍历结果

 $row = mysqli_fetch_array($result_quantity);
   $total =  mysql_num_rows($variable_resultfrom_sql_query);
   while($row = mysql_fetch_array($variable_resultfrom_sql_query))

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 2021-07-27
    • 1970-01-01
    • 2011-11-30
    • 2017-09-03
    • 2021-12-28
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    相关资源
    最近更新 更多