...

View Full Version : Formatting Output?



dniwebdesign
09-12-2005, 01:10 AM
<?php
$textlist = mysql_query("SELECT * FROM utexts_category") or print mysql_error();
$count=1;
if(mysql_num_rows($textlist))
{
while($row=mysql_fetch_array($textlist))
{
?>
<td valign="top"><img src="files/folder.gif" width="31" height="30" align="absmiddle"><a href="list.php?cat=<?php echo $row["link"]; ?>"><strong><?php echo $row["category"]; ?></strong></a>*( <?php $total_results = mysql_num_rows(mysql_query("SELECT * FROM ".$dbprefix."_catalog WHERE category='".$row["link"]."' ".$sql)); echo "$total_results"; ?> )</td>
<?php



$count++;
}
}
?>

Alright I got the following PHP code which works excellent... Just a bit of a formatting question.
The current output of the script puts out categories in one row...
Example:

Cat1 Cat2 Cat3 Cat4 Cat5 Cat6 Cat7 Cat8
// And so on


However I only want rows of 3....


Cat1 Cat2 Cat3
Cat4 Cat5 Cat6
Cat7 Cat8


How would I go about implementing this?

delinear
09-12-2005, 11:34 AM
Assuming you have an opening <tr> prior to your code, you can implement a little count within your code which adds three columns then returns to column one of the next row:


<?php
$textlist = mysql_query("SELECT * FROM utexts_category") or print mysql_error();
$count=1;
if(mysql_num_rows($textlist))
{

$col=1; // we start with column 1

while($row=mysql_fetch_array($textlist))
{
?>
<td valign="top"><img src="files/folder.gif" width="31" height="30" align="absmiddle"><a href="list.php?cat=<?php echo $row["link"]; ?>"><strong><?php echo $row["category"]; ?></strong></a>*( <?php $total_results = mysql_num_rows(mysql_query("SELECT * FROM ".$dbprefix."_catalog WHERE category='".$row["link"]."' ".$sql)); echo "$total_results"; ?> )</td>
<?php

$count++;
// now we check which column we are on and whether we need to insert a new row
if($col==3) {
$col = 1;
?>
</tr><tr>
<?php
} else {
$col++;
}

}
}
?>

missing-score
09-12-2005, 01:24 PM
The above code will work fine provided that you have a multiple of 3 categories, however if you dont then it could be a problem... I personally would do this.

P.S: On a side note, closing PHP parsing (?>) then opening again (<?php) is ok, but I wouldnt reccomend it inside loops... This generally takes more time than just priting out the content to the page. However, I will leave your current method itact in my example...




$counter = 0;

while($row=mysql_fetch_array($textlist))
{

if($counter % 3 == 0)
{
echo '<tr>';
}

$counter++;

?>
<td valign="top"><img src="files/folder.gif" width="31" height="30" align="absmiddle"><a href="list.php?cat=<?php echo $row["link"]; ?>"><strong><?php echo $row["category"]; ?></strong></a>*( <?php $total_results = mysql_num_rows(mysql_query("SELECT * FROM ".$dbprefix."_catalog WHERE category='".$row["link"]."' ".$sql)); echo "$total_results"; ?> )</td>
<?php

if($counter % 3 == 0)
{
echo '</tr>';
}

}

$remaining = ($counter % 3);
if($remaining > 0)
{
echo str_repeat('<td>&nbsp;</td>', $remaining);
echo '</tr>';
}


You should note that this is untested, but in my head it seems like it should work ;)

delinear
09-12-2005, 01:28 PM
The above code will work fine provided that you have a multiple of 3 categories, however if you dont then it could be a problem...

You're absolutely right. I've spent so long trying to get my mind out of the table-layout mindset that I completely overlooked the messy results of having only one or two items in the final row of the table - oops! :p

mrruben5
09-13-2005, 06:06 PM
This could be done with css. Just set float on the generated divs, give them a width of 33%. After the divs put a div with float:auto



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum