Thank you. CodeIgniter is impressive, I agree. I spent some time looking at a while ago. I have looked at some MVC frameworks to see how they do it. Unfortunately it gets very complicated quickly. Although I have to praise a couple of frameworks which write tutorials on the subject. At least giving some insight on where to start.
The database method actually proved to be a bit of a pain, and I also looked at different methods until I got the follow code to work. Mind you this is alfa code, and therefore in my opinion not suited for live websites as such, till debugged and tested fully. For now it does the job.
I came to the solution through the following: 'URL info' (http://nl2.php.net/manual/en/ref.url.php). When I read the 'parse_url' (http://nl2.php.net/manual/en/function.parse-url.php) It showed a link to
'pathinfo' (http://nl2.php.net/manual/en/function.pathinfo.php). Then the code uses mysql_fetch_assoc (http://nl2.php.net/mysql_fetch_assoc) to fetch a result row as an associative array.
The code does go out that the pretty url path is stored in the database as 'this-is-a-url' (without the quotes and with no extension). Although you could probably save it with extension as well. Like I said this code is alfa stage. With for example str_replace() you could technically do the pretty url rewriting on the fly. It is unknown to me what the server overhead exactly would be using str_replace() on the fly. Haven't tested that (yet) fully. From what I read about str_replace() so far it will add more overhead.
Choose model/view based on pretty url
<?php
$home ='http://domain_without_ext_and_slash';
$home_variable = $this_home . $_SERVER['REQUEST_URI'];
$path = $home_variable;
$file = dirname($path);
$path_parts = pathinfo($home_variable);
$qinfo = $path_parts['filename'];
$sql = "SELECT field1, field2, field3
FROM database
";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
$field1 = $row["relevant_row1"];
$field2 = $row["relevant_row2"];
$field3 = $row["relevant_row3"];
if ( $home_variable == 'http://domain.ext/' ):
require ('file1.php');
elseif ($field1 == $qinfo):
require ('file2.php');
elseif ($field2 == $qinfo):
require ('file3.php');
elseif ($field3 == $qinfo):
require ('file4.php');
else:
echo 'not found';
endif;
} mysql_free_result($result);
?>
The mysql_free_result command does give an error at the this point, and I am not sure why at the moment. Clearing the mysql query gives us a clean slate for following queries. Depending on your personal need of course. One possible caveat: if this would have to be done for 10,000+ records it might add up and become slow. I am not sure. Although using caching that overhead could be resolved in certain setups. (the same goes for str_replace as well).
.htacces
<IfModule mod_rewrite.c>
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]
</IfModule>
Personally, I would like to add a check, if not included already, that checks to make sure the relevant php files are on the home server. I am not sure how safe the current code is. Nor am I sure if it is proper code writing.