SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru Nick Carlson's Avatar
    Join Date
    Aug 2003
    Location
    Denver
    Posts
    644
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    optimizing code...uhh, i know im writing too much

    I've been working a lot on my directory project, and I won't lie to you, I'm new to php. I've gotten the hang of it, and I'm getting my desired results, the only problem is that evertime I change something(ie, add a new colum to a table in a database) I end up having to go through all my code and changing 200 things.

    I feel like I'm starting from scratch every time.

    I have a nice history of OOP c++ experience, so logic isn't the problem. I feel like mabey Im missing some shortcuts, that everyone else uses.

    Can anybody help me out here? are there any articles on how to avoid writing too much code?
    ncarlson.net - a programmer's dystopia

  2. #2
    SitePoint Guru Nick Carlson's Avatar
    Join Date
    Aug 2003
    Location
    Denver
    Posts
    644
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just wanted to give you an example of what I mean.

    All this does, it take values from a form, and update a table with any new values entered in. The thing is, my scripts for editing, deleting anad adding are almost identical, but I don't know how to write them so that they use the same code.

    I really want to make sure that before I rewrite my code again, that I'm going to do it right this time. Any pointers would be greatly appreciated.

    PHP Code:
    <?
        $connstr 
    "dbname=XX user=XX password=XX";
        
    $dbh pg_connect($connstr);
        if (
    $dbh) {
        } else {
        echo 
    "connection error";
        }

    #table to edit
    $tbl $_POST['table'];

    #for temporary companies
    if ($tbl == 'tmp_comps') {

    $comp_id_h $_POST['comp_id_h'];
    $user_name $_POST['user_name_h'];
    $job_pos $_POST['job_pos_h'];
    $comp_name $_POST['comp_name_h'];
    $comp_tely $_POST['comp_tely_h'];
    $comp_fax $_POST['comp_fax_h'];
    $comp_web $_POST['comp_web_h'];
    $comp_email $_POST['comp_email_h'];
    $comp_street $_POST['comp_street_h'];
    $comp_city $_POST['comp_city_h'];
    $comp_state $_POST['comp_state_h'];
    $comp_zip $_POST['comp_zip_h'];
    $comp_country $_POST['comp_country_h'];
    $comp_desc $_POST['comp_desc_h'];
    $comp_dir $_POST['comp_dir_h'];

                                
    #for all other companies
            
    } elseif ($tbl == 'comps') {

    $comp_id_h $_POST['comp_id'];
    $user_name $_POST['user_name'];
    $job_pos $_POST['job_pos'];
    $comp_name $_POST['comp_name'];
    $comp_tely $_POST['comp_tely'];
    $comp_fax $_POST['comp_fax'];
    $comp_web $_POST['comp_web'];
    $comp_email $_POST['comp_email'];
    $comp_street $_POST['comp_street'];
    $comp_city $_POST['comp_city'];
    $comp_state $_POST['comp_state'];
    $comp_zip $_POST['comp_zip'];
    $comp_country $_POST['comp_country'];
    $comp_desc $_POST['comp_desc'];
    $comp_dir $_POST['comp_dir'];

            }






    # variables used for querying database
    $user_name_h strtolower($user_name);
    $job_pos_h strtolower($job_pos);
    $comp_name_h strtolower($comp_name);
    $comp_tely_h strtolower($comp_tely);
    $comp_fax_h strtolower($comp_fax);
    $comp_web_h strtolower($comp_web);
    $comp_email_h strtolower($comp_email);
    $comp_street_h strtolower($comp_street);
    $comp_city_h strtolower($comp_city);
    $comp_state_h strtolower($comp_state);
    $comp_zip_h strtolower($comp_zip);
    $comp_country_h strtolower($comp_country);
    $comp_desc_h strtolower($comp_desc);
    $comp_dir_h $comp_dir;


            if (
    $tbl == 'tmp_comps') {
            
    $sql "UPDATE tmp_comps SET user_name_h='$user_name_h', job_pos_h='$job_pos_h', comp_name_h='$comp_name_h', comp_tely_h='$comp_tely_h', comp_fax_h='$comp_fax_h', comp_web_h='$comp_web_h', comp_email_h='$comp_email_h', comp_street_h='$comp_street_h', comp_city_h='$comp_city_h', comp_state_h='$comp_state_h', comp_zip_h='$comp_zip_h', comp_country_h='$comp_country_h', comp_desc_h='$comp_desc_h', comp_dir_h=$comp_dir_h WHERE comp_id_h = $comp_id_h";     
            } elseif (
    $tbl == 'comps') {
            
    $sql "UPDATE comps SET user_name='$user_name_h', job_pos='$job_pos_h', comp_name='$comp_name_h', comp_tely='$comp_tely_h', comp_fax='$comp_fax_h', comp_web='$comp_web_h', comp_email='$comp_email_h', comp_street='$comp_street_h', comp_city='$comp_city_h', comp_state='$comp_state_h', comp_zip='$comp_zip_h', comp_country='$comp_country_h', comp_desc='$comp_desc_h', comp_dir=$comp_dir_h WHERE comp_id = $comp_id_h";     
            }




    if (
    $result pg_exec($dbh$sql)) {
            
    echo 
    "It worked!";s

    } else {
        echo 
    "error querying db";
    }
    pg_close($dbh)
    ?>
    Thanks
    ncarlson.net - a programmer's dystopia

  3. #3
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    The only thing I can think is to combine getting the POST value and strtolower. IE

    PHP Code:
    function myfunction ($name){
    $value $_POST['$name'];
    $value strtolower($value);
    return 
    $value;
    }

    $job_pos myfunction('job_pos'); 
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  4. #4
    Can we go to a 48 hour day?
    Join Date
    May 2002
    Location
    MI
    Posts
    906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one of the things I have been trying to do more of lately is use arrays to carry values from forms and such.

    So a foreach in after the POST creates an array $values['fieldname']['value']

    Then I match the fieldname to the object variable name and finally to the database field so that I can loop through everything and create my queries and such all from the values in the array.
    mitechie.com
    "Techies just think a little differently
    ...at least that is what they keep telling me."


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
  •