你似乎在正確的軌道上,但有一些事情混在一起。
菜單
首先讓你想使用unordered list
一個菜單時,然後用CSS樣式。一個basic example of this是:
<ul class="menu">
<li><a href="test">Test</a></li>
<li><a href="test2">Test 2</a></li>
<li><a href="test3">Test 3</a></li>
</ul>
然後你可以在下面的CSS
ul.menu, ul.menu * {
list-style: none;
padding: 0;
margin: 0;
}
ul.menu {
width: 100%;
height: 20px;
background: #ccc;
padding: 5px 0; /* Add padding top and bottom */
}
ul.menu > li {
height: 20px;
line-height: 20px;
float: left;
}
/* Make a tag fill the entire LI so users can click
anywhere, not just on the text. */
ul.menu > li > a {
display: block;
padding: 0 10px; /* Add padding between items */
color: #000;
text-decoration: none;
}
ul.menu > li > a:hover, ul.menu > li > a:active {
background: #000;
color: #FFF;
}
/* Add divider between items, except last item (Does not work with earlier versions of IE) */
ul.menu > li:not(:last-child) {
border-right: 1px solid #000;
}
PHP循環
首先記風格吧。您正在使用mysql
,即depreciated
。這意味着在即將到來的PHP版本中,它將不再可用。很多人推薦你學習PDO。我個人更喜歡MySQLi準備的陳述,但這只是我的偏好。要麼是好的,但要學習其中之一。
現在爲您的循環。你好像在你的循環中檢查你的mysql查詢的結果,這是錯誤的。我猜你上面有一個查詢,它將結果加載到$ subArr中。在將它們加載到$ subArr之前,您需要先撥打mysql_num_rows
。
循環本身並非如此,只要您將其應用於上面的列表。你的最終代碼應該看起來像這樣。請注意,我在我的示例中使用了MySQLi,我建議您也這樣做,儘管如果您願意的話,將其轉換爲MySQL應該不會太難。
<?php
$subArr = array();
$query = "SELECT something FROM somewhere";
$result = $mysql->query($query);
if($result->num_rows) {
while($row = $result->fetch_assoc()) { //I personally prefer fetch_assoc over the others, but fetch_row or fetch_array are both fine here too.
$subArr = $row;
}
}
//Lets output the menu
$resultString .= '<ul class="menu">';
foreach($subArr as $sub => $result) {
$resultString .= '<li><a href="' . $result['url'] . '">' . $result['name'] . '</a></li>'
}
$resultString = '</ul>';
隨着音符的最後一個點,你並不需要把cursor: pointer
上的一個標籤,它在默認情況下該造型。
我希望這能夠爲你清理一些東西。
你應該包含'print_r($ subArr)'的結果'我們不知道你的數組結構。 – Robert