[英]Display mysql results with php in columns and sorted per column
I have the following code to display some brands per category. 我有以下代码来显示每个类别的一些品牌。
<?
$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>";
?>
right now it displays this 现在它显示了这个
Jewels 珠宝
But i want to order it per column like this: 但我想按照这样的顺序为它排序:
Jewels 珠宝
The fields in the database are: 数据库中的字段是:
category_anchor
keyword
brand_anchor
category_id
I have only one while loop and with the $lasteventname i print the category only once 我只有一个while循环和$ lasteventname我只打印一次类别
Any Ideas? 有任何想法吗?
is it need to be a table ? 它需要成为一张桌子吗? for my idea i will do with list items
对于我的想法,我将使用列表项
<?php
$result = mysql_query($query);
$rows = mysql_num_rows($result);
$i = 0;
echo '<ul>';
while($row=mysql_fetch_array($result))
{
if (($i % 4) == 0 && $i > 0) {
echo '</ul><ul>';
}
echo '<li><h3>your code</h3></li>';
$i++;
}
echo '</ul>';
?>
the result will be like this, then you can style it like table as you want 结果将是这样的,然后你可以像你想要的那样设置它的样式
<div class="banner">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<ul>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.