Forum Moderators: coopster

Message Too Old, No Replies

Displaying mysql results in a table

mysql results display table

         

matthewamzn

5:20 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



I know how to display the mysql results in a table vertically. One product below another. I would like to create a table that would show 4 products side by side (more like a grid). Here is the script I'm using to currently display the results:

echo "<table border=\"1\">\n";
while ($row = mysql_fetch_assoc($results)) {
echo "<tr>\n";
foreach ($row as $value) {
echo "<td>\n";
echo $value;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";

jatar_k

5:35 pm on Aug 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this one

Create a Dynamic Table from mysql result [webmasterworld.com] msg 4

matthewamzn

6:08 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



That script did work, but I'm having a problem with duplicated results. Instead of 3 columns in the table, I'm getting 6. The extra three columns are duplicates. Cany anyone see the problem?

$tdcount = 1;
$numtd = 3; // number of cells per row

//display the results in a table
echo "<table border=\"1\">";
while($row = mysql_fetch_array($results)) {
if ($tdcount == 1) echo "<tr>";
foreach ($row as $value) {
echo "<td>$value</td>"; } // display as you like
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

ergophobe

7:00 pm on Aug 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What is the row size? You aren't including the row size checks in your foreach (just the echo), so you get as many columns as there are items in your rows. The root of the problem must be with your query. Echo your query and then run that through the mysql client and see if you're getting the results you expect. Then debug the table output.

matthewamzn

7:16 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



This is the query I'm using:

$query = "SELECT name " .
"FROM example ";
$results = mysql_query($query)
or die(mysql_error());
$tdcount = 1;
$numtd = 3;

coopster

3:20 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What ergophobe is referring to is to run your command and dump the results so you can see what is actually coming back in your data. If you use the command line you can see if the duplicates are actually being returned and then modify your query accordingly.