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
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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.