replacing fields and mysql... what method is faster?
Posted: Mon Aug 13, 2007 1:37 am
I've created a CMS that allows search engine friendly links... The script has a unique id for each page. In the template code, you can put linkid-1-... the PHP code will then replace that with whatever the search engine friendly link is.
At the moment I use this code:
The only problem with that is if there are say 30 links in the template, it has to run 30 queries... that sucks... I'm sure it effects the load time of the page...
Is there a better way to do it so that it loads faster? Maybe run one query and put all the results into an array and then use preg_replace or erg_replace (I'm not sure which one) to replace the fields from the array.
What do you think? Any ideas?
At the moment I use this code:
Code: Select all
// ==============================
// Link All The Pages
// ==============================
$match_count = preg_match_all("/linkid\-([0-9]*)\-/si", $body_content, $matches);
$matches[1] = array_unique($matches[1]);
for ($i = 0; $i < $match_count; $i++) {
$linkid = "";
$body_content_linkid = "";
$linkid = $matches[1][$i];
$linkid_info = mysql_fetch_array(mysql_query("select slug from {$tbl_name}pages where id='$linkid'"));
###############################
# Mod_Rewrite
if ($mod_rewrite == "y") {
$body_content_linkid = $websiteurl."/".$linkid_info[slug];
} else {
$body_content_linkid = "index.php?page=".$linkid_info[slug];
}
###############################
$body_content = str_replace("http://linkid-{$linkid}-", $body_content_linkid, $body_content);
$body_content = str_replace("linkid-{$linkid}-", $body_content_linkid, $body_content);
}Is there a better way to do it so that it loads faster? Maybe run one query and put all the results into an array and then use preg_replace or erg_replace (I'm not sure which one) to replace the fields from the array.
What do you think? Any ideas?