Page 2 of 2
Posted: Tue Oct 04, 2005 7:58 pm
by mickd
read the second last post, the one feyd made, looks to me that it would be very helpful...
by the looks of it, it gets the info from the database and displays it in rows like you needed. alittle bit of modification and it could be suited to your cause...
Posted: Tue Oct 04, 2005 8:02 pm
by josh
I'd like to point out you could have used the modulus, that way you dont have to reset the [$rownum] variable every iteration, and you can have a variable representing the total amount of rows looped.
Also a couple mistakes you made:
'1' on line three should really probably be an integer instead of a string
mickd wrote:something like this should do it (prolly needs tweaking) and your elseif:
, should be $rownum
== 2
Other than that it should do exactly what the OP was asking.
Code: Select all
echo '<table>';
$rownum = 1;
while($row = mysql_fetch_assoc($result)) {
if($rownum == '1') {
echo '<tr>
<td>' . $row['name'] . '
</td>';
$rownum++;
} elseif($rownum = 2) {
echo '
<td>' . $row['name'] . '
</td>
</tr>';
$rownum = 1;
}
}
if((mysql_num_rows($result) % 2) == 1) {
echo '</tr>';
}
echo '</table>';
Posted: Tue Oct 04, 2005 8:08 pm
by mickd
yeah i was going to but it lead to some confusion while i was writing it so i just left it out
because of that i couldnt think of a way to tell the difference between row 1 and row 2
o wait, if i used 3 % 1 and 3 % 2 instead
} elseif($rownum = 2) {
, should be $rownum == 2
oops
'1' on line three should really probably be an integer instead of a string
oops, just too used to writing '' even if its bad practise :S
Posted: Tue Oct 04, 2005 8:30 pm
by josh
should do the trick.
mickd wrote:yeah i was going to but it lead to some confusion while i was writing it so i just left it out
Posted: Tue Oct 04, 2005 8:33 pm
by mickd
oh, also when you posted that, it just came to me that thats what feyd did in that post too
good idea, never thought of it that way.
Problem Solved
Posted: Wed Oct 05, 2005 6:33 pm
by icesolid
Well, thanks for all of the input, I guess the best way to learn things around here is to get all the programmers on this forum to start brainstorming on your topic. I now have my issue solved thanks to DevNetwork!
Here is my working code I put together from all of your brainstorming:
Code: Select all
<?php
$rownum = "1";
$color1 = "#F5F5F5";
$color2 = "#E9E9E9";
$rowcount = "0";
$result = mysql_query("SELECT * FROM specials ORDER BY sortorder ASC");
while($row = mysql_fetch_assoc($result)) {
$rowcolor = ($rowcount % "2") ? $color1 : $color2;
if($rownum == "1") {
?>
<tr>
<td bgcolor="<?php echo $rowcolor; ?>" align="center" valign="top" width="188">
<font face="Verdana" size="2"><b><?php echo $row["item"]; ?>:</b></font>
<br><br>
<a href="specials/<?php echo $row["file"]; ?>" target="_blank"><img src="thumbnail.php?dir=specials&file=<?php echo $row["file"]; ?>" alt="<?php echo $row["item"]; ?>" border="1"></a><br>
<font face="Verdana" size="1">(<a href="specials/<?php echo $row["file"]; ?>" target="_blank">Enlarge Photo</a>)</font>
<br><br>
<font face="Verdana" size="2" color="#FF0000"><b>Price:</b> $<?php echo $row["price"]; ?></font>
</td>
<?php
$rownum++;
} elseif($rownum == "2") {
?>
<td bgcolor="<?php echo $rowcolor; ?>" align="center" valign="top" width="187">
<font face="Verdana" size="2"><b><?php echo $row["item"]; ?>:</b></font>
<br><br>
<a href="specials/<?php echo $row["file"]; ?>" target="_blank"><img src="thumbnail.php?dir=specials&file=<?php echo $row["file"]; ?>" alt="<?php echo $row["item"]; ?>" border="1"></a><br>
<font face="Verdana" size="1">(<a href="specials/<?php echo $row["file"]; ?>" target="_blank">Enlarge Photo</a>)</font>
<br><br>
<font face="Verdana" size="2" color="#FF0000"><b>Price:</b> $<?php echo $row["price"]; ?></font>
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<?php
$rownum = "1";
$rowcount++;
}
}
if((mysql_num_rows($result) % "2") == "1") {
echo "</tr>\n";
}
?>