SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question slimdown functions or create class?

    I have 3 functions in this file and they have repetitive lines of code and I'm not sure how to go about slimming things down. The file is basically 3x as large as it needs to be in my opinion. Can I create a class with all 3 menu creating functions, while still including the database connecting parts?

    Or should I just trim these areas out?
    //connect to database -> in all three functions
    //select database -> in all three functions
    //close connection -> in all three functions

    Here is one of the functions from the functions.inc file.

    PHP Code:
    function category_menu()
    {
        
    //connect to database 
        
    $db_connect mysql_connect("localhost""root""") or die ("Could not connect");

        
    //select database
        
    $db mysql_select_db('em'$db_connect);
            if (!
    $db) {
                die(
    'Could not select em database: ' mysql_error());
            } else { echo 
    'Database Selected.<br />';
            }
                
        
    // execute the query
        
    $rResult mysql_query("SELECT `category_id`, `category_field` FROM `em_category`");    
        if (!
    is_resource($rResult))
            {
                die(
    'Could not query the em_category table: ' mysql_error());
            } else {echo 
    'Table query sent.<br />';
            }

        
    // echo tags to create menu
        
    echo "<select name = 'category_field' size='5'>";
            while (
    $row mysql_fetch_array($rResult)) 
            {
            echo 
    "<option name = '" $row['category_field'] . "'>"$row['category_field'] . "</option>";
            }
            echo 
    "</select>";

        
    //close connection. required in all functions?
        
    mysql_close($db_connect);


  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well...not sure what the three functions are or there relation to one another is. But yeah I would create a class then pass that database connection/object into the object. Or make it flexible and pass the required data in the specified format.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The functions basically do the same thing, creating menu's. The only real difference is the sql query for getting different categories.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Combine into one function and pass query as an argument.
    www.forkaya.com - Web Development, PHP Scripting

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    St. Louis, MO.
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anytime I have to repeat the same code more than twice I know I'm doing something wrong that I will regret later. Make it a class that connects to the db in the constructor with one function (method) to print out your options. You will end up adding more and more methods.

    A little trick that I learned the hard way is only use php to print out the <option> tags. Hard code in the <select name=''>. There are exceptions to that of course.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •