Jump to content

[SOLVED] How Do You Split Results into Two Columns?


Fluoresce

Recommended Posts

The code below selects my categories, counts how many products are available for each category, separates the categories into groups, and echoes the groups, each with a heading, like this:

 

Apparel

All Apparel/Clothing (11)

Women's Apparel (7)

Men's Apparel (2)

Children's Apparel (2)

 

Automotive

All Automotive (13)

Auto Insurance (7)

Auto Parts (6)

 

Babies and Kids

All Babies and Kids (9)

Games and Toys (3)

Baby Gear (6)

 

Altogether, there are 13 such category groups which run all the way down my page.

 

Can anyone tell me, please, how I can echo these results into two table columns? Any suggestions will be greatly appreciated.

 

<?php

$conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR);  
mysql_select_db('heru_ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);

$query = "SELECT tcat.catheading, tcat.cat, tcat.category, COUNT(tcpncat.id) AS cpn_count
          FROM tcat
          LEFT JOIN tcpncat ON tcat.catid = tcpncat.catid
          GROUP BY tcat.catid
          ORDER BY tcat.cattype ASC";

$result = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR);

$last_heading = '';

while ($row = mysql_fetch_assoc($result))
{
   if ($row['catheading'] != $last_heading)
   {
      if ($last_heading)
         echo "</p>";
      $last_heading = $row['catheading'];
      echo "<h3>{$last_heading}</h3><p>";
   }
   else
      echo "<br />";
      echo "<a href=\"" . $row['category'] . "\">" . $row['cat'] . "</a> (" . $row['cpn_count'] . ")";
}

echo "</p>";

?>

put a table writer into the loop.

 

If you can't give usable advice, don't reply!!!

 

OP:

 

It depends on how you want the categories split up (i.e. which categories in which column), and how you want the categories to appear. But one way you could do it would be to do this:

 

$div1 = '<div id="div1">';
$div2 = '<div id="div2">';
$i = 2;
while($row = mysql_fetch_assoc($result))
{
  if($i % 2 == 0)
  {
    $div1 .= _____; // output one category's worth of info here
  }
  else
  {
    $div2 .= _____; // output one category's worth of info here
  }
  $i++;
}
$div1 .= '</div>';
$div2 .= '</div>';
echo $div1;
echo $div2;

 

You will also need to add this to your CSS:

#div1, #div2
{
  float:left;
  width: 50%;
}

 

This code is going to create two divs that have the category info in them, alternating back and forth between categories. It will then output the divs to the page, and the CSS will put them into two columns.

I still use TR, and TD to develop my dynamic tables, which is a little harder to do.

Harder to do than what? Tables have to have TR and TD, or they are invalid code, and you aren't doing it right. Are you trying to say that doing it right is harder than doing it wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.