PHP MySQL site with one page changing content area depending on variable?

Posted on 2007-10-19
Last Modified: 2013-12-13
If I knew what this was called, I could ask the question properly, but I don't. This site has one page, and this is the PHP (along with other various includes and library files not listed) part I don't get
$catId  = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0;
$pdId   = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0;
and then where the content changes it has this
if ($pdId) {
      require_once 'include/productDetail.php';
} else if ($catId < '20' && $catId > '0') {
      require_once 'include/categoryList.php';
} else if ($catId) {
      require_once 'include/productList.php';
Which I am guessing changes the content depending on which function was invoked and then replaces the content area contents.
I want to integrate a search feature and it's not the search part that is stumping me, it's how to get it to display like the rest of the dynamic content (in that one area).  This is older code and all my books don't use this technique and are all PHP 5.  Well, that's my excuse. ;)
I am thinking I should have my form in the sidebar with the shopping cart (I will add it to the include file that makes up the sidebar), and it's processor in one seperate library file, then the formatting of the results in an include file?  I know I need to figure out what variable I should be using in the first pattern - $searchId maybe?  then that one lowercase letter?  yikes, what is that?  then write my else if part to include the search content should it be requested.  Oh yeah, and make sure I include the form processor in the require_once section that I didn't listin the code here.
 I don't think I understand how the variables are getting from the processor then back to the page to be displayed.  If this is not enough code, I'll post what ever you need.
Question by:google_girl
    LVL 6

    Expert Comment

    Hi google_girl, I hate it when other people do this, but take a look at:

    It has a healthy discussion about passing variables from one page to another in PHP, and just copying it and pasting it here feels like cheating.  Basically, if you have the variable set up in a Form on the first page, it's automatically passed to the next page when the Submit button is pressed.

    You can also embed the variables in the URL:  <a href="search.php?searchidurl=12345>Item 12345</a>

    Then in search.php, set up a php variable to retrieve the variable in the URL:
    $searchid = $_GET['searchidurl'];

    If you are using sessions, you can create your own session variables which are stored on the server until you need them.

    Hope that gets you started, at least enough to search for more info!
    LVL 3

    Author Comment

    Okay, I have the search working  - except for my original problem of keeping it all on the same page.  I don't want to pass it - I want it to display the result on the same page but in a certain location.  This site has one page, and for example, if you click on a product link, the navigation changes and the content section changes, but it's all the same page.  So there's an index.php and that's it.
    You can see that I have variables being retrieved from the URL, but I am unsure how to integrate this with the search.  But now that i know what that first section is called (retreiving the variable in the url) I can at least research it better.  
    LVL 6

    Accepted Solution

    Right, now you can pass the variable >to the same page< using a hyperlink:

    On the page index.php (or whatever):
    <a href="index.php?item=12345>Item 12345</a>

    This will reload the index.php page, but this time you can use the item variable to change the layout, say, to add an image of the selected item:

        $item = $_GET['item'];
        if (!Empty($item) {
            echo ('<img src="images/' . $item . '.gif" />');

    I know this isn't exactly the problem you're trying to solve, but it's the methodology for it - pass a variable to the same page using sessions, url variables, or via forms, and use conditional statements to look at the newly passed variable and format/construct the page accordingly.  Sessions are great for storing and passing along an array of multiple shopping cart items.

    I hope this helps, but I'm not quite sure what you want to do with the searchbar :can you post some more of your code and describe what you want to accomplish?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now