Page 1 of 1
Slow page load - too many scripts on page?
Posted: Wed Sep 15, 2004 10:49 pm
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?
Posted: Wed Sep 15, 2004 11:12 pm
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);
Posted: Thu Sep 16, 2004 12:10 pm
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.
Posted: Thu Sep 16, 2004 12:20 pm
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).
Posted: Thu Sep 16, 2004 1:41 pm
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.
Posted: Thu Sep 16, 2004 2:15 pm
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.
}
Posted: Thu Sep 16, 2004 3:00 pm
by cdickson
You are too kind, Pickle!
I will try this.
Posted: Mon Sep 27, 2004 12:14 pm
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>');
}
?>
Posted: Mon Sep 27, 2004 12:24 pm
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.
Posted: Mon Sep 27, 2004 4:25 pm
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
Posted: Wed Sep 29, 2004 10:13 am
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