2016-11-28 52 views
0

该代码获取表格中的所有值,并为每行显示警报格内的详细信息,我可以单击该单一产品的“订购准备就绪按钮”。从同一格内的表中显示所有相同的值

我需要做的是把所有产品都放在一个div中,这些产品都是来自同一个订单,为此我正在考虑使用所有具有相同日期值的行,并且当这个值更改时创建一个新的分区。

  <?php 
      $result = mysqli_query($mysqli, "SELECT * FROM kitchen"); 

      while ($row = mysqli_fetch_array($result)) { 

       $table = $row['table']; 
       $customer = $row['customer']; 
       $product = $row['product_name']; 
       $code = $row['product_code']; 
       $size = $row['size']; 
       $id = $row['id']; 
       $date = $row['date']; 

// It would have to open here in each first distinct $date 
       echo '<div class="alert alert-info" role="alert" id="'.$code.'">'; 
       echo '<h4>'.'Table '.$table.'</h4>'; 
       echo '<h4>'.'Name: '.$name.'</h4>'; 


// Repeat this for each equal $date value 
       if($code=="A01"||$code=="A02"||$code=="A03"||$code=="A04"){ 
       echo '<h4>'.$code.' - '.$product.' ('.$size.')'.'</h4>'; 
       } 
       else{ 
        echo '<h4>'.$code.' - '.$product.'</h4>'; 
       } 

// Close here before each next distinct $date 
       echo '<form action="actionkitchen.php" method="post">'; 
       echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data' type='submit'>Order Ready</button>"; 
       echo '</form>'; 
       echo '</div>';  
      } 
     ?> 
+0

有实现这一目标的多种方式,但最简单的将是BY子句添加命令,你的查询语句。这样,同一订单的所有产品将一起列出 – jeff

回答

0

这就是我最终得到的,而不是最优雅的解决方案,但它的工作。

<?php 
    $result = mysqli_query($mysqli, "SELECT * FROM kitchen"); 

    while ($row = mysqli_fetch_array($result)) { 

     $table[] = $row['table']; 
     $name[] = $row['name']; 
     $product[] = $row['product_name']; 
     $code[] = $row['product_code']; 
     $size[] = $row['size']; 
     $date[] = $row['date'];  
    } 

    $count = array_count_values($date); 
    $y = 0; 

    foreach ($count as $item){   
     for($i=0;$i<$item;$i++){ 
      if($i==0){ 
       echo '<div class="alert alert-info">'; 
       echo '<h4>'.'Table '.$table[$y].'</h4>'; 
       echo '<h4>'.'Name: '.$name[$y].'</h4>'; 
      } 
      if($code[$y]=="A01"||$code[$y]=="A02"||$code[$y]=="A03"||$code[$y]=="A04"){ 
      echo '<h4>'.$code[$y].' - '.$product[$y].' ('.$size[$y].')'.'</h4>'; 
      } 
      else{ 
       echo '<h4>'.$code[$y].' - '.$product[$y].'</h4>'; 
      } 
      if($i==$item-1){ 
       echo '<form action="actionkitchen.php" method="post">'; 
       echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data[$y]' type='submit'>Order Ready</button>"; 
       echo '</form>'; 
       echo '</div>'; 
      } 
      $y++; 
     } 
    } 
?> 
0

要设置您的产品在相同的顺序,我会按照他们的数组中的键。出于我们的目的,我们将使用multidimensional array,以便我们可以将我们的产品添加到唯一密钥中(使用示例中的“日期”)。下面你会看到我设置数组,从数据库中获取行(通过我们的组密钥进行排序,以便我们在前端具有一致性),并开始将它们放入其独特的组中。将产品推入日期数组时,我使用array_merge()结合in_array()ternary operator在HTML中设置“产品字符串”。

<?php 
    /* Fetch/Set Kitchen */ 
    $kitchen = array(); 
    $sql  = "SELECT * FROM `kitchen` ORDER BY `date`"; 
    $query  = mysqli_query($mysqli, $sql); 
    while($row = mysqli_fetch_array($query)) { 
     $kitchen[$row['date']][] = array_merge($row, array(
      'product_string' => (in_array($row['product_code'], array('A01', 'A02', 'A03', 'A04')) !== FALSE) 
       ? $row['product_code'] . ' - ' . $row['product_name'] . ' (' . $row['size'] . ')' 
       : $row['product_code'] . ' - ' . $row['product_name'] 
     )); 
    } 
?> 

为了保持我们的HTML整洁,从我们的PHP除了可读,你会看到,我选择要使用的控制结构的alternative syntax。这有助于通过使用制表符缩进来放置代码中的任何非常笨拙的大括号。

<?php foreach($kitchen as $date => $items): ?> 
    <div class="alert alert-info" role="alert" id="<?php echo $date; ?>"> 
     <?php foreach($items as $item): ?> 
      <h4>Table <?php echo $item['table']; ?></h4> 
      <h4>Name: <?php echo $item['customer']; ?></h4> 
      <h4><?php echo $item['product_string']; ?></h4> 
      <form action="actionkitchen.php" method="POST"> 
       <button class="btn btn-lg btn-primary btn-block" name="data" value="<?php echo $item['data']; ?>" type="submit">Order Ready</button> 
      </form> 
     <?php endforeach; ?> 
    </div> 
<?php endforeach; ?> 

上面的参考代码将输出HTML类似:

<div class="alert alert-info" role="alert" id="2016-10-21"> 
    <h4>Table Table 1</h4> 
    <h4>Name: Name 1</h4> 
    <h4>XXX1 - Product 1 (XXX)</h4> 
    <form action="actionkitchen.php" method="POST"> 
     <button class="btn btn-lg btn-primary btn-block" name="data" value="XXX1" type="submit">Order Ready</button> 
    </form> 
    <h4>Table Table 2</h4> 
    <h4>Name: Name 2</h4> 
    <h4>XXX2 - Product 2</h4> 
    <form action="actionkitchen.php" method="POST"> 
     <button class="btn btn-lg btn-primary btn-block" name="data" value="XXX2" type="submit">Order Ready</button> 
    </form> 
    <h4>Table Table 3</h4> 
    <h4>Name: Name 3</h4> 
    <h4>XXX3 - Product 3 (XXX)</h4> 
    <form action="actionkitchen.php" method="POST"> 
     <button class="btn btn-lg btn-primary btn-block" name="data" value="XXX3" type="submit">Order Ready</button> 
    </form> 
</div> 
<div class="alert alert-info" role="alert" id="2016-10-27"> 
    <h4>Table Table 4</h4> 
    <h4>Name: Name 4</h4> 
    <h4>XXX4 - Product 4</h4> 
    <form action="actionkitchen.php" method="POST"> 
     <button class="btn btn-lg btn-primary btn-block" name="data" value="XXX4" type="submit">Order Ready</button> 
    </form> 
    <h4>Table Table 5</h4> 
    <h4>Name: Name 5</h4> 
    <h4>XXX5 - Product 5 (XXX)</h4> 
    <form action="actionkitchen.php" method="POST"> 
     <button class="btn btn-lg btn-primary btn-block" name="data" value="XXX5" type="submit">Order Ready</button> 
    </form> 
</div> 
<div class="alert alert-info" role="alert" id="2016-11-06"> 
    ...etc. 
相关问题