...

View Full Version : Optimizing PHP



Exis
01-03-2006, 10:42 PM
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 :eek:. 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:


<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 (http://www.webpropertiesinc.com/testsql.php).

GJay
01-03-2006, 10:55 PM
$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...

Prikid
01-03-2006, 11:39 PM
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 (http://pear.php.net/manual/en/package.database.db-dataobject.php)

Exis
01-04-2006, 12:09 AM
$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 :D. 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!

marek_mar
01-04-2006, 10:13 AM
GJay is there a particular reason you use "." in the array definitions?

GJay
01-04-2006, 06:37 PM
Fat fingers? :)
(they should indeed be commas...)

Exis
01-05-2006, 04:47 PM
Here is what I have so far:


$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:


$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.

GJay
01-05-2006, 06:35 PM
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?

Exis
01-05-2006, 07:03 PM
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:



$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;
}

GJay
01-05-2006, 07:40 PM
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...)

Exis
01-05-2006, 07:59 PM
...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. :thumbsup: :thumbsup: :thumbsup:

Exis
01-05-2006, 09:34 PM
Waaah. I am so close to getting it. I just cannot figure out what the problem is.

Page I am working on (http://www.webpropertiesinc.com/testsql.php).

What it is supposed to look like (http://www.webpropertiesinc.com/testsqlworks.php).

Here is the PHP I have so far for the one I am working on:

<?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.


$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. :D

Exis
01-06-2006, 06:49 PM
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 :D.

GJay
01-06-2006, 07:26 PM
if you run the queries from a commandline or phpymyadmin (if you have access to one...) do they give the results you would expect?

Exis
01-06-2006, 09:16 PM
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?

GJay
01-06-2006, 11:22 PM
Try taking all but one of the 'area's out of the array (try each individually as well maybe), then add another and see what happens.
It looks like something's not being reset for the next loop. I can't see what, but I find it difficult when I can't play around myself :)

Exis
01-09-2006, 09:20 PM
Ok I have been messing around a ton with the placement of all my additions to your code in different FOREACH level, as to maybe remedy a formatting problem. I have pretty much tried every possible combination, even all the ones I knew would not work (which they didn't), and now I am hardly closer to a solution.

Heres one thing I did figure out, and maybe it will help you to see something in my latest posted code that I cannot. When I knock the $areas at the top down to 1 option, it will display correctly. However, when I put in several options the thing messes up a ton. Is something wrong there?

Exis
01-16-2006, 07:08 PM
Figured I would do a follow up post. I fixed my looping errors when I saw some FOREACH example that included a $table_row = NULL; line after the variable $table_row had been echoed but before the for each loop was closed with a }.
This perfectly fixed my problem, so just to let people know how to close a loop in which a variable in constantly redefined and re-echoed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum