So far I've been learning PHP by example by customizing a prewritten script.
I have two pages. Both return about the same number of results using the same tables. Page One returns the last 15 stories posted; Page Two returns all stories posted in a single category.
However, Page One loads very slowly and sometimes times out, while Page Two is fine. Iām guessing it has to do with how Page Two handles arrays, but Iām not sure.
I would really appreciate your help with two things:
Help me understand conceptually why Page One loads so much slower.
Help me fix Page One to function more like Page Two. It would be very helpful to see some code examples to learn from.
Here are snippets of code from both pages:
Page One (the slow one)
<?
$table = $dl->select(
"s.*, u.*, a.*,cg.*,sg.*, COUNT(DISTINCT c.cid) AS chap, COUNT(DISTINCT r.rid) AS rev",
"sl18_stories s
LEFT JOIN sl18_chapters c ON c.csid=s.sid
LEFT JOIN sl18_users u ON u.uid=s.suid
LEFT JOIN sl18_rate a ON a.ratsid=s.sid
LEFT JOIN sl18_category cg ON cg.caid=s.scid
LEFT JOIN sl18_subcategory sg ON sg.subid=s.ssubid
LEFT JOIN sl18_review r ON r.rsid=s.sid",
"",
"GROUP BY s.sid ORDER BY s.stime DESC LIMIT 0,15"
);
if ( count( $table ) < 1 )
print "There are currently no published stories.";
else {
foreach($table as $row) {
?>
Page Two
<?
$table = $dl->select("*","sl18_stories",array('ssubid'=>$_GET["list"]));
if ( count ( $table ) == 0 ) {
print "There are no stories contained in this sub-category";
print "</td></tr>";
} else {
$total = count( $table );
$total = ceil($total / 15);
if ( !isset ( $_GET["page"] ) || $_GET["page"] == 1 ) {
$page = 1;
$getpage = 0;
} else {
$page = $_GET["page"];
$getpage = $_GET["page"] * 15 - 15;
}
$table = $dl->select(
"s.*, u.*, a.*, COUNT(DISTINCT c.cid) AS chap, COUNT(DISTINCT r.rid) AS rev",
"sl18_stories s
LEFT JOIN sl18_chapters c ON c.csid=s.sid
LEFT JOIN sl18_users u ON u.uid=s.suid
LEFT JOIN sl18_rate a ON a.ratsid=s.sid
LEFT JOIN sl18_review r ON r.rsid=s.sid",
array('s.ssubid'=>$_GET["list"]),
"GROUP BY s.sid ORDER BY s.sname ASC LIMIT $getpage,15"
) or die(
$dl->getError()
);
?>