PHP MySQL Table: Filling the Blanks

Hi All,

I am new to php, having to use it for a school project in which I am creating a database using MAMP. The databases fields (the columns) are ID(Primary Key, and not in the database itself), Name, Email and Phone, and for some entries there is no data. Where should I put the   into the code below so that it automatically repeats itself, as I am having trouble doing this myself.

[php]<?php
include ‘includes/connection.php’;
$query = “SELECT * FROM people”;
$result = mysql_query(“SELECT * from people”);
echo “

”;
while($row = mysql_fetch_array($result)) {
echo “”;
}
echo “
Name Email Phone Delete
” . $row[‘Name’] . “ ” . $row[‘Email’] . “ ” . $row[‘Phone’] . “
”;
?>[/php]

I am guessing it would be in the line beginning with echo “

” but no sure where it would be. I want any unfilled datapoints ( as in the data) to have an empty cell. Any help is appreciated!

Thanks,
Alastair Hirsch

What exactly do you mean by ‘so it repeats itself’?

Tip: When learning don’t worry about putting everything on one line. This will help you see visually your code. And learn a good indentation practice.

<?php
  include 'includes/connection.php';
  $query = "SELECT * FROM people";
  $result = mysql_query("SELECT * from people");
  echo "<table border='2'><tr><th>Name</th><th>Email</th><th>Phone</th><th>Delete</th></tr>";
  while($row = mysql_fetch_array($result))
   {
        echo "<tr><td>";
        if(!empty($row['Name']))
          echo $row['Name'];
        else
           echo '&nbsp;';
        echo "</td><td>";
        if(!empty($row['Email']))
          echo $row['Email'];
        else
           echo '&nbsp;';
        echo "</td><td>";
        if(!empty($row['Phone']))
            echo $row['Phone'];
        else
           echo '&nbsp;';
        echo "</td></tr>";
  }
  echo "</table>";
?>

myself I prefer to use a ternary operator to cut down all the separate if…else statements.

    echo "<tr><td>" . 
      (!empty($row['Name'])?$row['Name']:'&nbsp;') . "</td><td>" . 
      (!empty($row['Email'])?$row['Email']:'&nbsp;') . "</td><td>" . 
      (!empty($row['Phone'])$row['Phone']:'&nbsp;') . "</td></tr>";

Thank you! works like a charm.

Sponsor our Newsletter | Privacy Policy | Terms of Service