Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Output data side by side in a tabular way

Posted on 2011-03-22
4
Medium Priority
?
264 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses

877 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