Jump to content

Formatting MySQL results in PHP


ellchr3

Recommended Posts

What's the best way to format results from a MySQL table?  I've found very vague examples of PHP code utilizing html tables.  Below is my current PHP.  Thanks!

 

<?php

$con = mysql_connect("localhost","xxxxx","xxxxx") or die('Could not connect: ' . mysql_error());

mysql_select_db("addresses", $con);

 

$result = mysql_query("SELECT * FROM addresses");

 

while($row = mysql_fetch_array($result))

  {

  echo $row['first_name'] . " " . $row['last_name'] . " " . $row['extra_info'] . " " . $row['address'] . " " . $row['city'] . " " . $row['state'] . " " . $row['zip'];

  echo "<br />";

  }

 

mysql_close($con);

?>

Link to comment
https://forums.phpfreaks.com/topic/253240-formatting-mysql-results-in-php/
Share on other sites

It really depends on how you want your data to be viewed. You can have a fancy layout or just a simple table.

 

echo '<table border="0" cellspacing="3" cellpadding="3">';
echo "\n<tr><td>First Name</td>Last Name</td><td>etc...</td></tr>\n";
while($row = mysql_fetch_array($result)){
    echo "<tr><td>".$row['first_name']."</td><td>".$row['last_name']."</td><td>".$row['etc']."</td></tr>\n";
}
echo "</table>\n";

This really isn't a PHP issue. You should first decide what you want your output to look like. THEN you should build the PHP code to generate that output. I would suggest building a basic HTML page with some mock data to put the content into the format you want. You can tweak whatever specifications you want until you are happy with the look.

 

In my opinion: it is much easier to design outside of code and it is much easier to code when you have a set design. Trying to design within code can be done, but it routinely becomes more difficult than it needs to be.

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.