Slow page load - too many scripts on page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Slow page load - too many scripts on page?

Post by cdickson »

Can a page can hold only so many scripts before it slows the page load?

I am upgrading a client site to be database driven. It is a membership site, so display of the members is broken down alphabetically by the first letter of the company name.

On the first page that we coded we found that it would display properly with 10 of the following scripts (1 for each letter of the alphabet).

Code: Select all

<?php
$query = "SELECT * FROM member WHERE type='2' AND company LIKE 'A%' ORDER BY company ASC";
$r = mysql_query($query);
					  
$tmp_cnt = 0;   // temporary variable
$per_row = 2;
					  
for ($j = 0; $j < mysql_num_rows($r); $j++) {
						
if ($tmp_cnt % $per_row == 0)
echo ('<tr>');				
							$rowarray = mysql_fetch_array($r);
	$field_num = mysql_num_fields($r);
							echo ("<td width="310" bgcolor="#FFFFFF">");		echo ("<table border=0>");
							if (!empty ($rowarray[6])) {
							echo ("<tr><td class="bodytext"><a href="http://{$rowarray[6]}" target="_blank">") . $rowarray[4] . ("</a></td></tr>");
								} else {
							echo ("<tr><td class="bodytext">") . $rowarray[4] . ("</td></tr>");
								}
							echo ("</table>");					echo ("</td>");       

$tmp_cnt = $tmp_cnt + 1;     
if ($tmp_cnt % $per_row == 0)    
echo ('</tr>');                  

}
?>
As soon as I add the 11th script, the page takes an excessive amount of time to load. When it finally does appear, it doesn't display in full and there is a HUGE long white space at the bottom of the page.

Is this typical?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You really shouldn't be adding the same chunk of code into a script 26 times .. it will become a nightmare to maintain, and it's icky ;)

Apart from that, you should probably be freeing the results as they are stored until the script ends.
mysql_free_result($r);
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

I think this seems rather tedious also. Besides, who wants to have icky code? Not me!

It seems equally tedious to put each script into it's own file and include it, but if the page will load faster I would certainly do it.

There is probably some other way to "query" and "echo" that is more concise, but I haven't figured out yet. I am working on it!

Thanks for your help.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Any slowdown you experience from PHP would result entirely from how much work the PHP compiler has to do. Including multiple files will require multiple hard drive reads, as well as meaning it would have to compile each of those files. If you put the logic in one function in one file, it will work faster.

I'd suggest modifying your query to not limit on 'company', but still order by it. Then, in PHP, you can run through the results and output them alphabetically. That will also remove 25 queries which will also speed up the page (although the amount of increased speed will likely not be noticable).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

Thanks for the suggestion. I have already considered this as an option and may end up resorting to this if I can't work out the other details.

The reason for the numerous scripts is to be able to display the results alphabetically, but where each starting letter has it's own section headed by that letter. It is more for the viewer's reading comfort than anything else.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ok, but you can still do it with one query:

Code: Select all

$result = mysql_query($query);
$current_letter = '';
while($row = mysql_fetch_assoc($result))
{
     if(substr($row['company']) != $current_letter)
    {
        $current_letter = $substr($row['company']]);
        print_out_section_head($current_letter);
    }

    //the rest of your code dealing with each entry goes here.
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

You are too kind, Pickle! :D
I will try this.
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

I got this to work but would like to adjust the output slightly.

In addition to alphabetizing the list, I have set the results up into 2 horizaontal columns. Currently the results appear like:
A1 A2
A3 A4
B1 B2
B3
C1 C2
For greater definition and readability I would like the results to appear with an alphabetical heading like this:
A
A1 A2
A3 A4

B
B1 B2
B3

C
C1 C2
I have tried several methods without success. Can you point me in the right direction? Code is:

Code: Select all

<?php
$query = "SELECT * FROM member WHERE type='1' ORDER BY company ASC";
$r = mysql_query($query);
					  
$tmp_cnt = 0;   // temporary variable
$per_row = 2;
					  
for ($j = 0; $j < mysql_num_rows($r); $j++) {
						
if ($tmp_cnt % $per_row == 0)
echo ('<tr>');
					
$rowarray = mysql_fetch_array($r);
$field_num = mysql_num_fields($r);
						
echo ("<td width="310" bgcolor="#FFFFFF">");
echo ("<table border=0>");
if (!empty ($rowarray[6])) {
echo ("<tr><td class="bodytext"><a href="http://{$rowarray[6]}" target="_blank">") . $rowarray[4] . ("</a></td></tr>");
} else {
echo ("<tr><td class="bodytext">") . $rowarray[4] . ("</td></tr>");
}
echo ("</table>");
echo ("</td>");

$tmp_cnt = $tmp_cnt + 1;     
if ($tmp_cnt % $per_row == 0)    
echo ('</tr>');                  

}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

store the last category (A,B,C,etc), start with a blank value, so when you check if the category has changed since last loop, you toss out the category heading.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?

//while loop
{

$currentletter = substr($rowarray[4],0,1);

if ($currentletter != $lastletter)
{

//outputs A, B, C
echo $currentletter;

}

echo $rowarray[4];

$lastletter = $rowarray[4];

}

?>
But you must make sure you order by alphebetic order
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

Thanks, Phenom - you are Phenom-enal! (Sorry - couldn't resist.)

This didn't work exactly the way it is written, but it pointed me in the right direction. I have almost figured this out, but have one small issue that I can't resolve: The header letter appears before EVERY listing instead of only at the beginning of each section. Here is how I have amended the code:

Code: Select all

<?php
$query = "SELECT * FROM member WHERE type='2' ORDER BY company ASC";
$r = mysql_query($query);
					  
$tmp_cnt = 0;   // temporary variable
$per_row = 2;
					  
for ($j = 0; $j < mysql_num_rows($r); $j++) {
						
if ($tmp_cnt % $per_row == 0)
echo ('<tr>');
						
$rowarray = mysql_fetch_array($r);
$field_num = mysql_num_fields($r);


echo ("<td width="310">");
echo ("<table border=0>");

{ 
$currentletter = substr($rowarray[4],0,1);
if ($currentletter != $lastletter)

{
echo ("<tr><td class="heading">" . $currentletter . "</td></tr>");
} 
						
$lastletter = $rowarray[4];
}							
if (!empty ($rowarray[6])) {
echo ("<tr><td class="bodytext"><a href="http://{$rowarray[6]}" target="_blank">") . $rowarray[4] . ("</a></td></tr>");
echo ("<tr><td class="bodytext">") . $rowarray[3] . ("</td></tr>");

   } else {

echo ("<tr><td class="bodytext">") . $rowarray[4] . ("</td></tr><br />");
echo ("<tr><td class="bodytext">") . $rowarray[3] . ("</td></tr>");
}
echo ("</table>");
echo ("</td>");

$tmp_cnt = $tmp_cnt + 1;     
if ($tmp_cnt % $per_row == 0)    
echo ('</tr>');                  

}
?>
Output looks like:
A A
A1 A2
A A
A3 A4

B B
B1 B2
Post Reply