Can I further optimize this PHP script to create dynamic Nav from MySQL database?
I am working on a new CMS for use in recurring projects. Basically, this piece of code connects to the server, grabs all the table names and uses them to create simple navigation. It works great for what I need, but I'm just wondering if it's possible that I can optimize this snapshot of code even more and make it even simpler. Maybe create a class that can customize the formatting? Etc. I tried to do this as "bare bones" as possible.
The only thing I would like to explain is that it checks to see if the table name indicates "included", this is the default table that my CMS uses to know what data should be displayed on the front as data.
<?php
echo '<div class="dynamic_nav_head">Navigation</div>';
echo '<div class="dynamic_nav">';
include('data.php');
$tables = mysql_list_tables($database);
while (list($table) = mysql_fetch_row($tables)) {
if($table!='includes'){
echo "<div class='cat'>".ucwords($table)."</div>";
echo "<div class='cat_item'>";
echo "<a href='?page=read§ion=".$table."'>View " . ucwords($table) . "</a>";
echo "</div>";
echo "<div class='cat_item'>";
echo "<a href='?page=add§ion=".$table."'>Add New ". ucwords($table) ."</a>";
echo "</div>";
} // End If not in Includes.
} // End While
echo '</div>';
?>
Any suggestions on how I can make this code even more compact, cleaner, and faster? Thanks in advance!
Edit: MySQL version: 4.1.22
a source to share
I suggest you visit http://refactormycode.com/
echo '<div class="dynamic_nav_head">Navigation</div><div class="dynamic_nav">'; // on less echo
include('data.php');
$tables = mysql_list_tables($database);
while (list($table) = mysql_fetch_row($tables)) {
if($table!='includes'){
$ucTable= ucwords($table); // just one function call
// just one echo;
// you where also using quotes and double quotes backwards
echo '<div class="cat">'.$ucTable.'</div><div class="cat_item"><a href="?page=read§ion='.$table.'">View ' .$ucTable.'</a></div><div class="cat_item"><a href="?page=add§ion='.$table.'">Add New '. $ucTable .'</a></div>';
} // End If not in Includes.
} // End While
echo '</div>';
a source to share
The code is not bad. One of the improvements for readability would also be fetching the array from the database, rather than mysql_fetch_row inside the while construct. It will also allow you to filter out unwanted names before entering the loop. You can also map the ucwords method on an array to infer this from the while loop construct. The point about double quotes is valid, but I would keep separate echo instructions for readability, as it won't make a noticeable difference here.
Hope it helps.
a source to share