.htaccess - PHP URL Routing - Using Database Entries in Class -
orginal query - updated query below
in process of building custom application in php. know there have been many questions asked routing etc. on here , have spent many of hours reading them all. how got routing elements work in first place. 1 thing cant fit project peoples suggestions on how route urls based on database entry.
the application working fine , have url routing in place works how want it. issue have when add new products database have trigger generates seo friendly url , stores in field in database.
all product urls structured in same way.
/north/productdetails/productname
/south/productdetails/productname
/northeast/productdetails/productname
etc.
looking not have manually write new url route routes.php file every time product added.
this .htaccess
rewriteengine on rewritebase / rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.+)$ index.php?uri=$1 [qsa,l]
and 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 go in , catch every url passed it. needs check url , send relevant information page.
$route->add('/results/northeast/productdetails/<whateveryisstoredindatabse>', 'producturls');
the class file use @ min check 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 in database>; } else { $itemid = '0'; $_post['showproductdetails'] = $itemid; } public function __construct() { producturls::item($itemid); include 'pages/productdetails.php'; } }
the class wrote above use deter current urls onsite, have modified identify need on.
this route controller:
class route { private $_uri = array(); private $_method = array(); /** * builds collection of internal url's * @param type $uri */ public function add($uri, $method = null) { $this->_uri[] = '/' . trim($uri, '/'); if ($method != null) { $this->_method[] = $method; } } /** * triggers start page */ public function gogo() { $urigetparam = isset($_get['uri']) ? '/' . $_get['uri'] : '/'; foreach ($this->_uri $key => $value) { if (preg_match("#^$value$#", $urigetparam)) { $usemethod = $this->_method[$key]; new $usemethod(); } } } }
does have suggestions in can have? or require complete rewrite of routing?
regards
updated
i have left original query in one. landing on page have been able obtain data database , use generate route in routing controller dynamically. how did it.
my index.php file looks 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` `products`"; $r=mysql_query($q); $numrows = mysql_num_rows($r); if($numrows==0) { // nothing database empty - not needed 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 1 of these urls called diverts right page. thing i'm having issues passing relevant id's via $_post
this ended not working.
class producturls { function clubs($itemid) { $q="select `itemid`,`seofriendlyurl` `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 doesn't work. because query etc. in index page needed again here? , better query store id in variable , use in function instead?
regards
i have managed working. here did. may of use else.
index.php
include 'system/config/routeurl.php'; $q="select `itemid`,`seofriendlyurl` `products`"; $r=mysql_query($q); $numrows = mysql_num_rows($r); if($numrows==0) { echo "there's nothing here!"; } // add's seofriendly url's in table 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` `products` `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"; // make sure productid being passed. include 'pages/productdetails.php'; } }
Comments
Post a Comment