显示同一div

时间:2016-11-28 03:24:02

标签: php html mysql

此代码从表中获取所有值,并且对于每一行,它在警报div中显示其详细信息,我可以单击" order ready按钮"对于那个单品。

我需要做的是将所有来自同一订单的产品放在一个div中,为此,我考虑使用具有相同日期值的所有行,并且当此值更改时创建一个新的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>';      
            }
         ?>

2 个答案:

答案 0 :(得分: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++;
        }
    }
 ?>  

答案 1 :(得分: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.