I have a mysql database connected with php. However, I want each row in the database to be a unique page. Could anyone please provide some sort of overview for how this woul
Here is an example of how I am taking a URL to display a different page for each row in a Property database.
I've shown my search mechanism below, but it can take a URL such as /search/id/123
to display property with id:123. $app->Property->search($array)
is where the MySQL code is executed. This script exports a JSON object, which is loaded by Ajax and displayed by JavaScript on the other end. You don't have to export JSON, you could also load a PHP/HTML template and export the page in PHP too.
$value) $qs[$key] = urldecode(htmlspecialchars($value));
array_shift($qs); #clear blank first element
# Parse URLs with the following format:
# /search/{$field1}/{$value1}/{$field2}/{$value2}/.../{$fieldN}/{$valueN}
switch ($val = array_shift($qs)) {
case "search":
$params = array(); # query array
$field = true; # switch between field or value
while (!is_null($val=array_shift($qs))) {
if ($field) $params[-1] = $val;
else $params[$params[-1]] = urldecode($val);
$field = !$field;
}
unset($params[-1]);
$result = $app->Property->search($params);
header('Content-type: application/json');
echo json_encode($result, JSON_FORCE_OBJECT);
exit; #don't load template after exporting JSON object
break;
...
This script works together with a .htaccess
script that redirects all requests to my index.php
file. The index.php at some point loads this controller.php
file, which handles the URLs and loads the page depending on the URL.
Here is the .htaccess code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]