SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Loops

  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loops

    I am trying to create a php table which will display the results (in 2 columns) of the name and birthday of a user.

    The following code I have will only list one of the entries in my database. How do I get it to list each entry from the database in seperate rows.

    Code:
    Code:
    <body>
    <?php 
    include 'inc/db.inc.php'?>
    <?php 
    $result = @mysql_query('Select firstname, surname, date_format(`birthday`, "%d %M")as datestring FROM birthdaydb');
    if (!$result) {
    	exit ('<p>Error Performing query: ' . mysql_error() . '</p>');
    	}
    while ($row = mysql_fetch_array($result)) {
    	$firstname = $row['firstname'];
    	$surname = $row['surname'];
    	$birthday = $row['datestring'];
    /*echo "<p>$firstname " . "$surname " . "$birthday</p>";  */	
        }
    	?>
    <table width="90%" border="0">
      <tr>
      	<td width="50%">
    <?php 
    	echo "$firstname " . "$surname "
    ?>
    	</td>
      	<td width="50%">
    <?php 
    	echo "$birthday"
    ?>
    	</td>
      </tr>
    </table>
    
    </body>
    Any help as always is greatly appreciated.

    JayM

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Add the table output to the loop not outside of it:
    Code php:
    <body>
    <table width="90%" border="0">
    <?php 
    include 'inc/db.inc.php'?>
    <?php 
    $result = @mysql_query('Select firstname, surname, date_format(`birthday`, "%d %M")as datestring FROM birthdaydb');
    if (!$result) {
    	exit ('<p>Error Performing query: ' . mysql_error() . '</p>');
    	}
    while ($row = mysql_fetch_array($result)) {
    	$firstname = $row['firstname'];
    	$surname = $row['surname'];
    	$birthday = $row['datestring'];
    /*echo "<p>$firstname " . "$surname " . "$birthday</p>";  */	
    	?>
      <tr>
      	<td width="50%">
    <?php 
    	echo "$firstname " . "$surname "
    ?>
    	</td>
      	<td width="50%">
    <?php 
    	echo "$birthday"
    ?>
    	</td>
      </tr>
    <?php } ?>
    </table>
     
    </body>
    Saul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •