Jump to content

Need to display results in multiple columns.. not working


simcoweb

Recommended Posts

I want to display sub-category titles/links in a two column format. I've got this code currently but it's only displaying a single column.

 

<?php
// run query
$sql = "SELECT * FROM subcategories";
$results = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($results) or die(mysql_error());
while ($row = mysql_fetch_array($results)){
$totalColumns = 2;
$i = 1;
echo "<table border='0' cellpadding='2'>";
for ($b=0;$b<3;$b++)
{
if ($i == 1)
	echo "<tr>";

echo "<td>";
echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>";
echo "</td>";

if ($i == $totalColumns)
	{
	echo "</tr>";
	$i = 0;
	}
$i++;
}

echo "</table>";
}
?>

 

I want it to look like:

boatsbooks

stuffjunk

thingscrap

 

etc.

 

Here try my code. Just change the #10 in the for() function for how many links you have and the number linked to $rows for how many links you want in each row:

 


$rows = 7;
$i = 1;
echo "<table align=\"center\">";
for ($n=1; $n<=10; $n++) {

if ($i == 1) 

	echo "<tr>";

	echo "<td>test</td>";

if ($i == $rows) {

	echo "</tr>";
	$i = 0;

}

$i++;

}

echo "</table>";

 

Just replace "test" with your info

try

<?php
// run query
$sql = "SELECT * FROM subcategories";
$results = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($results) or die(mysql_error());
$totalColumns = 2;
$i = 1;
echo "<table border='0' cellpadding='2'>";
while ($row = mysql_fetch_array($results)){
if ($i == 1) echo "<tr>";
echo "<td>";
echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>";
echo "</td>";
if ($i == $totalColumns)
	{
	echo "</tr>";
	$i = 0;
	}
$i++;
}
echo "</table>";
?>

Ok, before we all warmy swarmy and take showers together.... another small issue has arisen.

 

The best way I can describe it is it's not showing all the entries in the database. It's leaving one item out of each list. For example,

 

I have this:

 

$sql = "SELECT * FROM subcategories WHERE cat_id='1'";

 

cat_id #1 has 7 entries. It only displays 6. cat_id #2 has 24 entries. It only displays 23. There's 7 main categories ( cat_id=' ' ).

 

The item that is getting dropped is the very first item in the database in each incident. Ideas?

Try this: (hopefully you get my idea of what im trying to do)

 


<?php
// run query
$sql = "SELECT * FROM subcategories";
$results = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($sql) or die(mysql_error());
$totalColumns = 2;
$i = 1;
$cat = $count['cat_id'];
echo "<table border='0' cellpadding='2'>";
while ($row = mysql_fetch_array($results)){

  for ($c=1; $c<= $cat; $c++) {

    if ($i == 1)
       echo "<tr>";
       echo "<td>";
       echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>";
       echo "</td>";

       if ($i == $totalColumns)
       {
          echo "</tr>";
          $i = 0;
       }
$i++;
}

}
echo "</table>";
?>  

 

Your code:

 


<?php
// run query
$sql = "SELECT * FROM subcategories";
$results = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($results) or die(mysql_error());
while ($row = mysql_fetch_array($results)){
$totalColumns = 2;
$i = 1;
echo "<table border='0' cellpadding='2'>";
for ($b=0;$b<3;$b++)
{
if ($i == 1)
	echo "<tr>";

echo "<td>";
echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>";
echo "</td>";

if ($i == $totalColumns)
	{
	echo "</tr>";
	$i = 0;
	}
$i++;
}

echo "</table>";
}
?>

 

you don't need to define $row twice, if you are using a while statement always define $row in the while statement. You weren't counting the number of rows you had so the for() didn't know where to start or when to stop. I hope this doesn't confuse you.

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.