Forum Moderators: coopster

Message Too Old, No Replies

printing multiple mysql rows in one html row w/ php

         

skateboard

1:41 pm on Oct 2, 2004 (gmt 0)

10+ Year Member



Howdy folks,

First timer here, but here goes.

I have a mysql query that returns 6 rows with 3 fields each. I would like to print these in two html rows with three columns each, and each column having a row for each of the 3 fields.

Any suggestions would be much appreciated.

Thanks,

Doug

skateboard

3:24 am on Oct 3, 2004 (gmt 0)

10+ Year Member



I found this story on the subject:
Display Mutliple Records Per Row [devshed.com]

here is what I have come up with up to this point using what I could with above tutorial. It prints an overly long column of imageless tn's. Here goes:

<?php

$result = mysql_query("
SELECT
A.ID,
A.style,
A.tn,
A.desc
FROM
A
WHERE
A.ID >0");
if($r = mysql_fetch_array($result))
{
print("<table>");
print("<tr>");
$num = @mysql_num_rows($result);
$thumbcols = 3;
$thumbrows = 1+ round($num / $thumbcols);
function display_table() {
global $num, $result, $thumbrows, $thumbcols;
for($r=1;$r<=$thumbrows;$r++) {
print '<tr>';
for($c=1;$c<=$thumbcols;$c++) {
print '<td bgcolor="#999999" align="center" valign="top">';

printf("<a href=\"%s?ids=%s\"><img src=http://foobar.com/foobar/styles/%s></a><br>\n", $PHP_SELF, $r["ID"], $r["tn"]);
printf("<a href=\"%s?ids=%s\">%s</a><br>\n", $PHP_SELF, $r["ID"], $r["style"]);

print("</td>");
}
print("</tr>");
}
}
display_table();
print("</table>");
}
else {
print '&nbsp;';
}

?>
Any suggestions. I feel like I am close, but OH SO FAR!

Is it ethical to use the tutorial script in this way?

Thanks

[edited by: ergophobe at 4:10 pm (utc) on Oct. 9, 2004]
[edit reason] fixed sidescroll [/edit]

dreamcatcher

8:42 am on Oct 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World Skateboard.

This snippet of code was posted by coopster a while back:

$item_per_row = 3;
$content = '<tr>';
$sql = "SELECT * FROM table";
$rows = mysql_query($sql);

if (mysql_num_rows($rows) == 0)
{
$content = '<td>No Items</td></tr>';
}
else
{
$count = 0;
while ($row = mysql_fetch_assoc($rows))
{
$content .= '<td>' . $row['item'] . '</td>';
if (++$count % $item_per_row === 0)
{
$content .= '</tr>';
}
if ($count % $item_per_row!== 0)
{
$content .= '</tr>';
}

It works like a charm and may be some use to you.

:)

skateboard

12:21 pm on Oct 3, 2004 (gmt 0)

10+ Year Member



Dreamcatcher,

Thanks, the code seems simple and straightforward. I will play with it and report back.

I very much appreciated the help.

:)

dreamcatcher

3:10 pm on Oct 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, no problem, we are here to help each other. :)

skateboard

8:02 pm on Oct 8, 2004 (gmt 0)

10+ Year Member



where can I find a difference between these two modes of scripting PHP:

A.
printf($r["ID"]);

B.
$content .= '<td>' . $row['ID'] . '</td>';

B seems more efficient

I outfitted the graciously provided code, above, with my data and it prints nothing.?

thanks

skateboard

10:05 pm on Oct 8, 2004 (gmt 0)

10+ Year Member



heres what I've got, but it prints the first row twice, then the second row:

<table border=5>
<?
$item_per_row = 3;
$content = '<tr>';
$sql = "SELECT name FROM table";
$rows = mysql_query($sql);
if (mysql_num_rows($rows) == 0) {
$content = '<td>No Items</td></tr>';
} else {
$count = 0;
while ($row = mysql_fetch_assoc($rows)) {
$content .= '<td>' . $row['name'] .'</td>';
if (++$count % $item_per_row === 0) {
print $content .= '</tr>';
}
}
if ($count % $item_per_row!== 0) {
$content .= '</td>';
}
}
?>
</table>

I have added two "}" as well as a "print" and changed the final "</tr>" to "</td>"

[edited by: jatar_k at 10:40 pm (utc) on Oct. 8, 2004]
[edit reason] reformatted code [/edit]

skateboard

3:12 am on Oct 9, 2004 (gmt 0)

10+ Year Member




<table border=5>
<?
$item_per_row = 3;
$content = '<tr>';
$sql =
"SELECT
a,
b,
c,
d
FROM
table
GROUP BY
d
ORDER BY
d
ASC
";

$rows = mysql_query($sql);
if (mysql_num_rows($rows) == 0)
{
$content = '<td>No Items</td></tr>';
}
else
{
$count = 0;
while ($row = mysql_fetch_assoc($rows))
{
$s='http://foo.com';
$content .= '<td valign=top>' . '<b><a href=http://foo.com/bar.php?idg='.$row['a'].'>'.$row['b'] .'</a></b><br>' . $row['c'].'<br>' .'<img src=http://foo.com/bar/'.$row['d'] .'>'. '</td>';
if (++$count % $item_per_row === 0)
{
}
}
print $content .= '</tr>';
}
if ($count % $item_per_row!== 0)
{
$content .= '</td>';
}
?>
</table>

This prints one row, with the mysql rows arranged in columns.

Getting there.......
ideas?