Need help simplifying my php table
I'm relatively new to php and feel like I'm going to go a long way when displaying data from mysql.
I have table a I want to show several fields from my database.
How could I achieve this without repeating every bit of the table?
Here is the code:
<?php
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");
while ($row = mysql_fetch_array($query1))
{
echo ' <table id="account_table" style="width:550px; border:none; ">
<tr>
<td width="155">Contact Name</td>';
echo '<td width="335">';
echo $row['firstname'] ;
echo ' ';
echo $row['lastname'];
echo '</td>
</tr>
<tr>
<td>Email Address</td>
<td>';
echo $row['email'];
echo ' </td>
</tr>
<tr>
<td>Username</td>
<td>' ;
echo $row['user'];
echo '</td>
</tr>
<tr>
<td>Country</td>
<td>';
echo $row['country'];
echo '</td>
</tr>
<tr>
<td>Time Zone</td>
<td>GMT+1</td>
</tr>
<tr>
<td>Activated</td>
<td>16 Dec 2009</td>
</tr>
</table>';
}
?>
a source to share
You can first get all the data in an array and then loop over that array. You don't need to send all this HTML using PHP.
At the top of the file, you have to do all the processing (i.e. fetching, checking the data) and the rest . write normal HTML , only print values with PHP.
This already gives you some degree of separation. Others mention templating engines (Smarty, etc.). I don't think you really need this because PHP itself is a templating engine.
Just don't be tempted to do complicated things in your presentation;)
In addition, the alternate syntax of the alternate syntax for control structures is very useful to use in conjunction with presentation because it is more readable.
I changed the table structure a bit because you didn't generate the correct HTML (you create many tables with the same ID in your original code).
It just generates one table with a row for each customer.
<?php
$customers = array();
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");
while ($row = mysql_fetch_array($query1)) {
$cusomters[] = $row;
}
?>
<table id="account_table" style="width:550px; border:none;">
<tr>
<th width="155">Contact Name</th>
<th>Email Address</th>
<th>Username</th>
<th>Country</th>
<th>Time Zone</th>
<th>Activated</th>
</tr>
<?php foreach($customers as $customer): ?>
<tr>
<td width="335">
<?php echo $row['firstname'] ?>
<?php echo $row['lastname'] ?>
</td>
<td><?php echo $row['email'] ?> </td>
<td><?php echo $row['user'] ?></td>
<td><?php echo $row['country'] ?></td>
<td>GMT+1</td>
<td>16 Dec 2009</td>
</tr>
<?php endforeach; ?>
</table>
a source to share
The basics seem to be right, although you will need to move the tag <table>
out of the while loop, you are now creating a table for each record, and I assume this is not what you want.
You also need to prepare your output for browser output with htmlspecialchars($row[...])
. This way you avoid potential problems if the output contains html tags (javascript, etc.).
a source to share
Some tips first, keep the window / tab open in the php manual. (i.e. the mysql_send () function doesn't exist).
Depart your code and try to find an agreed upon convention to read your code.
You are inserting your table markup into your loop while
, which is wrong. Your table should wrap your loop.
Using $ _COOKIE, namely to select from your database, is valid . Filter and sanitize your entrance before using it in any request.
To avoid mixing data presentation and business logic, you can take a look at the templating engine.
Check out 19 promising PHP templates to find one or take a look at the wikipedia list .
a source to share