views:

90

answers:

2

I'm working on a new CMS to use on repeative projects. Basically this chunk of code connects to a server, grabs all the names of the tables and uses them to generate a simple navigation. It works pretty great for what I need however I'm just wondering if maybe I can optimize this code sniplet even more and make it even more simple. Maybe making a class that could customize the formating? Etc. I tried to make this as "bare-bones" as possible.

The only thing that is there that I would like to explain is that it checks to see if the table name isn't "includes" this is a default table my CMS uses to know what data to display on the front end as far 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&section=".$table."'>View " . ucwords($table) . "</a>";
                              echo "</div>";
                   echo "<div class='cat_item'>";
                                echo "<a href='?page=add&section=".$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 leaner, cleaner, and more swift? Thanks in advance!

Edit: MySQL Version: 4.1.22

+1  A: 

How do you know the code is slow? What does your profiler say about the code? Which statement is slowing it down? What platform are you on? What version of mysql? How many tables are in this catalog? Are you suffering from premature optimization?

jmucchiello
I have used this code for awhile now and it works fine for smaller databases with 10-14 tables of data (used normally as sections of a page) but I just did a site with 80 or so tables and it started to lag a bit about half way down the page. Is there a better solution? Would having a script that just loaded all the table names into another separate table be quicker? Would it for small numbers of table sites vs. the code above? Thanks for the quick response!
stogdilla
i'm almost sure this part is fast enough. it sounds more like the browser has problems rendering it: "(...) it started to lag a bit about half way down the page"
Schnalle
A: 

I suggest you to 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&section='.$table.'">View ' .$ucTable.'</a></div><div class="cat_item"><a href="?page=add&section='.$table.'">Add New '. $ucTable .'</a></div>';
                    }  // End If not in Includes.
          } // End While
      echo '</div>';
Luis Melgratti
Actually echo takes multiple parameters so you should change all the .'s in the echo to commas. No reason to concat the string before sending it to the output buffer.
jmucchiello