Page 1 of 1

Formatting output to specified number of table columns

Posted: Thu Aug 09, 2007 9:55 am
by VladSun

Code: Select all

<table>
	<?php
	$cols = 3;
	$i = 0;
	while ($sql_row = @mysql_fetch_array($sql_result_types))
	{
		if (!($i % $cols))
		{
			if ($i)
			{
				echo "</tr>";
			}
			echo "<tr>";
		}
		echo "<td>".$sql_row['name']."</td>";
		$i++;
	}
	if (($i % $cols) && $i)
	{
		echo "</tr>";
	}
	?>
</table>
This should print the data into a table with $cols columns.

Any better ways to do this?

Posted: Thu Aug 09, 2007 10:30 am
by iknownothing
maybe...

Code: Select all

$count = 1;
$cols = 3;

while ($sql_row = mysql_fetch_array($sql_result_types)) 
{
     echo "<td>" . $sql_row['name']. "</td>";

     if ($count == $cols){
          echo '</tr><tr>';
          $count = 0;
     }

     $count++;
}

Posted: Thu Aug 09, 2007 10:35 am
by matthijs
Seems better to place the query outside the loop. First get all results you need. Then loop through the result array to show what you want

Posted: Thu Aug 09, 2007 10:38 am
by VladSun
So it is now:

Code: Select all

<table>
<tr>
	<?php
	$cols = 3;
	$i = 1;
	while ($sql_row = @mysql_fetch_array($sql_result_types))
	{
		echo "<td>".$sql_row['name']."</td>";
		if ($i == $cols)
		{
			echo "</tr><tr>";
			$i = 0;
		}
		$i++;
	}
	?>
</tr>
</table>
I'm not sure whether insufficient number of "<td>" elements at the end of the table could cause any problems to browsers' render.

Posted: Thu Aug 09, 2007 10:39 am
by VladSun
matthijs wrote:Seems better to place the query outside the loop. First get all results you need. Then loop through the result array to show what you want
What will be the advantages?

Posted: Thu Aug 09, 2007 10:44 am
by iknownothing
when you implemented my version of the code, you changed $i back to 0 up the top. If you follow it counting, you'll realise its wrong, that why I set it at 1.

And whats the purpose of the error suppressor?

Posted: Thu Aug 09, 2007 10:53 am
by VladSun
1. Yes, I've noticed it - just forgot to edit it here.
2. Should I add aditional HTML code like that:

Code: Select all

<table>
<tr>
	<?php
	$cols = 3;
	$i = 1;
	while ($sql_row = @mysql_fetch_array($sql_result_types))
	{
		echo "<td>".$sql_row['name']."</td>";
		if ($i == $cols)
		{
			echo "</tr><tr>";
			$i = 0;
		}
		$i++;
	}
	while ($i <= $cols)
	{
		echo "<td></td>";
		$i++;
	}
	?>
</tr>
</table>

Posted: Thu Aug 09, 2007 11:13 am
by iknownothing
Yes, I suppose you could do that, but I'm not sure if it's necessary, but, best keep IE happy, if anything will have a whinge, its IE.

but you'll have to add:

Code: Select all

$i++
in the second while too.

Posted: Thu Aug 09, 2007 11:16 am
by VladSun
:) I've just edited it ;)

Posted: Thu Aug 09, 2007 12:06 pm
by VladSun
I am more curious of using an array function (e.g. array_walk() ) to accomplish this. It should be related to matthijs's post.

Posted: Thu Aug 09, 2007 3:05 pm
by TheMoose
VladSun wrote:I am more curious of using an array function (e.g. array_walk() ) to accomplish this. It should be related to matthijs's post.

Code: Select all

<table>
<?
$cols = 3;
$i = 3;
while ($rows[] = @mysql_fetch_array($sql_result_types)) { }
foreach($rows as $row)
{
	if !($i % $cols)
		echo "<tr>";
	echo "<td>" . $row['name'] . "</td>";
	$i++;
	if !($i % $cols)
		echo "</tr>";
}
// add this portion outside so that if there isn't an exact multiple of $cols, it will still close the table row
if ($i % $cols)
	echo "</tr>";
?>
</table>

Posted: Thu Aug 09, 2007 4:39 pm
by programmingjeff
TheMoose wrote:
VladSun wrote:I am more curious of using an array function (e.g. array_walk() ) to accomplish this. It should be related to matthijs's post.

Code: Select all

<table>
<?
$cols = 3;
$i = 3;
while ($rows[] = @mysql_fetch_array($sql_result_types)) { }
foreach($rows as $row)
{
	if !($i % $cols)
		echo "<tr>";
	echo "<td>" . $row['name'] . "</td>";
	$i++;
	if !($i % $cols)
		echo "</tr>";
}
// add this portion outside so that if there isn't an exact multiple of $cols, it will still close the table row
if ($i % $cols)
	echo "</tr>";
?>
</table>
By using this code, you are now causing your program runtime to increase exponentially at O(n^2) from having 2 loops as opposed to having 1 loop (which was posted earlier by VladSun). In this example, there is no reason to have that extra runtime overhead addition to the extra memory consumption from caching all of the names in the $rows table.

Posted: Fri Aug 10, 2007 3:00 pm
by Begby
programmingjeff wrote: By using this code, you are now causing your program runtime to increase exponentially at O(n^2) from having 2 loops as opposed to having 1 loop (which was posted earlier by VladSun). In this example, there is no reason to have that extra runtime overhead addition to the extra memory consumption from caching all of the names in the $rows table.
I really doubt you are going to notice any difference whatsoever by changing this to one loop.

Posted: Fri Aug 10, 2007 4:54 pm
by VladSun
Well, indeed the complexity increases to 0(2*n) - not O(n^2), but still there is an incremenet.
I really doubt you are going to notice any difference whatsoever by changing this to one loop.
Then ... what would happen if you have 10'000 users at the same time?!?
And it is still question of efficient programming ...

Posted: Sun Aug 12, 2007 3:28 pm
by VladSun
On second thought - if I am to follow the MVC style I'll have to do this like programmingjeff said in order to have the M-V-C separation... What do you think?

(the posting is a little offtopic, but I am very interested in getting some answers.)