Results 1 to 4 of 4

Thread: URL routing to pages

  1. #1
    Join Date
    Nov 2007
    Location
    India
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default URL routing to pages

    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.


    PHP Code:
    </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

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    are you asking how to parse the links ?

    PHP Code:
    echo '<ul>';
    foreach(
    $pages as $page => $pageValue) {
         echo 
    '\n\t<li><a href="'$pageValue .'">'$page .'</a>';
    }
    echo 
    '</ul>'
    where $page = title
    $pageValue = url

  3. #3
    Join Date
    Nov 2007
    Location
    India
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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
    Code:
    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
    PHP Code:
    $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>';
    }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •