?
Solved

Output data side by side in a tabular way

Posted on 2011-03-22
4
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 14

Accepted Solution

by:
Scott Madeira earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

719 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