2017-11-05 4 views
0

最初に、見出しはh1タグに格納されます。これは、「menu_type」という別のテーブルから取得されます。これは "メニュー"テーブルを介してリンクされています。見出しがある表の動的表示

私はこのような基材上にデータを表示しようとしています:

表データ

秒データ

を見出し---ループになるまで第二の見出しすべて完了です---

Here is a like page of what it is doing

私は正しい方法があり、それが何をしているのかを見ることができます、それは最初の見出し、次に空白の表を印刷し、次に2番目の見出しと最初のテーブルのデータです。

は私のコードを参照してください:

<?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

あなたは、クエリを繰り返しているようだ、それは、もはやこれらを必要としません。それはまた不正確に見える。

$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:このコードは間違っている、それは、配列に値を挿入しません。私はコードを更新しました( "try this")。

$result_array[] = $menuType; 

2 UPDATE: $結果が繰り返しmysqliの機能に使用され、インデックスは移動されています。私がやったことは、最初の$結果を$ 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> 
    "; 
} 

?> 
+0

これはおかげで、まだ間違って印刷されます。このリンクを見てください - http://adonnelly759.students.cs.qub.ac.uk/thatch/sample.php - メインコースにスターターアイテムを表示していますか?何か案は? –

+1

@AidanDonnellyこんにちは、私は答えを更新しました。サイトのコードを更新した場合はお知らせください。 –

+0

コードを更新しましたが、まだ表示されていません。私は2日間それに取り組んでいて、まだ目に見えて終わることができません –

関連する問題