...

View Full Version : Playing with rows and columns...



losse
04-28-2006, 03:14 AM
Hi there
Have a look at the attached code. This is basically a script calling some records from a table "id" and "description". This displays the records 1 per row... How would I modify this so that it displays, say 2 or 3 per row?

Thanks!




<?php

$q = mysql_query("select * from table");
$r = mysql_query($q) or die();
while($row = mysql_fetch_assoc($r)){
print "<a href='product.php?id=$row[id]'>$row[description]</a><br/>";
}
?>



Say i wanted to display records 3 per row instead of 1 per row, how would this code be modified?

Thanks

Nightfire
04-28-2006, 03:50 AM
<?php

$q = mysql_query("select * from table LIMIT 3");
$r = mysql_query($q) or die();
while($row = mysql_fetch_assoc($r)){
print "<a href='product.php?id=$row[id]'>$row[description]</a><br/>";
}
?>

zactanaz
04-28-2006, 05:26 AM
You want to put 3 in the same row, like this:

Link 1 - Link 2 - Link 3

If this is what you whan then try this:



$q = mysql_query("select * from table");
$r = mysql_query($q) or die();
$numrows = 0;
print "<table><tr>";
while($row = mysql_fetch_assoc($r)){
print "<td align=\"center\"><p align=\"center\"><a href='product.php?id=$row[id]'>$row[description]</a></p>";
$numrows++;
if($numrows > 2) {
echo "</td></tr><tr>";
$numrows = 0;
} else {
echo "</td>";
}
}
echo "</tr></table>";

bustamelon
04-28-2006, 06:57 AM
Something like this would work too:




<?php

$q = mysql_query("select * from table");
$r = mysql_query($q) or die();
$i = 1;
while($row = mysql_fetch_assoc($r)){
print "<a href='product.php?id=$row[id]'>$row[description]</a>";
if( $i % 3 == 0 ) {
print "<br/>";
}
$i++;
}
?>

This is known as MODULUS. It says "if $i is divisible by 3".

losse
04-28-2006, 11:59 AM
Thanks gang... I'll play around with the variations and see which works for me.

I appreciate your input

marek_mar
04-28-2006, 12:14 PM
Modulo doesn't anwser the question if x is dividable by y (directly). It gives the remainder of dividong x by y. Modulo can return values from 0 to y - 1.

GJay
04-28-2006, 12:49 PM
if( $i % 3 = 0 ) {
print "<br/>";
}

Should be:


if($i%3==0) {
print "<br />";
}

= is assignment, and will evaulate to true.

losse
04-28-2006, 01:10 PM
Hi gang... I liked the way this one worked:




$q = mysql_query("select * from table");
$r = mysql_query($q) or die();
$numrows = 0;
print "<table><tr>";
while($row = mysql_fetch_assoc($r)){
print "<td align=\"center\"><p align=\"center\"><a href='product.php?id=$row[id]'>$row[description]</a></p>";
$numrows++;
if($numrows > 2) {
echo "</td></tr><tr>";
$numrows = 0;
} else {
echo "</td>";
}
}
echo "</tr></table>";



So the script that zactanaz provided allows me to put 3 of these side by side:

+-----------------+
| name |
+-----------------+

However each of the items in my table owns 2 rows of data... Something like this:

+-----------------+
| name |
+-----------------+
| description |
+-----------------+

So how would I input another row in there so that you have the name and description 3 times, before it dumps it onto a second line?

Thanks

bustamelon
04-28-2006, 02:14 PM
if( $i % 3 = 0 ) {
print "<br/>";
}

Should be:


if($i%3==0) {
print "<br />";
}

= is assignment, and will evaulate to true.


Good catch. It was a typo.

Thanks for the explanation though. Duly noted and post edited. :thumbsup:

losse
04-28-2006, 05:35 PM
Thanks for that.. I was wondering what was up with that...

But how do I add a second row to each record so that my php script displays 2 rows times 3 columns before it drops the record onto an additional row?

See my previous post...

Thanks

bustamelon
04-28-2006, 08:30 PM
Just gotta play around with the cells. Something like this perhaps (not tested):


$q = mysql_query("select * from table");
$r = mysql_query($q) or die();
$numcols = 1;
$numrows = 1;
print "<table><tr>";
while($row = mysql_fetch_assoc($r)){
print "<td align=\"center\"><p align=\"center\"><a href='product.php?id=$row[id]'>$row[description]</a></p>";

if($numcols > 2) {
echo "</td></tr><tr>";
$numcols = 1;
$numrows++;
} else {
echo "</td>";
}
$numcols++;

if($numrows > 1) {
echo "</tr><tr>";
$numrows = 1;
}
}
echo "</tr></table>";

Edit: oops, dunno what I was thinking... the next post has it right

zactanaz
04-28-2006, 08:42 PM
$q = mysql_query("select * from table");
$r = mysql_query($q) or die();
$numrows = 0;
print "<table><tr>";
while($row = mysql_fetch_assoc($r)){
print "<td align=\"left\">";
print "<a href='product.php?id=$row[id]'>$row[name]</a><br>";
print "<a href='product.php?id=$row[id]'>$row[description]</a>";
$numrows++;
if($numrows > 2) {
echo "</td></tr><tr>";
$numrows = 0;
} else {
echo "</td>";
}
}
echo "</tr></table>";

This should print something like this three times in a row:

+--------------+
| Name
| Description
+--------------+

marek_mar
04-28-2006, 10:59 PM
if( $i % 3 = 0 ) {
print "<br/>";
}

Should be:


if($i%3==0) {
print "<br />";
}

= is assignment, and will evaulate to true.
Actually it will give a parse error as you can't assign anything to an int (modulo always returns an int).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum