Solved

Output data side by side in a tabular way

Posted on 2011-03-22
4
255 Views
Last Modified: 2013-12-13
Most explanation are included with the code excerpt
I would like getting three sets of each $un and $thumb_name side by side
then go to the next three
etc... etc...
This output data one one after the other one in a vertical table organized way

$num=$db->num_rows($result3);
$i=0;
while ($i <$num)
{	
	$un=mysql_result($result3,$i,"username");
	$thumb_name=mysql_result($result3,$i,"thumb_name");
	echo"<tr><td>$un</td><td>$thumb_name</td></tr>";
	
$i++;
}

What I am looking for is:
echo"<tr><td>$un</td><td>$thumb_name</td><td>$un</td><td>$thumb_name</td><td>$un</td><td>$thumb_name</td></tr>";	
where results will show three sets at a time side by side
then jump to the next three set etc...
and taking care of left over from $i%3 if it comes to an odd number
but I do not figure how to do that

Open in new window

0
Comment
Question by:array007
  • 3
4 Comments
 
LVL 14

Accepted Solution

by:
Scott Madeira earned 500 total points
ID: 35189627
Try this:

 
$num=$db->num_rows($result3);
$i=0;

// Start the first row
echo '<tr>';

while ($i <$num)
{	
	$un=mysql_result($result3,$i,"username");
	$thumb_name=mysql_result($result3,$i,"thumb_name");
	        
        // Spit out the data
	echo"<td>$un</td><td>$thumb_name</td>";
	if ( (( $i+1)%3) ==0 ) {
		// End of row
		echo '</tr>';
	} 


$i++;
}

if (! $i%3) {
    // Clean up last partial row
    echo '</tr>';
}

Open in new window


You may need to play with the last if statement to make sure it ends the row for partial cases.
0
 
LVL 1

Author Comment

by:array007
ID: 35189758
Thanks
But it is still outputting one above the other one
as:
<tr><td>aaa</td><td>bbb</td></tr>
<tr><td>aaa</td><td>bbb</td></tr>

instead of three results side by side as:

<tr><td>$un</td><td>$thumb_name</td> <td>$un</td><td>$thumb_name</td> <td>$un</td><td>$thumb_name</td></tr>
<tr><td>$un</td><td>$thumb_name</td> <td>$un</td><td>$thumb_name</td> <td>$un</td><td>$thumb_name</td></tr>

etc....
0
 
LVL 1

Author Comment

by:array007
ID: 35190696
Any idea on how to fix it?
0
 
LVL 1

Author Closing Comment

by:array007
ID: 35193937
I cleaned my OWN code error and now
it works fine
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question