Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

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

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.
  • 2
1 Solution
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!
google_girlAuthor Commented:
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.  
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?


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now