简体   繁体   中英

Dynamically display for table with headings accordingly

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

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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