php - 动态显示带有相应标题的表格

标签 php html mysql dynamic

首先,标题存储在 h1 标签中。这取自名为“menu_type”的单独表。这是通过“菜单”表链接的。

我正在尝试在底座上显示数据,如下所示:

标题

表格数据

第二个标题

第二个数据

--- 循环直到全部完成 ---

Here is a like page of what it is doing

我相信我的方法是正确的,并且可以看到它在做什么,它正在打印第一个标题,然后是一个空白表格,然后是第二个标题,然后是第一个表格中的数据。

查看我的代码:

<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns

while($row = mysqli_fetch_assoc($result)){
    $menuType = $row['type'];
    $result_array[] = $menuType; // This array holds each of the menu_types from DB
}

$countArray = count($result_array);

for($i = 0; $i < $numRows; $i++){


    $query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
    $result = mysqli_query($connect, $query) or die(mysqli_error($connect));

    echo "
        <div id='hide'>
            <h1 id='wines' class='head-font text-center head'>$result_array[$i]</h1>
            <table class='table table-hover table-responsive'>
                <thead>
                    <tr>
                        <th>
                            Item
                        </th>

                        <th class='text-right'>
                            Price
                        </th>
                    </tr>
                </thead>
                <tbody>
                <tr> 
    ";  


    $menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
    $menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));

    while ($row = mysqli_fetch_assoc($menuResult)) {
        $name = $row['name'];
        $description = $row['description'];
        $price = $row['price'];

    echo "
            <td>$name - <small>$description</small></td>
            <td class='text-right'>£$price </td>
        ";
    }

    echo 
    " 
        </tr>
        </tbody>
        </table>
    ";

}

// print_r($result_array[2]);
    ?>

最佳答案

您不再需要这些,就像您在重复查询一样。它看起来也不正确。

 $menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
 $menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));

菜单项已在此查询中,您只需循环遍历它即可;

$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));

更新1:此代码不正确,它不会将值插入到数组中。我更新了代码(在“尝试这个”之后)。

$result_array[] = $menuType;

更新2: $result 在 mysqli 函数中重复使用,索引正在移动。我所做的是将初始 $result 复制到 $resultCopy。再试一下代码,哈哈

使用 array_push($array,$value_you_insert) 函数将元素插入数组。

试试这个;

<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$resultCopy = $result;
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns

while($row = mysqli_fetch_assoc($resultCopy)){
    $menuType = $row['type'];
    array_push($result_array,$menuType);
}

for($i = 0; $i < $numRows; $i++){

    echo "
     <div id='hide'>
      <h1 id='wines' class='head-font text-center head'>".$result_array[$i]."</h1>
      <table class='table table-hover table-responsive'>
       <thead>
        <tr>
         <th>Item</th>
         <th class='text-right'>Price</th>
        </tr>
       </thead>
       <tbody>
    ";  

    $query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
    $result = mysqli_query($connect, $query) or die(mysqli_error($connect));

    while ($row = mysqli_fetch_assoc($result)) {
        $name = $row['name'];
        $description = $row['description'];
        $price = $row['price'];

        echo"
        <tr>
         <td>".$name." - <small>".$description."</small></td>
         <td class='text-right'>£".$price."</td>
        </tr>
        ";
    }

    echo 
    " 
        </tbody>
        </table>
    ";
}

?>

关于php - 动态显示带有相应标题的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120989/

相关文章:

html - 隐藏除特定子元素内容之外的所有内容

PHP 选择 2 个日期之间的平均值

php - LARAVEL MYSQL 如何使用自然 JOIN

mysql - 根据特定标准选择每个公司的产品最低价格

php - 把PHP分成3列,一一展示

phpstorm 自动完成和 ctrl+click 无法识别嵌套变量/方法

javascript - 我希望当我单击编辑图像时可以使用编辑功能

html - 不能让我的页脚粘住并为内容提供滚动条

php - 方法重构?

html - 单个 css 或多个用于响应式设计 - css 速度