Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Optimizing PHP

  1. #1
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Optimizing PHP

    There has got to be a better way to do this than the way I bushed together without total knowledge of what I am doing. So I attached the actual file so you can see all the 33-ish k of data on 1 page just in text alone . What can I do to call the same info in less data usage? The main thing that is taking up room is this repeated section:

    PHP Code:
    <h5><a name="west">West Plains</a></h5>
    <?php echo $preamble;
    $table_row NULL;
    while (
    $row mysql_fetch_assoc($resultwest)) { 
         
    $table_row .= '<tbody class="' $color_switch '">' "\n"
         
    $table_row .= '<tr>' "\n"
         
    $table_row .= '<td rowspan="15" width="155"><a href="' $ROOT '/' $row['HREF'] . '"><img src="' $ROOT '/images/' $row['Picture'] . '" class="listingpic"/></a></td>' "\n"
         
    $table_row .= '<td rowspan="6"><dl><dt><a href="' $ROOT '/' $row['HREF'] . '" title="View complete information">' $row['Building Name'] . '</a></dt><dd><address>' $row['Building Address 1'] . '<br>' $row['Building Address 2'] . '</address></dd></dl></td>' "\n"
         
    $table_row .= '<td>' $row['Type A'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Unit A'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft A'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate A'] . '&nbsp;</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type B'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Unit B'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft B'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate B'] . '&nbsp;</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type C'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Unit C'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft C'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate C'] . '&nbsp;</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type D'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Unit D'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft D'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate D'] . '&nbsp;</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type E'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Unit E'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft E'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate E'] . '&nbsp;</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type F'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Unit F'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft F'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate F'] . '&nbsp;</td>' "\n"
         
    $table_row .= '</tr>' "\n";
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td rowspan="15"><a href="' $ROOT '/' $row['HREF'] . '">View Complete Information</a></td>' "\n";       
         
    $table_row .= '<td>' $row['Type G'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Unit G'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft G'] . '&nbsp;</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate G'] . '&nbsp;</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type H'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Unit H'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft H'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate H'] . '</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type I'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Unit I'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft I'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate I'] . '</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type J'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Unit J'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft J'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate J'] . '</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '<tr>' "\n";
         
    $table_row .= '<td>' $row['Type K'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Unit K'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Sq Ft K'] . '</td>' "\n"
         
    $table_row .= '<td>' $row['Annual Rate K'] . '</td>' "\n"
         
    $table_row .= '</tr>' "\n"
         
    $table_row .= '</tbody>' "\n";      
        
         if (
    $color_switch == 'first') {
              
    $color_switch 'second';
         } else {
              
    $color_switch 'first';
              }
        } 

    echo 
    $table_row;
    echo 
    '</table>' "\n";  
    ?>
    See? Thats a ton of info to repeat several times, and I need a better answer before this gets out of control. Anyone have any ideas? Oh you can see the output at this page.
    Last edited by Exis; 01-09-2006 at 09:45 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    $letters=array('A'.'B'.'C'.'D'); //etc (could probably do with chr() but nm...
    $bits=array('Type'.'Unit','Sq Ft','ANnal Rate');
    $valleys=array(''cbd;=>'CBD & Periphery','west'=>'West Plains'//etc.
    foreach($valleys as $key=> $cur_valley) {
    $string='result'.$key;
    $cur_result=$$string;
    while (
    $row mysql_fetch_assoc($cur_result)) {
    $row.='<tr>';

    $heading='<h2>'.$cur_valley.'</h2>';
    foreach(
    $letters as $cur_letter) {
    foreach(
    $bits as $cur_type) {
      
    $row.='<td>'.$row[$cur_type.' '.$cur_letter].'</td>';
    }
    }
    $row.='</tr>';
    }
    echo 
    $heading.$row;

    Should put you in the right direction (you'll need to adjust the formatting, but it should replace most of your code...)
    [edit]Didn't realise how much was missing in the posted code...
    Last edited by GJay; 01-03-2006 at 11:54 PM.

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    One of the ways is to start using OO (Object Oriented) programming.
    PEAR, which comes with php by default, has very nice modules that can be installed on the server and help you create better, faster code.
    One of the modules is DB_DataObject

  • #4
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay
    PHP Code:
    $bits=array('Type'.'Unit','Sq Ft','ANnal Rate'); 
    Should put you in the right direction (you'll need to adjust the formatting, but it should replace most of your code...)
    [edit]Didn't realise how much was missing in the posted code...
    Thanks for the help so far you two. I was unaware that properties needed an ANnal Rate, but I will look into that and see if it needs to be on our site . More seriously, I am very unfamiliar with arrays as I am just starting PHP & MySQL, and my only prior experience has been with html, css, and altering things that I can make out in Javascript...which isn't much. I really want to start learning about arrays and I will be doing so this week. This example has given me something good to start on. Thanks!

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    GJay is there a particular reason you use "." in the array definitions?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Fat fingers?
    (they should indeed be commas...)

  • #7
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is what I have so far:

    PHP Code:
    $letters=array('A','B','C','D','E','F','G','H','I','J','K'); //etc (could probably do with chr() but nm...
    $bits=array('Type','Unit','Sq Ft','Annual Rate');
    $areas=array('cbd'=>'CBD & Periphery','west'=>'West Plains'); //etc.
    foreach($areas as $key=> $cur_area) {
    $string='result'.$key;
    $cur_result=$$string;
    while (
    $row mysql_fetch_assoc($cur_result)) {
    $row.='<tr>';

    $heading='<h2>'.$cur_area.'</h2>';
    foreach(
    $letters as $cur_letter) {
    foreach(
    $bits as $cur_type) {
      
    $row.='<td>'.$row[$cur_type.' '.$cur_letter].'</td>';
    }
    }
    $row.='</tr>';
    }
    echo 
    $heading.$row;

    So I changed valleys to areas...no big deal there, just personal pref. Added the rest of the arrays, and replaced all the "."s with ","s. I am getting an error though and maybe you could help me out with it.


    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/webprop/public_html/testsql.php on line 46

    Line 46 for me would be the while arguement. What is wrong with it? Also on the following line:
    PHP Code:
    $cur_result=$$string
    Do I need the double "$$"? Or should it be just 1 $? Any other things that you think I should change real fast? Thanks again for the help.

    EDIT: Oh yeah, also fixed a quote error on the areas array line.

  • #8
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    The $$ is intentional, it will use the variable with the value $string, so $resultcbd, $resultwest etc.- are these still defined at the top of the script?

  • #9
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay
    The $$ is intentional, it will use the variable with the value $string, so $resultcbd, $resultwest etc.- are these still defined at the top of the script?
    Oh I see. Ok so I reset the defined result queries at the top, and now the headings are displayed correctly and there are no for accessing the database. While the headings do display, the rest of the content that is supposed to be pulled does not show up. Here is what I have now:

    PHP Code:
    $letters=array('A','B','C','D','E','F','G','H','I','J','K'); //etc (could probably do with chr() but nm...
    $bits=array('Type','Unit','Sq Ft','Annual Rate');
    $areas=array('cbd'=>'CBD & Periphery','north'=>'North Side','valley'=>'Spokane Valley','shill'=>'South Hill','west'=>'West Plains','outside'=>'Outside Spokane'); //etc.
    foreach($areas as $key=> $cur_area) {
    $string='result'.$key;
    $cur_result=$$string;
    while (
    $row mysql_fetch_assoc($cur_result)) {
    $row.='<tr>';

    $heading='<h2 class="location">'.$cur_area.'</h2>';
    foreach(
    $letters as $cur_letter) {
    foreach(
    $bits as $cur_type) {
      
    $row.='<td>'.$row[$cur_type.' '.$cur_letter].'</td>';
    }
    }
    $row.='</tr>';
    }
    echo 
    $heading.$row;


  • #10
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    I stupidly used $row to refer to both the database-row and the table-row, change the references to one of them to a new variable name and that should at least move on to a new problem... (ever the optimist...)

  • #11
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay
    ...that should at least move on to a new problem... (ever the optimist...)
    Ha, I hear you on that one. Well good news is that I did what you said and it worked. All my data is coming in and seems to flow onto one line fairly nicely and in the right categories, I guess. Now I will start the formatting, and hopefully I don't mess that up too much.

    Thanks again for all your help. Today I got a basic grasp on what an array is, how to define it, how to write IF conditional statements, and a basic grasp on the FOREACH conditional. Thanks again for all that help. It really helps me learn this faster with some hands on examples like this.

  • #12
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Waaah. I am so close to getting it. I just cannot figure out what the problem is.

    Page I am working on.

    What it is supposed to look like.

    Here is the PHP I have so far for the one I am working on:
    PHP Code:
    <?php
    $letters
    =array('A','B','C','D','E','F','G','H','I','J','K');
    $bits=array('Type','Unit','Sq Ft','Annual Rate');
    $areas=array('cbd'=>'CBD & Periphery','north'=>'North Side','valley'=>'Spokane Valley','shill'=>'South Hill','west'=>'West Plains','outside'=>'Outside Spokane'); //etc.
    foreach($areas as $key=> $cur_area) {
    $string='result'.$key;
    $cur_result=$$string;
    while (
    $row mysql_fetch_assoc($cur_result)) {
    $heading='<h5><a name="' .$cur_area.'">' .$cur_area.'</a></h5>';
        
    $table_row .= '<tbody class="' $color_switch '">' "\n"
        
    $table_row .= '<tr>' "\n"
        
    $table_row .= '<td rowspan="15" width="155"><a href="' $ROOT '/' $row['HREF'] . '"><img src="' $ROOT '/images/' $row['Picture'] . '" class="listingpic"/></a></td>' "\n"
        
    $table_row .= '<td rowspan="15"><dl><dt><a href="' $ROOT '/' $row['HREF'] . '" title="View complete information">' $row['Building Name'] . '</a></dt><dd><address>' $row['Building Address 1'] . '<br>' $row['Building Address 2'] . '</address></dd></dl></td>' "\n"
    foreach(
    $letters as $cur_letter) {
        
    $table_row .=  $var_row;
        if ( 
    $cur_letter == 'A' ) {
        
    $var_row "";
    } else {
        
    $var_row '<tr>' "\n";
    }
        foreach(
    $bits as $cur_type) {
        
    $table_row.='<td>'.$row[$cur_type.' '.$cur_letter].'&nbsp;</td>';
    }
        
    $table_row .= '</tr>' "\n";
    }
        
    $table_row .= '</tbody>' "\n";
         if (
    $color_switch == 'first') {
              
    $color_switch 'second';
         } else {
              
    $color_switch 'first';
              }
    }
    echo 
    $heading "\n";  
    echo 
    $preamble "\n";  
    echo 
    $table_row "\n";  
    }  
    ?>
    Oh and this is at the top of my page, so you can see how each area is called.
    PHP Code:
     $dbh=mysql_connect ("localhost""webprop_admin""hummerh2") or die ('I cannot connect to the database because: ' mysql_error());
     
    mysql_select_db ("webprop_test1") or die(mysql_error());

    $resultcbd mysql_query("SELECT * FROM `Building Information` WHERE (Location = 'CBD & Periphery') ORDER BY `Building Name` ASC") or die(mysql_error());
    $resultnorth mysql_query("SELECT * FROM `Building Information` WHERE (Location = 'North Side') ORDER BY `Building Name` ASC") or die(mysql_error());
    $resultvalley mysql_query("SELECT * FROM `Building Information` WHERE (Location = 'Spokane Valley') ORDER BY `Building Name` ASC") or die(mysql_error());
    $resultshill mysql_query("SELECT * FROM `Building Information` WHERE (Location = 'South Hill') ORDER BY `Building Name` ASC") or die(mysql_error());
    $resultwest mysql_query("SELECT * FROM `Building Information` WHERE (Location = 'West Plains') ORDER BY `Building Name` ASC") or die(mysql_error());
    $resultoutside mysql_query("SELECT * FROM `Building Information` WHERE (Location = 'Outside Spokane') ORDER BY `Building Name` ASC") or die(mysql_error());

    $color_switch 'first'
    Can someone please help me find out what is going wrong? I know my info is getting called from the database, but everything seems to be in the wrong order when it is displayed. Notice the area CBD & Periphery has nothing below it. Also notice, along with several other errors, that the "220 Building" starts off ever area! Wtf...I mean it looks like it is randomly calling in properties and I don't know why! This is probably too long of a post, so I really hope someone responds, but I am pretty sure most of the people who even check this post don't read down to here.
    Last edited by Exis; 01-06-2006 at 06:39 PM.

  • #13
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok got almost all my formatting down...edit last posts php to reflect that. Still need help with the connection info. Please read last post for the info if you want to help .

  • #14
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    if you run the queries from a commandline or phpymyadmin (if you have access to one...) do they give the results you would expect?

  • #15
    Regular Coder Exis's Avatar
    Join Date
    Nov 2003
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I double checked all my queries at the top, so those are all working in phpmyadmin, so I at least that went right. I have no idea what could possibly be going on. Ussually I can see if I missed a comma or something like that, but it looks to me like all the changes I have made to the php have been made correctly. What else should I check?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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