循环捕获数据中的Javascript问题

时间:2018-09-15 20:02:53

标签: javascript php mysql

我尝试从数据库中循环多个值,并且每次我单击菜单上的特定项目时,即使函数在循环内,Javascript也只会捕获数据库中出现的第一个值,即使我单击最后一个项目,第一个项目仍显示...如何解决?这是我的代码。 (结果显示在最上面,我没有包括在内)

<?php
    $res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));
        while($rows = mysqli_fetch_array($res)){
            $f_id = $rows['Menu_id'];
            $fname = $rows['Food_name'];
            $fprice = $rows['Price'];                      
?>

    <div class="col-12 helv menu rounded">
    <ul class="nav navbar-nav navbar-expand-md">
      <li class="nav-item"><img class="rounded" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Generic placeholder image" width="180" height="180"></li>
      <li class="nav-item marginleft">
          <br>
          <input id="foodname" value="<?php echo $fname;?>" style="display: none;"><h5><b><?php echo $fname;?></b></h1></input><br>
          <input id="foodprice" value="<?php echo $fprice;?>" style="display: none;"><h5>Php <?php echo $fprice;?>.00</h1></input>
      </li>
      <li class="nav-item" style="position: absolute; margin-left: 90%">
        <button class="fa fa-plus btn btn-danger" onclick="add()"> Add</button>
      </li>
    </ul>
    <br>
    </div>

<script>
      function add() {
          var fdname = document.getElementById("foodname").value;
          var fdprice = document.getElementById("foodprice").value;
          document.getElementById("display").innerHTML = fdname;
          document.getElementById("display1").innerHTML = fdprice;
      }
  </script> 

<?php } ?> 

3 个答案:

答案 0 :(得分:1)

<?php
  // As Jeto said, you should use prepared statement
  $res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));
    while($rows = mysqli_fetch_array($res)){
      $f_id = $rows['Menu_id'];
      $fname = $rows['Food_name'];
      $fprice = $rows['Price'];                      
?>
  <div class="col-12 helv menu rounded">
  <ul class="nav navbar-nav navbar-expand-md">
    <li class="nav-item"><img class="rounded" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Generic placeholder image" width="180" height="180"></li>
    <li class="nav-item marginleft">
        <br>
        <input class="foodname" value="<?php echo $fname;?>" style="display: none;"><h5><b><?php echo $fname;?></b></h1></input><br>
        <input class="foodprice" value="<?php echo $fprice;?>" style="display: none;"><h5>Php <?php echo $fprice;?>.00</h1></input>
    </li>
    <li class="nav-item" style="position: absolute; margin-left: 90%">
      <button class="fa fa-plus btn btn-danger" onclick="add('<?php echo $fname;?>', <?php echo $fprice;?>)"> Add</button>
    </li>
  </ul>
  <br>
  </div>
<?php } ?>

<script>
  function add(fdname, fdprice) {
      document.getElementById("display").innerHTML = fdname;
      document.getElementById("display1").innerHTML = fdprice;
  }
</script> 

答案 1 :(得分:0)

首先查询while查询之前其中有多少项

$res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));

$count_of_items = mysqli_num_rows($res);

然后运行while查询,然后根据项目数在表单内运行for循环

for($x=0;$x<count($count_of_items); $x++){
<input id="foodname" value="<?php echo $fname[$x];?>" style="display: none;"><h5><b><?php echo $fname[$x];?></b></h1></input><br>
      <input id="foodprice" value="<?php echo $fprice[$x];?>" style="display: none;"><h5>Php <?php echo $fprice[$x];?>.00</h1></input>
}

答案 2 :(得分:0)

还有更多错误。

1)ID是唯一的

请选中此https://stackoverflow.com/a/12889416/3742228

2)重复的JS函数

您进行的每个循环都会创建另一个名为“ add”的JS函数。如果创建3个名为“ add”的函数,则在编写add()时要调用哪个函数? :)


对于您的代码,还有更多解决方案,但这取决于您要执行的操作。看看这个:

<?php
$res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));
    while($rows = mysqli_fetch_array($res)){
        $f_id = $rows['Menu_id'];
        $fname = $rows['Food_name'];
        $fprice = $rows['Price'];                      
?>

<div class="col-12 helv menu rounded">
<ul class="nav navbar-nav navbar-expand-md">
  <li class="nav-item"><img class="rounded" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Generic placeholder image" width="180" height="180"></li>
  <li class="nav-item marginleft">
      something here
  </li>
  <li class="nav-item" style="position: absolute; margin-left: 90%">
    <button class="fa fa-plus btn btn-danger" foodName="<?php echo $fname;?>" foodPrice="<?php echo $fprice;?>" onclick="add(this)"> Add</button>
  </li>
</ul>
<br>
</div>


<?php } ?>
  <script>
  function add(e) {
      var fdname = e.getAttribute("foodName");
      var fdprice = e.getAttribute("foodPrice");
      document.getElementById("display").innerHTML = fdname;
      document.getElementById("display1").innerHTML = fdprice;
  }
  </script> 
相关问题