ORGINAL QUERY - Updated Query Below
I am in the process of building a custom application in PHP. I know there have been many questions asked about Routing etc. on here and I have spent many of hours reading them all. This is how I got my routing elements to work in the first place. However 1 thing I cant get to fit into my project is peoples suggestions on how to route URLs based on a database entry.
The application itself is working perfectly fine and I already have some URL Routing in place which works exactly how I want it. The issue I have is when I add new products into my database I have a trigger that generates a SEO Friendly URL and stores it in a field in the database.
All the product URLs are structured in the same way.
/North/productdetails/Productname
/South/productdetails/Productname
/NorthEast/productdetails/Productname
etc.
What I am looking to do is not have to manually write a new URL Route into the routes.php file every time a product is added.
this is my .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
rewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
and my index.php file contains:
<?php
header("Cache-Control: no-cache");
include 'System/Config/route.php';
include 'System/Config/settings.php';
connect();
$route = new Route();
include 'System/Config/routeURL.php';
$route->add('/', 'Home');
$route->add('/results', 'Results');
$route->add('/special', 'Special');
$route->gogo();
?>
I need something like this to go in and catch every URL passed to it. It then needs to check the URL and send the relevant information to a page.
$route->add('/results/NorthEast/productdetails/<whateveryisstoredindatabse>', 'productURLS');
The class file that I use at the min to check it is this:
class productURLS
{
function Item($itemID)
{
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$itemID = '0';
if($host == host +<URLStored in database>) {
$itemID = <what ever id is in database>;
} else {
$itemID = '0';
$_POST['ShowProductDetails'] = $itemID;
}
public function __construct()
{
productURLS::Item($ItemID);
include 'pages/productdetails.php';
}
}
The class I wrote above is what I use to deter the current URLS onsite, I have modified it to identify where I need help on.
This is the route controller:
class Route
{
private $_uri = array();
private $_method = array();
/**
* Builds a collection of internal URL's to look for
* @param type $uri
*/
public function add($uri, $method = null)
{
$this->_uri[] = '/' . trim($uri, '/');
if ($method != null) {
$this->_method[] = $method;
}
}
/**
* Triggers from start page
*/
public function gogo()
{
$uriGetParam = isset($_GET['uri']) ? '/' . $_GET['uri'] : '/';
foreach ($this->_uri as $key => $value)
{
if (preg_match("#^$value$#", $uriGetParam))
{
$usemethod = $this->_method[$key];
new $usemethod();
}
}
}
}
Does anyone have any suggestions in what I can do with what I have? or will it require a complete rewrite of the routing?
Regards
UPDATED
I have left the original query in as well as this one. For anyone landing on this page I have now been able to obtain data from the database and use it to generate a route in my routing controller dynamically. This is how I did it.
my index.php file now looks like this:
<?php
header("Cache-Control: no-cache");
include 'System/Config/route.php';
include 'System/Config/settings.php';
connect();
$route = new Route();
include 'System/Config/routeURL.php';
$q="SELECT `itemID`,`SEOFriendlyURL` FROM `Products`";
$r=mysql_query($q);
$numrows = mysql_num_rows($r);
if($numrows==0)
{
// Does nothing as the database is empty - Not really needed but good just in case
}
// Dynamic Routing Elements
while($row = mysql_fetch_array($r))
{
$route->add("/results" . $row['SEOFriendlyURL'] ."", 'productURLS');
}
//Static Routing Elements
$route->add('/', 'Home');
$route->add('/results', 'Results');
$route->add('/special', 'Special');
$route->gogo();
?>
This has worked perfectly, When ever one of these URLS is called it diverts to the right page. The only thing I'm having issues with now is passing the relevant ID's via $_post
This is what I ended up with but its not working.
class productURLS
{
function clubs($ItemID)
{
$q="SELECT `itemID`,`SEOFriendlyURL` FROM `products`";
$r=mysql_query($q);
$numrows = mysql_num_rows($r);
if($numrows==0)
{
$ItemID = '0';
}
while($row = mysql_fetch_array($r))
{
$host = $_SERVER['REQUEST_URI'];
$ClubID = '0';
if($host == "/newtest/results" . $row['SEOFriendlyURL'] ."") {
$ClubID = "'" . $row['itemID'] ."'";
} else {
$itemID = '0';
}
}
$_POST['ShowClubDetails'] = $itemID;
}
public function __construct()
{
productURLS::clubs($itemID);
include 'pages/productdetails.php';
}
}
However this doesn't work. Because the query etc. is in the index page is it really needed again here? and is it better to get the query to store the ID in a variable and use that in the function instead?
Regards
I have managed to get this fully working. Here is what I did. May be of some use to someone else.
Index.php
include 'System/Config/routeURL.php';
$q="SELECT `ItemID`,`SEOFriendlyURL` FROM `Products`";
$r=mysql_query($q);
$numrows = mysql_num_rows($r);
if($numrows==0)
{
echo "There's nothing here!";
}
// Add's all SEOFriendly URL's in table into route file (Dynamic)
while($row = mysql_fetch_array($r))
{
$route->add("/results" . $row['SEOFriendlyURL'] ."", 'ProductURLS');
}
routeURL.php
class ProductURLS
{
public function __construct()
{
$host = $_SERVER['REQUEST_URI'];
$host = ltrim ($host, '/results');
$host = "/$host";
$q="SELECT `ItemID`,`SEOFriendlyURL` FROM `Products` WHERE `SEOFriendlyURL` = '$host'";
$r=mysql_query($q);
if($r) {
$row = mysql_fetch_assoc($r);
$ItemID = $row['ItemID'];
} else {
$ItemID = '0';
}
$_POST['ShowClubDetails'] = $ItemID;
//echo "Whole query: $ItemID"; // This is to make sure the ProductID is being passed.
include 'pages/ProductDetails.php';
}
}