Jump to content

[SOLVED] Help! functioned table not behaving


Spectre

Recommended Posts

  function tab($result){
    while($tab = mysqli_fetch_assoc($result)) {
         tabrow($tab);
        }
  
  }
  
  function tabrow($row){
    echo '<TR>';
      echo '<td width="15">&nbsp</td>';
       for($i = 1; $i<=4; $i++){
        if($row[image] == "blank"){
         $row[image] = 'image/achievement_locked.png';
        }
        echo '<td><img src="'.$row[image].'" title="'.$row[alt_text].'"></td><td width="10"> </td>';

       }
        echo'<td width="5"> </td>';
        if($i%4==0){
         echo'</tr>';
        }
  }

?>

  <TABLE WIDTH="400" HEIGHT="450" ALIGN="center"
   STYLE="background-image:
     url('image/hutBK.png');">

<?

     tab($result);

?>
     <tr height="30"></tr>
  </table>
<?

 

 

Ok so there's my code snippet, i'm working on no sleep in 3 days (well other than an hour nap I managed) all i'm trying to do is make the table echo out in a 4 x 4 style (4 columns and 4 rows). But what it's doing is, echoing out the first thing 4 times, then new row, then 2nd thing 4 times and so on. I want it to show the first image, then 2nd, 3rd, 4th NEW ROW 5th 6th and so on, up to 16th and table ends... a little help please?

  function tab($result){
    while($tab = mysqli_fetch_assoc($result)) {
         tabrow($tab);
        }
  
  }
  
  function tabrow($row){
    echo '<TR>';
      echo '<td width="15">&nbsp</td>';
       for($i = 1; $i<=4; $i++){
        if($row[image] == "blank"){
         $row[image] = 'image/achievement_locked.png';
        }
        echo '<td><img src="'.$row[image].'" title="'.$row[alt_text].'"></td><td width="10"> </td>';

       }
        echo'<td width="5"> </td>';
        if($i%4==0){
         echo'</tr>';
        }
  }

?>

  <TABLE WIDTH="400" HEIGHT="450" ALIGN="center"
   STYLE="background-image:
     url('image/hutBK.png');">

<?

     tab($result);

?>
     <tr height="30"></tr>
  </table>
<?

 

 

Ok so there's my code snippet, i'm working on no sleep in 3 days (well other than an hour nap I managed) all i'm trying to do is make the table echo out in a 4 x 4 style (4 columns and 4 rows). But what it's doing is, echoing out the first thing 4 times, then new row, then 2nd thing 4 times and so on. I want it to show the first image, then 2nd, 3rd, 4th NEW ROW 5th 6th and so on, up to 16th and table ends... a little help please?

 

Something like the following should work:

 

$count = 0;
$table = "<table><tr>";

while($row = mysqli_fetch_assoc($result))
{
   if(($count % 4) == 0)
   {
      $table .= "</tr><tr>";
   }

   if($row['image'] == 'blank')
   {
      $row['image'] = "image/achievement_locked.png";
   }

   $table .= "<td><img src=\"{$row['image']}\" alt=\"{$row['alt_text']}\" /></td>";
   ++$count;
}

$table .= "</tr></table>";

echo $table;

Used your way, with a few tweaks (as I'm adding in paging to it etc) and works fine ( I knew it'd be simple but my brain was just not seeing it). Thanks for the help lol, much appreciated, and I now know not to code on so little sleep as I keep trying to make something simple, overly complicated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.