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

时间:2017-11-05 11:23:34

标签: 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]);
    ?>

1 个答案:

答案 0 :(得分:1)

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

 $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));

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

$result_array[] = $menuType;

更新2:在mysqli函数中重复使用$ result,正在移动索引。我所做的是将初始$ 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>
    ";
}

?>