Solved

Output data side by side in a tabular way

Posted on 2011-03-22
4
247 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now