How to print multiple database results per table row?

Experts,

I know how to create a table using data from my db using a while loop.  Doing so places a single db result in each row until the query is completed.  How can I get multiple db results to print in the same row as in the example below?

Example:

<table.....>
<tr><td>Result 1</td><td>Result 2</td><td>Result 3</td></tr>
<tr><td>Result 4</td><td>Result 5</td><td>Result 6</td></tr>
</table>

Not:

<table...>
<tr><td>Result 1</td></tr>
<tr><td>Result 2</td></tr>
<tr><td>Result 3</td></tr>
<tr><td>Result 4</td></tr>
<tr><td>Result 5</td></tr>
<tr><td>Result 6</td></tr>
</table>
evibesmusicAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
I think you would just need to count the number of <td> elements you produced for each <tr> -- maybe something like this:
// START THE TABLE
echo "<table>" . PHP_EOL;

// HOW MANY COLUMNS IN EACH HTML ROW
$td_limit   = 3;

$td_counter = 0;
while ($row = mysql_fetch_assoc($res))
{
    // START A NEW HTML TABLE ROW?
    if ($td_counter == 0)
    {
        echo "<tr>";
        $td_counter = $td_limit;
    }
    
    // PRODUCE THE HTML TABLE DATA
    echo "<td>" . $row["thing"] . "</td>";
    
    // DECREMENT THE COUNTER
    $td_counter--;
    
    // IF WE REACHED THE END OF THE HTML TABLE ROW
    if ($td_counter == 0)
    {
        echo "</tr>" . PHP_EOL;
    }
}

// CLOSE OUT THE LAST ROW, IF NECESSARY
$td_flag = FALSE;
while ($td_counter > 0)
{
    // SET A FLAG TO KNOW THAT WE NEED TO CLOSE THE ROW
    $td_flag = TRUE;
    echo "<td></td>";
    $td_counter--;
}

// IF NEEDED
if ($td_flag) echo "</tr>" . PHP_EOL;

// CLOSE THE TABLE
echo "</table>" . PHP_EOL;

Open in new window

0
 
Om PrakashCommented:
You need to store multilpe results in common array and then dislpay in common table.
0
 
evibesmusicAuthor Commented:
@om_prakash_p:

Can you give me a simple example of how to do so?  I have never done that before...cheers.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
shaunakCommented:
You are trying for multiple tables or multiple database?

If you are trying to get data from multiple tables, then you need to use JOIN statement.

e.g. look at the below data

table: usernames
user_id, user_name
1,user1
2,user2
3,user3
4,user4

table: userdetails
userdetails_id, fullname, city, country,user_id
1,name1,city1,country1,4


The two table stores the data. This is called normalizing of tables. The first row in table userdetails holds the value of user4 from the table usernames


Now to retrieve the data into a single result you
SELECT usernames.user_name, userdetails.*FROM userdetails INNER JOINusernames on usernames.user_id = userdetails.user_id

If you want from multiple databases, then you need to loop through separate queries in your while loop



0
 
shaunakCommented:
If you are looking for just printing the result set in the mentioned form then this might help:



<?php
$c_link = mysql_connect($dbhost, $dbuser, $dbpass) 
or die('counter CONNECT error: '.mysql_errno().', '.mysql_error());
			
mysql_select_db($dbname);

$sql="your query";

$strresultset = mysql_query($sql,$c_link)
or die('Counter error: '.mysql_errno().', '.mysql_error());


$rowcount=mysql_num_rows($strresultset);
?>

<table>
<?php

for ($i=0;$i<$rowcount;$i++)
{
?>
<tr>
	<td><?=if (isset(mysql_result($strresultset,$i,'feildname')) echo mysql_result($strresultset,$i,'feildname');?></td>
	<td><?=if (isset(mysql_result($strresultset,$i+1,'feildname')) echo mysql_result($strresultset,$i+1,'feildname');?></td>
	<td><?=if (isset(mysql_result($strresultset,$i+2,'feildname')) echo mysql_result($strresultset,$i,'feildname');?></td>
</tr>
<?php	
}
?>
</table>

Open in new window

0
 
evibesmusicAuthor Commented:
@All:

To clarify, the results of my query are coming from only one db table.  So the example that shaunak is referring to is probably not needed.  Limiting the number of results that are printed per row seems like the more likely solution.

Let me take a closer look at the suggested solutions and then get back to you all.  Thanks for your assistance.
0
 
markloganCommented:
Using Rays solution you can use the loop count to work out if you have output a full rows worth of data.


Returns the floating point remainder of dividing the dividend (x) by the divisor (y). The reminder (r) is defined as: x = i * y + r, for some integer i. If y is non-zero, r has the same sign as x and a magnitude less than the magnitude of y.

http://php.net/manual/en/function.fmod.php

So it will return a zero if the number is divisible by 3.

And yes you will still need to clear up the table end.
$tableRowEnd = (fmod($i++, 3)) ? '</tr><tr>'  : '';


...


echo "<td>" . $row["thing"] . "</td>" . $tableRowEnd;

Open in new window

0
 
evibesmusicAuthor Commented:
Thanks Ray.  Sorry for the late response but, I was pulled off on another task...the solution is great, easy to understand.  I implemented it successfully.

Cheers!

EVibesMusic
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.