簡體   English   中英

PHP SQL查詢來填充嵌套菜單?

[英]PHP SQL query to populate nested menu?

我正在建立一個網站,該網站使用一年中幾個月的選項卡菜單,然后在每個月/選項卡中使用手風琴菜單,以列出該月發生的所有事件。

我在填充網站時不熟悉使用PHP,但是在使代碼正常工作時遇到了一些麻煩。 PHP之前的代碼如下所示:

<div class="tabscontainer">
<div class="tabs">
    <div class="tab selected first" id="tab_menu_1">
        <div class="link">$Month</div>
        <div class="arrow"></div>
    </div>
</div>

<div class="curvedContainer">
<div class="tabcontent" id="tab_content_1" style="display:block">
    <div id="page">     
    <ul id="example4" class="accordion">
        <li>
        <h3>$EventListGoesHere</h3>
            <div class="panel loading">
                <p>Some content here.</p>
            </div>
        </li>
    </ul>
    </div>
</div>

我嘗試使用while循環,但是我一直在破壞代碼。 我嘗試使用的while循環是:

<?php 
// Connects to your Database 
mysql_connect("iphere", "username", "password") or die(mysql_error()); 
mysql_select_db("dbname") or die(mysql_error()); 
$data = mysql_query("SELECT * FROM MonthTable") 
or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) 
{ 
Print "My code here"; 
Nested SQL code to got through events table here.   
} 
?> 

我想我的問題是,我是否走了正確的道路,如果可以,有人可以給我一個想法,讓標准代碼在PHP while循環中如何適應嗎?

我的數據庫是這樣的:

月表
ID | 短| 月長
1 | 2012年10月| 2012年10月
2 | 2012年9月| 2012年9月

事件表
ID | MonthID | 事件
1 | 1 | 婚禮
2 | 1 | 戒律禮拜堂
3 | 1 | 葬禮
4 | 2 | 生日
5 | 2 | 生日
6 | 2 | 婚禮

非常感謝

您只能執行一個查詢。 例如:

<?php

$sql = "
  SELECT
    a.id, b.id AS monthId, a.event, b.monthshort, b.monthlong
  FROM
    events_table_name AS a
  INNER JOIN
    month_table_name AS b ON b.id = a.monthId
  ORDER BY
    b.id, a.id ASC
  ";

// rest of db stuff here

$events = array();
$months = array();
while ($row = mysql_fetch_array($result)) {
  if (! isset($events[$row["monthId"]])) {
    $events[$row["monthId"]] = array();
  }
  $months[$row["monthId"]] = $row["monthlong"];
  $events[$row["monthId"]][] = $row["event"];
}

for ($x = 1; $x <= 12; $x++) {
  echo '<div class="tab" id="tab_$x">';
  if (isset($events[$x])) {
    echo '<div class="month-title">' . $months[$x] . '</div>';
    echo '<ul>';
    foreach ($events[$x] as $event) {
      echo "<li>". $event ."</li>";
    }
    echo '</ul>';
  }
  echo '</div>';
}

?>

您遇到什么錯誤? 那while循環很好。 調查使用mysqli。 標准的mysqli循環為:

$db=new mysqli('host','user','pass');
$q=$db->query("SELECT * FROM db.table");
while ($f=$q->fetch_assoc()) {
     print_r($f);
     // echo "<br>"; 
}

嘗試一下,它將顯示出您為循環的每一行獲取的數組。 同樣,您遇到什么錯誤?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM