PHP mysql list table contents.

Discussion in 'PHP' started by encom, May 31, 2009.

  1. #1
    Hi, I know how to list the contents of a mysql table, but could someone help me out with how I would lay it out like this:

    
    <table border="0">
    	<tr>
    		<td>Text 01</td>
    		<td>Text 02</td>
    		<td>Text 03</td>
    		<td>Text 04</td>
    	</tr>
    	<tr>
    		<td>Text 05</td>
    		<td>Text 06</td>
    		<td>Text 07</td>
    		<td>Text 08</td>
    	</tr>
    	<tr>
    		<td>Text 09</td>
    		<td>Text 10</td>
    		<td>Text 11</td>
    		<td>Text 12</td>
    	</tr>
    	<tr>
    		<td>Text 13</td>
    		<td>Text 14</td>
    		<td>Text 15</td>
    		<td>Text 16</td>
    	</tr>
    </table>
    
    Code (markup):
    Cheers.
    Grant
     
    encom, May 31, 2009 IP
  2. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use a counter:

    $count = 1;
    //start the table
    echo '<table border="0"><tr>';
    
    // run through the data
    while ($row=mysql_fetch_array($result)) {
    
    // if this is the fourth bit of data, add a new row
      if($count == 4) {
        echo '<td>' . $row['your_field_name'] . '</td></tr><tr>';
        $count = 0;
        }
    
    // else just add a new cell
      else {
        echo '<td>' . $row['your_field_name'] . '</td>';
        }
      
      $count++;
    }
    
    // after the data has run out, finish the table
    echo '</tr></table>';
    PHP:
     
    JDevereux, May 31, 2009 IP
  3. encom

    encom Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks I will try that out in a sec.

    Could I ask how I would adapt this code to display a set amount of data (for example: 20) and have some kind of next button to go to the next 20.

    Basicly display 20 per page?
     
    encom, May 31, 2009 IP
  4. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Add "LIMIT 0, 20" to the end of your query. Google "PHP Pagination" and you'll find lots of tutorials.
     
    JDevereux, May 31, 2009 IP
  5. encom

    encom Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thankyou so much.
     
    encom, May 31, 2009 IP