SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if statement for navigation bar

    Hi,

    I am building a left navigation bar with about 10 categories.

    I need an IF statement within the below if statement that identifies when a particular category is clicked:

    PHP Code:
    if(isset($_GET['category'])) {
     
    $selcat $_GET['category'];

    I am using this code. Which then helps create the navigation bar when clicked as so:

    PHP Code:
    if(isset($selcat)) {
      echo 
    "<a href='product.php?category=".$selcat."'>".$cats[$selcat]."</a>";
      unset(
    $cats[$selcat]); //Gets rid of it for later.
      
    $res mysql_query("SELECT productid,name FROM products WHERE categoryid = ".$selcat);
      while(
    $row mysql_fetch_array($res)) {
       echo 
    "<a href='product.php?productname=".$row['productid']."'>".$row['name']."</a>";
      }

    The problem is if people click accessories I DO NOT want the links to read

    PHP Code:
    "<a href='product.php?productname=".$row['productid']."'>".$row['name']."</a>"
    as there is a few levels to the accessory navigation. For instance, rather than a list of products, there is first a list of manufacturers.

    What I need is to do is further develop the first bit of code:

    PHP Code:
    if(isset($_GET['category'])) {
     
    $selcat $_GET['category'];

    How can I get this to identify: IF $_GET['category'] = 8 then $selcataccessory=something different
    else as normal ($selcat= $_GET['category']


    Then I can change the code that writes the navigation to recognise something different using isset. And alter the $res function also.

    PHP Code:
    if(isset($selcataccessory)) {
      echo 
    "<a href='product.php?category=".$selcat."'>".$cats[$selcat]."</a>";
      unset(
    $cats[$selcat]); //Gets rid of it for later.
      
    $res mysql_query("SELECT ANOTHER TABLE FROM products WHERE categoryid = ".$selcataccessory);
      while(
    $row mysql_fetch_array($res)) {
       echo 
    "<a href='ACCESSORY.php?ACCESSORYSUBMENU=".$row['ACCESSORYid']."'>".$row['name']."</a>";
      }

    So I think it is just the first bit of code that needs to be altered. How do I get the if statement added to the if statement to identify category 8 and change the selcat if this is the case!?!? Simple, probably, but I am stuck.

    Matt.
    Last edited by AnthonySterling; May 25, 2011 at 03:33. Reason: Added bbcode tags

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post
    How can I get this to identify: IF $_GET['category'] = 8 then $selcataccessory=something different
    else as normal ($selcat= $_GET['category']
    You've... pretty much spelled it out exactly there.

    (Generalized) IF format:
    if (condition) {
    True Statements;
    } else {
    False Statements;
    }

    Keep in mind that the conditional operator for equals to is == (or === if you're being type-specific).

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - Thanks - I've done that.

    I was surprised that 'or' functions need stating exactly.

    if (condition) must be if ($selcat==8 or $selcat==9)

    NOT

    if($selcat==8 or 9)


    Now I know,

    Matt.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Well, think of it as seperate sentences.
    IF:
    Selcat == 8 //Okay, that's a test i can do.
    OR
    9 // Uh....


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
  •