Forum Moderators: coopster
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
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
...
} 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
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.
..
} 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!";
}
}
?>
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.
<?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> </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]
} 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!";
}
}
?>
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