Forum Moderators: coopster

Message Too Old, No Replies

Displaying Query Results In A Two Column Table

         

keep it loose

12:33 am on Aug 1, 2005 (gmt 0)

10+ Year Member



Hello. I apologize for registering and opening up with a question rather than a solution, but I'm stumped.

I have a script that I am "developing" and am able to grab and display the results that I want, but I am unable to properly format the results in a table such that the first x number of results are displayed in one column, and the next x number of results are displayed in a second column.

I have read the posts pertaining to formatting query results, but I am unable to fit these suggestions into my script.

I'll post the full script and then the portion of the script that I wish to have displayed in a two column table (ordered with half of the results in one column, the other half of the results in the second column).

Basically it is a link page script. While I appreciate the intentions of posters who may suggest a third-party app as an alternative, I wish to utilize my own script.

The script works fine, I just can't get it formatted as I wish.

Full Script:

<?

$db = mysql_connect("localhost", "un", "pwd");

mysql_select_db("my_db",$db);

// display individual record

if ($id) {

$result = mysql_query("SELECT * FROM table_1 WHERE category=$id",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("<b>%s\n</b><br>", $myrow["title"]);

printf("%s\n<br>", $myrow["description"]);

printf("<a href=\"%s\">%s</a><br><br>\n", $myrow["url"], $myrow["url"]);

}

} else {

// show list

$result = mysql_query("SELECT * FROM table_2",$db);

if ($myrow = mysql_fetch_array($result)) {

// display list if there are records to display

do {

printf("<a href=\"%s?id=%s\">%s %s</a><br><br>\n", $PHP_SELF, $myrow["id"], $myrow["name"], $myrow[""]);

} while ($myrow = mysql_fetch_array($result));

} else {

// no records to display

echo "Sorry, no records were found!";

}

}

?>

The results that I want to have displayed in a two column table (half the results in one column, the second half in the second column):

printf("<a href=\"%s?id=%s\">%s %s</a><br><br>\n", $PHP_SELF, $myrow["id"], $myrow["name"], $myrow[""]);

} while ($myrow = mysql_fetch_array($result));

Any input would be greatly appreciated. I apologize again for starting my Webmaster World posting career with a request, but I just can't get this to work as intended.

I have found the members of these forums to be extremely knowledgeable and helpful and know that one of you will offer a great solution to my problem!

Cheers

keep it loose

2:13 am on Aug 1, 2005 (gmt 0)

10+ Year Member



Quick bump before I give up for the night. :)

/bump

Anyango

6:11 am on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yo!

why dont you create a <table boder=0> before entering the print loop and in your print loop you can simply check if loop counter is even ie if it starts from 0 you can check if its 2 then print a "<tr>" and then when printing your data you just add a "<td>"

so that before starting of your loop, a hidden table will be created right, and on each EVEN index a new <tr> will be created that will take pointer to next line and on each main print statement in your loop a <td> will be created which will take pointer to next column in the same row so that in the end you ll get it alligned in 2 column format, i guess that is what you need.

Thanks

Kami


keep it loose

7:40 am on Aug 1, 2005 (gmt 0)

10+ Year Member



Kami,

Thanks for the reply. That sounds like it will work exactly the way I'd like it to, but how would I do it?

mcibor

1:02 pm on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a snipet that should have the work done:

...
} else {
// show list
$sql = "SELECT * FROM table_2";
$result = mysql_query($sql, $db) or die(mysql_error());

echo "<table>\n";
$even = 0;
if(mysql_num_rows($result)) {
while($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
if(!$even) echo "<tr>\n";
printf("<td><a href=\"%s?id=%s\">%s %s</a></td>\n", $PHP_SELF, $myrow["id"], $myrow["name"], $myrow[""]);
if(!$even) echo "</tr>\n";
$even = 1 - $even;
}
echo "</table>\n";
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>

Best regards
Michal Cibor

keep it loose

2:10 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Mr. Cibor,

Thank you very much for your suggestion, but this also displays the data in a one column table. It looks better than the output that I was able to generate, but it still displays the data in one very long column, rather than two seperate, alternating tables.

I am extremely grateful for your time, though.

I am still trying to find a solution to my quesiton, so please don't think that I'm sitting idly by waiting for one of you cats to waste your time on a newb's problem.

mcibor

3:53 pm on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I mixed the logic a bit. It should be:

..
} else {
// show list
$sql = "SELECT * FROM table_2";
$result = mysql_query($sql, $db) or die(mysql_error());

echo "<table>\n";
$even = 0;
if(mysql_num_rows($result)) {
while($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
if(!$even) echo "<tr>\n";
printf("<td><a href=\"%s?id=%s\">%s %s</a></td>\n", $PHP_SELF, $myrow["id"], $myrow["name"], $myrow[""]);
if($even) echo "</tr>\n";//here without the!
$even = 1 - $even;
}
echo "</table>\n";
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>

mcibor

3:54 pm on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also put the results in 3 or more columns, but that will recuire the modulo function (%): 0 % 3 = 0; 1 % 3 = 1; 2 % 3 = 2; 3 % 3 = 0; 4 % 3 = 1...(the rest of division)
and then $count = 0;
$columns = 3;
while...
if($count == 0) echo "<tr>";
print...
if($count == $columns - 1) echo "</tr>";
$count = ++$count % $columns;

There's also a problem with closing of last <tr> (which should be resolved with:)

while{...}
if($count!= 2) echo "</tr>\n";
echo "<table>\n";

Best regards
Michal Cibor

PS. The above code will work for $columns = 2 as well.

bomburmusicmallet

4:15 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Here's the actual code I use for displaying the files in a particular directory in a two column table.


<?php
session_start();
$numcol = '2';
$dirPath = 'recipes';
if ($handle = opendir($dirPath))
{
while (false!== ($file = readdir($handle)))
if ($file!= "." && $file!= "..")
$filesArr[] = trim($file);
closedir($handle);
//sort array of file names
$array_lowercase = array_map('strtolower', $filesArr);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $filesArr);
}
$count = count($filesArr);
$loop = $count / $numcol;
$cellwidth = 100 / $numcol;
$rem = $count % $numcol;
$pptr=-1; //array begins at 0
print '<table width="100%">';
for ($i=1; $i<=$loop; $i++)
{
print '<tr>';
for ($j=0; $j<$numcol; $j++)
{
$ptr = ++$pptr;
print '<td align="left" valign="top" width="'.$cellwidth.'%">';
print '<a href="recipes/'.$filesArr[$ptr].'" target="_blank">';
print $filesArr[$ptr].'</a></td>';
}
print '</tr>';
}
if ($rem)
{
print '<tr>';
for ($i=0; $i<$rem; $i++)
{
$ptr = ++$pptr;
print '<td align="center" valign="top" width="'.$cellwidth.'%">';
print '<a href="recipes/'.$filesArr[$ptr].'" target="_blank">';
print $filesArr[$ptr].'</a></td>';
}
//fill remaining cells with space
for ($j=$rem; $j<=$numcol; $j++)
{ print '<td>&nbsp;</td>'; }
print '</tr>';
}
print '</table>';
?>

This code will print the files in alphabetical order, but across the table, not down one column and then the other. I have that code somewhere for something much more complicated, but if you want it, let me know and I'll post it.

HTH, Jenny

[edited by: bomburmusicmallet at 4:22 pm (utc) on Aug. 1, 2005]

keep it loose

4:21 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Thank you so much! That does exactly what I want it to do...Where does one learn how to manipulate data like that? ;)

I can't thank you enough for your time.

Cheers

mcibor

4:22 pm on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's one error in the post above, so here's the final version for as many columns as you wish:

} else {
// show list
$sql = "SELECT * FROM table_2";
$result = mysql_query($sql, $db) or die(mysql_error());

echo "<table>\n";
$count = 0;
$columns = 3; //column number declaration
if(mysql_num_rows($result)) {
while($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
if($count == 0) echo "<tr>\n";
printf("<td><a href=\"%s?id=%s\">%s %s</a></td>\n", $PHP_SELF, $myrow["id"], $myrow["name"], $myrow[""]);
if($count == $columns - 1) echo "</tr>\n";//here without the!
$count = ++$count % $columns;
}
if($count!= 0) echo "</tr>\n";
echo "</table>\n";
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>

bomburmusicmallet

4:25 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Hey keep it loose, whose code did you end up using?

Just curious, Jenny

keep it loose

4:44 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Jenny,

When I received the email stating that somebody had replied to the post, Michal's was the only reply, so I went with it.

I just got another email and was suprised that there were now several responses! I will definetly give your code a shot, Jenny, and let you know how it works out.

Thanks again to everyone for the great suggestions.

Cheers