Result Table w/ single static cell

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsol828
Forum Newbie
Posts: 3
Joined: Tue Apr 24, 2012 12:17 pm

Result Table w/ single static cell

Post by dsol828 »

Hello,

I am trying to display the results from a mysql query in a three column table (regular HTML). The catch is...I would like the top right cell (row 1, column3) to be static and display other HTML.
Picture this:
[result1][result2][CONTENT]
[result3][result4][result5]
[result6][result7][result8]
...etc...

Here is the code i have so far, although I can't get it to produce that static cell...
Anyone have any ideas?

Code: Select all

$row = 0;
$column = 0;
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
    if ($column == 0) {echo "<tr>";}
    if (($column == 2) && ($row == 0)){echo "<td>content</td></tr><tr>";}
  echo "<td>".$row['business']."</td>";
  $column++;
  if ($column >= 3) {
    $row++;
    echo "</tr>";
    $column = 0;
  }
}
echo "</table>";
thanks in advance!
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Result Table w/ single static cell

Post by x_mutatis_mutandis_x »

You are using "$row" to fetch the result from the database as well as a counter to keep track of your 3 column table, and gets reset everytime in the while statement by fetching the next row in resultset. Other than that and a minor tweak in your logic, it should be fine (see below).

Code: Select all

 //$row = 0;

//the result set row fetched from the query
 $row = array();

 $column = 0;
 //counter keeps track of 3 column table rows
 $rowCounter = 0;

 echo "<table>";
 while($row = mysql_fetch_assoc($result)) {
     if ($column == 0) {echo "<tr>";}
     //if (($column == 2) && ($row == 0)){echo "<td>content</td></tr><tr>";}
   //echo "<td>".$row['business']."</td>";

   if (($column==2) && ($rowCounter==0)){echo "<td>content</td>";} else {echo "<td>{$row['business']}</td>";}

   $column++;

   if ($column >= 3) {
     //$row++;
     $rowCounter++;
     echo "</tr>";
     $column = 0;
   }
 }
 echo "</table>";
dsol828
Forum Newbie
Posts: 3
Joined: Tue Apr 24, 2012 12:17 pm

Re: Result Table w/ single static cell

Post by dsol828 »

A-HA!
Thanks for that!

Although, now that I have it working, I notice I'm completely losing the third result with the else statement. My result jump..1,2,4, 5, 6,etc
Is there any way to shift the result back, making the third result appear in row 2?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Result Table w/ single static cell

Post by Celauran »

Code: Select all

if (($column == 1) && ($rowCounter == 0)) { echo "<td>{$row['business']}</td><td>content</td>"; $column++; } else { echo "<td>{$row['business']}</td>"; }
dsol828
Forum Newbie
Posts: 3
Joined: Tue Apr 24, 2012 12:17 pm

Re: Result Table w/ single static cell

Post by dsol828 »

nailed it! thanks Celauran!
Post Reply