While Loop With HTML Table

Discussion in 'PHP' started by MajHate, Mar 18, 2009.

  1. #1
    Hello Everyone,

    I am trying to retrieve names from my database. I am using a While Loop, however I do not want to display the names in one long list. I want to break them up into 3 columns and use a table, so it should be broken up into 3 <td>'s. I have this script but I don't know how to integrate an html table with it. Does anyone have any ideas?

    Thank you,
    Jason

    <?php
    $something = mysql_query("SELECT * FROM somewhere");
    $column_count = 3;
    $i = 1;
    while ($row = mysql_fetch_array($something)) {
       
            if (is_int($i/$column_count)) {
                echo $row['your_value'], "<br>";
            } else {
                echo $row['your_value'], " - "; // remove - symbol if you don't want to split columns
            }
    
                    $i++;
       
    }
    ?>
    PHP:
     
    MajHate, Mar 18, 2009 IP
  2. abmathur

    abmathur Member

    Messages:
    211
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #2
    Echo '<table><tr>' before starting your loop; echo '<td>your return query</td>' within loop and '</tr></table>' after loop.
     
    abmathur, Mar 18, 2009 IP
  3. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Well I know how to make a table, but this script is confusing and I dont know exactly where to do the <td>
     
    MajHate, Mar 18, 2009 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    From that fragment I can't tell either but it is one of the two lines:
    Change one of those to:

     
    Colbyt, Mar 18, 2009 IP
  5. djdeth

    djdeth Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It is a little more work than that because you want to make sure that if the last row isn't complete, you do a colspan.

    <?php
    $something = mysql_query("SELECT * FROM somewhere");
    $column_count = 0;
    $i = 1;
    echo'<table><tr>';
    while ($row = mysql_fetch_array($something)) {
       
            if ($column_count ==3) {
            
            echo'</tr><tr><td>';
                echo $row['your_value'];
                echo'</td>';
                $column_count=0;
            } else {
             echo'<td>';
                echo $row['your_value']; // remove - symbol if you don't want to split columns
                
                 echo'</td>';
                
                
            }
    
                    $i++;
       
    }
    
    
    
    if($column_count > 0){
    
    $colspan=(3-$column_count);
    echo'<td colspan="'.$colspan.'">&nbsp;</td>';
    
    
    }	
    echo'</tr></table>';
    
    
    
    
    ?>
    PHP:
     
    djdeth, Mar 18, 2009 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Try this:

    <?php
    $something = mysql_query("SELECT * FROM somewhere");
    $column_count = 3;
    $i = 1;
    
    echo '<table>';
    
    while ($row = mysql_fetch_array($something)) {
    if ($i == 1) { echo '<tr>'; }
    
    echo "<td>".$row['your_value']."</td>";
    
    $i++;
    if ($i > $column_count) { $i=1; echo '</tr>'; }
    }
    
    echo '</table>';
    ?>
    PHP:
    - ads2help
     
    ads2help, Mar 18, 2009 IP
  7. djdeth

    djdeth Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It isn't NEEDED, but you should add a check at the end to do a colspan if needed to make your markup valid.
     
    djdeth, Mar 18, 2009 IP
  8. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hello,


    Well as you want to show 3 records per column. You can do like this


    
    <table width="120" border="0" cellpadding="5" cellspacing="0">
            <?php 
    $selectQuery = "Your Sql Statement'";
    $resultQuery= query($selectQuery );
    $numRows = numRows($resultQuery);
    if($numRows =="")
     {
     ?>
            <tr>
              <td class="txts" style="padding:5px;"><?php echo ("Record not available");?></td>
            </tr>
            <tr>
              <?php 
      }
      else
      {
     $count=0;
     while($rowQuery=mysql_fetch_array($resultQuery)) 
       { 
          if($count % 3==0)
          echo "</tr><tr>";
        ?>
              <td style="padding-bottom:10px;padding-top:10px;" valign="top" class="tabs"><table width="100" border="0" cellspacing="0" cellpadding="0" style="border:1px #cccccc solid';">
                  <tr>
                    <td width="100" align="center"><table width="110" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                          <td height="30" align="center "><?php echo $rowQuery['recordName']?>                  </td>
                        </tr>
                                        </table></td>
                  </tr>
              </table></td>
              <?php 
    	$count++;
    	}
    }
    	?>
            </tr>
          </table>
    
    PHP:
    We hope it works for you

    Regards,
    Stylesofts Developing Team
     
    Stylesofts, Mar 19, 2009 IP