Jump to content

HTML Table populated from MySQL horizontally


thecase

Recommended Posts

Hi,

 

I'm trying to populate an HTML table horizontally, so each new record on the table is inserted in the next cell until it reaches the fouth one, then an new row is added and the cycle repeats hopefully the code I have started will explain better

 

      echo " 
  <table width='70%' cellspacing='0' cellpadding='0' border='1' align='center'>

<tr>
<td align='center' width='18%'><b><p>Date / Time</p></b></td>
<td align='center' width='25%'><b><p>Presenter 1</p></b></td>
<td align='center' width='25%'><b><p>Presenter 2</p></b></td>
<td align='center' width='25%'><b><p>Engineer</p></b></td>

</tr>    

   ";

    
  $colors = array("#df8700", "#006700", "#cf0000");
  
  $count = 0;
  
  while ($row = mysql_fetch_assoc($query))
      {
      $bg = ($bg == '#FFD5A8' ? '#BBBBBB' : '#FFD5A8');    
      
      if ($count == 0){
        echo '<tr>';
      }
      
      echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><p class='table'>{$row['date']}</p></td>";
      
          if ($row['userid'] == '31'){
          echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors['1']}\"><p class='table'>None</p> </font></td>";
          } else {
          echo " <td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors[$row['status']]}\"><p class='table'>{$row['user']}</p> </font></td>";
          }
          
          if ($count + 1 == 4){
          echo "</tr> ";  
          }else{
          ++$count;
          }
          
          }
             
          
          
      echo "</table><BR>";  
      unset($colors, $row);
}   

 

Although this does not work. It adds extra cells and doesn't complete all before starting the next, can anyone see the problem.

 

 

Thanks

Allow me to re-phrase.. You're only adding the "<tr>" on the first row, and "</tr>" on the fourth row. What you're effectively doing is creating a new row in the HTML table after the fourth row in the database.. Which is why you see the broken structure shown in that image.

I think I get it now, so I set the $count back to 0 when the $count reaches 4. It looks fine in the view source but now the date has got stuck in the loop and inserts itself between every cell instead of just the first cell on each row.

 

Version 2

 

      echo " 
  <table width='70%' cellspacing='0' cellpadding='0' border='1' align='center'>

<tr>
<td align='center' width='25%'><b><p>Date</p></b></td>
<td align='center' width='25%'><b><p>Presenter 1</p></b></td>
<td align='center' width='25%'><b><p>Presenter 2</p></b></td>
<td align='center' width='25%'><b><p>Engineer</p></b></td>

</tr>    

   ";

    
  $colors = array("#df8700", "#006700", "#cf0000");
  
  $count = 0;
  
  while ($row = mysql_fetch_assoc($query))
      {
      $bg = ($bg == '#FFD5A8' ? '#BBBBBB' : '#FFD5A8');    
      

      
      if ($count == 0){
        echo '<tr>';
      }
      
      echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><p class='table'>{$row['date']}</p> </td>";
      
          if ($row['userid'] == '31'){
          echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors['1']}\"><p class='table'>None</p> </font></td>";
          } else {
          echo " <td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors[$row['status']]}\"><p class='table'>{$row['user']}</p> </font></td>";
          }
          
          if ($count + 1 == 4){
          $count = '0';
          echo "</tr> ";  
          }else{
          ++$count;
          }
          
          }
             
          
          
      echo "</table><BR>";  

 

Thanks

 

[attachment deleted by admin]

I have fixed this but I have added another cell to display the comments of each record from the table. The problem is it only displays the every third record in the database three times, how do I get it to get the correct comment. Hope this makes sence.

 


      echo " 
  <table width='70%' cellspacing='0' cellpadding='0' border='1' align='center'>

<tr>
<td align='center' width='25%'><b><p>Date</p></b></td>
<td align='center' width='25%'><b><p>Presenter 1</p></b></td>
<td align='center' width='25%'><b><p>Presenter 2</p></b></td>
<td align='center' width='25%'><b><p>Engineer</p></b></td>

</tr>    

   ";
      
      
      $colors = array("#df8700", "#006700", "#cf0000");
      
      $count = 0;
      
      while ($row = mysql_fetch_assoc($query))
          {
          if ($count == 0)
              {
              echo '<tr>';
              $bg = ($bg == '#FFD5A8' ? '#BBBBBB' : '#FFD5A8');
              echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><p class='table'>{$row['show']}</p> </td>";
              }
          
          
          
          if ($row['userid'] == '31')
              {
              echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors['1']}\"><p class='table'>None</p> </font></td>";
              }
          else
              {
              echo " <td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors[$row['status']]}\"><p class='table'>{$row['user']} </font></p> </td>";
              }
          
          if ($count + 1 == 3)
              {
             
              
              echo "</tr> 
              <tr>
              <td></td>
              <td bgcolor=\"$bg\" align=\"center\" width='25%'><p class='table'>{$row['comment']}</p></td>
              <td bgcolor=\"$bg\" align=\"center\" width='25%'><p class='table'>{$row['comment']}</p></td>
              <td bgcolor=\"$bg\" align=\"center\" width='25%'><p class='table'>{$row['comment']}</p></td> 
              </tr>
              ";
              
               $count = '0';
              
              }
          else
              {
              ++$count;
              }
          }

      
      echo "</table><BR>";

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.