douyue8191 2017-11-05 11:23
浏览 37

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

Firstly, the headings are stored in h1 tags. This is taken from a separate table named "menu_type". Which is linked through a "menu" table.

I am trying to display data on the base like this:

HEADING

Table Data

2nd Heading

Second Data

--- In a loop until it is all completed ---

Here is a like page of what it is doing

I believe I have the methods correct and can see what it is doing, it is printing the first heading, then a blank table, then the second heading and then the data from the first table.

See my code:

<?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条回答 默认 最新

  • doudong0425 2017-11-05 11:43
    关注

    You don't need these anymore, it's like you're repeating the query. It looks incorrect also.

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

    The menu items are already in this query, you just have to loop through it;

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

    UPDATE 1: this code is incorrect, it doesn't insert the value to the array. I updated the code (after "try this").

    $result_array[] = $menuType;
    

    UPDATE 2: the $result is repeatedly used in mysqli functions, the index is being moved. What I did is copied the initial $result to $resultCopy. Try code again, haha

    Use array_push($array,$value_you_insert) function, for inserting elements to an array.

    Try this;

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致