Making Nice Tables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post 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...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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:

Code: Select all

} elseif($rownum = 2) {
, 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>';
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

yeah i was going to but it lead to some confusion while i was writing it so i just left it out ;)

Code: Select all

2%1 == 0
2%2 == 0
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 :o
} 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

if ( $rownum % 2 == 0 )
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 ;)

Code: Select all

2%1 == 0
2%2 == 0
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

oh, also when you posted that, it just came to me that thats what feyd did in that post too :oops:

good idea, never thought of it that way.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Problem Solved

Post 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">&nbsp;</td>
</tr>
<?php
$rownum = "1";
$rowcount++;
}
}

if((mysql_num_rows($result) % "2") == "1") {
echo "</tr>\n";
}
?>
Post Reply