繁体   English   中英

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

[英]Dynamically display for table with headings accordingly

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

我试图在这样的基础上显示数据:

标题

表数据

第二标题

第二数据

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

这是正在做的类似页面

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

看我的代码:

<?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>
    ";
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM