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!
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).
<?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.
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.
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.
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.
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: