0

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

  • 1 2
  • 3 4
  • 5 6
  • 7 8

But i want to order it per column like this:

Jewels

  • 1 5
  • 2 6
  • 3 7
  • 4 8

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

Any Ideas?

1

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The trouble is to display the category title (category) and underneath the values of each category. Right now i have this: if($row[0] != $lasteventname){ $title = $row[0]; $lasteventname = $row[0]; echo "<tr><td colspan=5><h2>$title</h2></td></tr>"; } To Show the category once
You can put a another li with class like <li class="title"></li> and this will have css clear:both property, if can not make it work please comment back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.