View Full Version : URL routing to pages

12-11-2007, 11:39 PM
Hi All,

I'm a newbie in PHP. Im working as SQL DBA and thought of creating a website for it.
My plan is to have a single layout and the articles, scripts should be in loaded inside the layout only even if we specify link directly in Browser URL.

I've managed to create a layout with all the menus. So the URL looks like as below
When the above url is used it will correctly load the script page inside the layout and i've hardcoded the values for the variable in the index page as below.

<? $pages = Array(
'abtus' => 'abtus.php',
'articles' => 'articles.php',
'home' => 'home.php',
'scripts' => 'scripts.php',
'tips' => 'tips.php',
'forums' => 'forums/index.php',
'stats' => 'stats.php',
'sugg' => 'suggestions.php',
'error' => '',
if (isset($_GET['page']) && array_key_exists($_GET['page'],$pages)) {
else {
} ?>

Inside the script page im calling database to provide the links of all my articles and scripts. Here is the problem im unable to solve, I want the URL to be displayed as shown above for all the articles and scripts from the database. I can't hardcode all the links as above into the index page to avoid increase in page size.

Can any one explain me how to do this with an example.

Thanks for understanding.


12-12-2007, 02:01 PM
are you asking how to parse the links ?

echo '<ul>';
foreach($pages as $page => $pageValue) {
echo '\n\t<li><a href="'. $pageValue .'">'. $page .'</a>';
echo '</ul>';

where $page = title
$pageValue = url

12-20-2007, 01:31 PM
Hi can you pls elaborate me how can we create simple PHP page navigation. Currently Im saving all the links in a db and i need to call these links as above..

Ex. If any one tries as www.mysite.com/index.php?page=test, then it should correctly load the page test in the main site layout iselft..

I've tried reaching you in yahoo mess but i couldnt get you..


12-20-2007, 01:53 PM
Okay the categories would need to have their own table.
If you have any sub categories then you would need to create an ability to check which category it has for a parent.

an example db setup for your table could be

categories table


then you create a php function that will loop through the table and grab everything from the from the categories table and parses it onto the page template

$db = array(
'host' => 'http://www.domain.com',
'user' => 'username',
'pass' => 'password',
'db' => 'database'

function getCategories($parent)

$conn = @mysqli_connect($db['host'], $db['user'], $db['pass'], $db['db']);

// Checks if a connection can be made, attempts to run a query and return results
$sql = "SELECT cat_id, cat_title, cat_desc FROM categories WHERE parent_id = '". $parent ."'";
if($rs = mysqli_query($sql))
while($result = mysql_fetch_array($rs))
$cat[] = array(
'ID' => $result['cat_id'],
'TITLE' => $result['cat_title'],
'DESC' => $result['cat_desc'],
echo "Unable to Execute Query";
echo "Unable to Connect to Database";

// Checks if categories exist and attempts to print them to the page
echo '<ul id="menu">';
foreach($cat as $c)
echo '<li><a href="'. $c['CAT_ID'] .'" title="'. $c['DESC'] .'">'. $c['TITLE'] .'</a></li>';
echo '</ul>';