Page 1 of 1

Result Table w/ single static cell

Posted: Tue Apr 24, 2012 12:21 pm
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!

Re: Result Table w/ single static cell

Posted: Tue Apr 24, 2012 1:59 pm
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>";

Re: Result Table w/ single static cell

Posted: Tue Apr 24, 2012 3:13 pm
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?

Re: Result Table w/ single static cell

Posted: Tue Apr 24, 2012 3:21 pm
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>"; }

Re: Result Table w/ single static cell

Posted: Tue Apr 24, 2012 3:39 pm
by dsol828
nailed it! thanks Celauran!