PDA

View Full Version : URL routing to pages



kvs1983
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
www.mysite.com/index.php?page=script
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.



</script>
<? $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)) {
include($pages[$_GET['page']]);
}
else {
include('home.php');
} ?>

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.

Regards,
Sagar

boogyman
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

kvs1983
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..

thanks

boogyman
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


cat_id
parent_id
cat_title
cat_desc


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
if($conn)
$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'],
)
}
}
else
{
echo "Unable to Execute Query";
}
}
else
{
echo "Unable to Connect to Database";
}

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

}