Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php按列显示mysql结果,并按列排序_Php_Html_Mysql - Fatal编程技术网

使用php按列显示mysql结果,并按列排序

使用php按列显示mysql结果,并按列排序,php,html,mysql,Php,Html,Mysql,我有以下代码来显示每个类别的一些品牌 <? $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

我有以下代码来显示每个类别的一些品牌

<?
$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>";
?>
我只有一个while循环,使用$lasteventname只打印一次类别


有什么想法吗?

需要一张桌子吗?根据我的想法,我将处理列表项

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

    ?>

结果将是这样的,然后您可以根据需要将其设置为类似于表的样式

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

  • 一,
  • 二,
  • 三,
  • 四,
  • 五,
  • 六,
  • 七,
  • 八,

看看-谢谢。问题在于显示类别标题(category)以及每个类别的值下面。现在我有这样一个例子:如果($row[0]!=$lasteventname){$title=$row[0];$lasteventname=$row[0];echo“$title”;}一次显示类别,您可以将另一个li放入类中,如
  • ,这将具有css清除:这两个属性,如果无法使其工作,请回复
    <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>