Gossamer Forum
Home : General : Databases and SQL :

Split MYSQL select results into 2 columns

Quote Reply
Split MYSQL select results into 2 columns
Hi! I'm anewbie in using MySQL and PHP. I manage to search MYSQL database for some terms and it's returning the results in a table in html. However, as the search results can be very long, I was wondering if it was possible to split the results into 2 column table.

This is what I get now:

ID CD
1 A
2 B
3 C
4 D

and so on...

But I would like it to be:

ID CD ID CD
1 A 2 B
3 C 4 D
and so on...

I tried to find many articles on using both PHP codes and mysql codes to achieve what I need but to no avail. Could anyone help me, please?
Julian
Quote Reply
Re: [vampy] Split MYSQL select results into 2 columns In reply to
You say it's returning the results in a table in html... This, obviously, is not the default format for a MySQL result set. Are you using some program/script/utility that is outputting the html? Or are you using a loop function in PHP to output the results as html? What you are looking to do is surely possible, but you'll need to provide more information about how the html is being generated before anyone can tell you what to do to format it differently.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Split MYSQL select results into 2 columns In reply to
Thanks. Yes, I'm using a phph script to return the results in a html table.
Code:
$query = "SELECT * FROM discos ORDER BY id";
$result = mysql_query ($query)
or die ("Query failed<BR>");
print "<table border=0 cellpadding=2>\n";
print "<tr><td><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#FF9900\" size=\"2\"><b>ID</b></font></td>\n";
print "<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#FF9900\" size=\"2\"><b>CD No.</b></font></td></tr>\n";
while ($row = mysql_fetch_object ($result)) {
print('<tr class="searchRow">');
foreach($row as $value){
print('<td class="searchCell">'.$value.'</td>');
}
print('</tr>');
print "</table>\n";

Thanks.
Julian
Quote Reply
Re: [vampy] Split MYSQL select results into 2 columns In reply to
There must be more to that script, since what you've included doesn't appear to provide a closing bracket for that while loop.

Nonetheless, you could accomplish something like what you're hoping to do with:

$num_rows = mysql_num_rows($result);
$half_rows = ($num_rows / 2);

And then insert a counter into your while loop so that it's actually outputting two html tables side by side (maybe just use nested tables, so that each one is in a <td> in the same <tr> of a larger table that contains all the data). The table on the left contains all the rows up to $half_rows, and then the table on the right contains the rest.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Split MYSQL select results into 2 columns In reply to
Opps... I miss that out.Blush I'll try and see whether I can figure it out.
Julian
Quote Reply
Re: [hennagaijin] Split MYSQL select results into 2 columns In reply to
Thanks... I got it to work now.
Julian