I'm a newbie with PHP and I need help with url query. I've searched and it has been some help, but I'm still confused. This is what I want to do:

for example, at www.jcrew.com , it uses url query to jump from page to page with an id=somenumber. I'd like to know how the heck I can do this. The url remains the same, like www.jcrew.com/index.jhtml?id=cat1111, the only thing that changes is the id. Are the pages dynamically created with a certain id, on the same index.jhtml page? Or are there just many pages and the id is corresponding to those pages?

For me, I have pages name:

index.php
category.php
account.php
etc...

how can I use the index.php and use url query strings to jump to the other pages? I want the url to always display the same thing, just the id would thing. Like http://index.php?id=stone

Please help me out! Thanks.

    Hey, never mind guys. I just figured it out!! Woohoo! It feels so good to get something to work and when it actually looks good.

      Hi. You would normally use an ID to reference a page from a database.

      The easiest way to do it... for your scenario... is do this:

      if(isset($_GET["p"])){ // were p = page
      $page = $_GET["p"];
      include("/[optional folder]/".$page.".php");
      }
      

      I would recommend testing if the page actually exists in your directory before it is included. Ask me if you need more help concerning that.

      Did the above script help?

        yes, thanks evo4ever. However, I do have a problem. Here's a sample of how I have the directories:

        index.php
        catalog/catalog.php
        catalog/rings.php
        catalog/bracelets.php

        The problem I have is that whenever I enter a boggus url query string to test the php script, the page it's suppose to go to shows up all messed up. This is the script I'm using to test:

        if(isset($_GET['category']))
        {
            $cat=$_GET['categoru'];
            if(file_exists($cat.".php")
            {
                if($cat=="rings")
                   include($cat.".php");
                elseif($cat=="bracelet")
                   include($cat.".php");
                else
                   include("../index.php");
            }
            else
                 include("../index.php");
        }
        else
            include("../index.php");
        

        a query would be from the index.php page targeting the catalog.php like this:

        catalog.php?category=rings

        I dont see anything wrong with the code, from index.php I can go to catalog.php, using my links. But when I enter something like:

        catalog.php?category=adfaf

        its suppose to go to index.php, however, there is no error, just that the page does not show up correctly. When I view the source, it is indeed the index.php, all the html is there. Its really strange.

          You know, I guess I could just use a <meta> tag and redirect it to index.php when the url query is wrong.

            Write a Reply...