我有以下代码来显示每个类别的一些品牌。
<?
$con = mysql_connect("localhost","baba","passsss");//database connection
mysql_select_db("gr_brands_igi") or die(mysql_error());
mysql_set_charset('utf8',$con);
$query = "SELECT * FROM brands ORDER BY category_anchor ASC, brand_anchor ASC";
$result = mysql_query($query);
$lasteventname = "";
echo "<table border='0' width='100%' cellspacing='0' cellpadding='0'>";
$cell_ctr = 0;
while($row=mysql_fetch_array($result))
{
if($row[0] != $lasteventname){
$title = $row[0];
$lasteventname = $row[0];
echo "<tr><td colspan=5><h2>$title</h2></td></tr>";
}
if(($cell_ctr % 5) == 4) // 2 is the number of columns
{
echo"</tr>";
echo "<tr>";
$cell_ctr = 0;
}
echo "<td align='justify' cellspacing='2' cellpadding='2'>";
echo "<h3><a href='http://www.example.com/search/search.asp?txtsearch=$row[2]&catg=$row[3]&intitleanddesc=1&isnavbarsearch=1&gallery=1'>" . $row[2] . "</a></h3>";
echo "</td>";
$cell_ctr++;
}
if($cell_ctr == 1)
{
echo "</tr>";
}
else if($cell_ctr == 2)
{
echo "</tr>";
}
echo"</table>";
?>
现在它显示这个
珠宝
- 1 2
- 3 4
- 5 6
- 7 8
但我想像这样按列排序:
珠宝
- 1 5
- 2 6
- 3 7
- 4 8
数据库中的字段是:
category_anchor
keyword
brand_anchor
category_id
我只有一个 while 循环,使用 $lasteventname 我只打印一次类别
有任何想法吗?