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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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
Ray PaseurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.