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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.