Forum Moderators: coopster
example
this works
http://www.example.co.uk/demo/content.php?title=about%20us
this doesn't but links correctly
http://www.example.co.uk/demo/content.php?title=about-us
here is my page belwow
I feel I'm so close on this one yet so far
$query="SELECT * FROM content WHERE title='$title'";
$result=mysql_query($query);
mysql_close();
function stringForUrl($strIn) {
$strOut = $strIn;
$allowed = '/[^a-z0-9 ]/ims';
// only alphanum and spaces allowed
$strOut = preg_replace($allowed, '', $strOut);
// swap spaces for _
$strOut = str_replace(' ', '-', $strOut);
$strIn = str_replace('-', ' ', $strIn);
// make lower case for consistancy
$strOut = strtolower($strOut);
return $strOut;
}
$content_id=mysql_result($result,$i,"content_id");
$title=mysql_result($result,$i,"title");
$desc=mysql_result($result,$i,"desc");
$sef = stringForUrl($title);
///////////////////////////////////////////////////////////////////////////////////////////
$site_title = "My Website";
$page_title = "Sample Template 3 Content Page 1";
$main .= "<a href=content.php?title=$sef>$title</a><br>$desc<hr>";
require('include/header.php');
require('include/strapline.php');
require('include/main_menu.php');
require('include/footer.php');
// First we read the contents of the template.php file as the variable $template.
$template = file_get_contents('include/template.php');
// Now we do a search and replace on $template inserting content where the keywords were placed
// We do this with 2 arrays, one for search and one for replace.
// We then use the preg_replace function to make the changes and create an output variable
$search = array('¦{PAGE_TITLE}¦',
'¦{MAIN_MENU}¦',
'¦{MAIN}¦',
'¦{HEADER}¦',
'¦{FOOTER}¦',
'¦{STRAPLINE}¦'
);
$replace = array("$site_title - $page_title",
$main_menu,
$main,
$header,
$footer,
$strapline
);
$output = preg_replace($search, $replace, $template);
echo $output;
?>
[edited by: jatar_k at 4:14 pm (utc) on Aug. 10, 2007]
[edit reason] no urls thanks [/edit]
So at the SELECT statement the about%20us and about-us are sent as 2 different pages i.e. looking for 2 different pages in database. If you want users to be able to get the page both ways then move your str_replace up to the top of your code then the spaces and - will be converted before the SELECT statement.
<?php require('include/connection.php');
function stringForUrl($strIn) {
$strOut = $strIn;
$allowed = '/[^a-z0-9 ]/ims';
// only alphanum and spaces allowed
$strOut = preg_replace($allowed, '', $strOut);
// swap spaces for _
$strOut = str_replace(' ', '-', $strOut);
// make lower case for consistancy
$strOut = strtolower($strOut);
return $strOut;
}
$query="SELECT * FROM content WHERE title='$title'";
$result=mysql_query($query);
mysql_close();
$content_id=mysql_result($result,$i,"content_id");
$title=mysql_result($result,$i,"title");
$desc=mysql_result($result,$i,"desc");
$sef = stringForUrl($title);