Results 1 to 11 of 11

Thread: PHP Help?

  1. #1
    XBOX Commy Beo's Avatar
    Join Date
    Mar 2002
    Location
    So Cal
    Posts
    1,963
    Rep Power
    24

    PHP Help?

    Hello, I'm wondering if anyone knows PHP to be able to help me. I usually work with ASP so don't know the best way to write this out.

    Basically, what I need is a different header image displayed depending on what page the user is on. So on page 1, I need banner 1 shown. Page 2 shows banner 2 and so forth. I have six of those instances. I also need to show a different banner image if the user is not on any of those six pages.

    Here is what I have. I'm sure there's a better way to do what I have so far too:

    Code:
       
      if ($content==south){
    print ("image");
    
      }
      if ($content==east){
    print ("image");
    }
      if ($content==inland){
    print ("image");
     } 
      if ($content==north){
    print ("image");
     } 
     
          if ($content==central){
    print ("image");
    }	
     
         if ($content==centralcoastal){
    print ("image");
    }	
    
    here's where I need my "if not" statement
    
    if not any of the items above, show 'this image' instead
    Please help.

  2. #2
    Tahellwichoo =DBA=Ronin's Avatar
    Join Date
    Mar 2002
    Location
    wHisKEYonsin
    Posts
    5,694
    Rep Power
    28

    Re: PHP Help?

    I don't know PHP from a hole in the ground, like you I am most familiar with ASP, however, I also do work a bit with javascript, and I do a bit with nesting if then else's. It appears to me you just have 6 individual if's that don't nest with each other, and maybe thats just how it works in PHP(??). Based on keeping that structure, I suppose the only thing to do at the end is say..
    PHP Code:
    if ($content!=south && $content!=east && $content!=inland && $content!=north && $content!=central && $content!=centralcoast){
    print (
    "image")

    Otherwise, nest all your existing if's together and at the last else put your image for when all else is not true..ie...
    PHP Code:
    if  ($content==south){
        print (
    "image")
    }else{
        if  (
    $content==east){
            print (
    "image")
        }else{
            if  (
    $content==inland){
                print (
    "image")
            }else{
                if  (
    $content==north){
                    print (
    "image")
                }else{
                    if  (
    $content==central){
                        print (
    "image")
                    }else{
                        if  (
    $content==centralcoastal){
                            print (
    "image")
                        }else{            
                            print (
    "image"//this would be the image if no other conditions are true
                        
    }
                    }
                }
            }
        }

    But, like I said, don't know PHP for shit so I could be way off base here.


    "(God) is constantly searching our hearts and minds. He's kind of like Santa Claus" - George W. Bush C.I.C.

  3. #3
    XBOX Commy Beo's Avatar
    Join Date
    Mar 2002
    Location
    So Cal
    Posts
    1,963
    Rep Power
    24

    Re: PHP Help?

    Ronin, you are the best. That's exactly what I needed.

    Thanks!!!


    /me pets Ronin with a rake for old times sake

  4. #4
    The Hiphopopotamus BuddhaMan's Avatar
    Join Date
    Jul 2002
    Location
    Joklahoma
    Posts
    10,708
    Rep Power
    10

    Re: PHP Help?

    I haven't used PHP in a loooong time and only learned enough to finish a project, that's it. But I DO know that nesting nesting IF-THENs like that is a bad idea. You want to use the CASE, SWITCH, and DEFAULT commands which work like BASIC's SELECT-CASE statement and a lot easier to maintain and add more conditions to. Don't forget to read the examples which point out you need the BREAK statement also.

    http://us.php.net/switch

    Reminder for Beo: the forum software's [ php ] tag colors PHP code nicely.

    I just taught myself how to do this from the examples so I'm sure you can figure out how to add to it if you can do ASP (if you need to).

    PHP Code:
    <?php
    switch ($content) {
    case 
    "south":
        print (
    "image1.png");
        break;
    case 
    "east":
        print (
    "image2.gif");
        break;
    case 
    "inland":
        print (
    "image3.jpg");
        break;
    case 
    "north":
        print (
    "image4.jpeg");
        break;
    case 
    "central":
        print (
    "image5.bmp");
        break;
    case 
    "centralcoastal":
        print (
    "image6.tiff");
        break;
    default:
        print (
    "default-banner.dib");
    }
    ?>
    BuddhaMan -- Showin' the ladies his "O" face since 1986


  5. #5
    Tahellwichoo =DBA=Ronin's Avatar
    Join Date
    Mar 2002
    Location
    wHisKEYonsin
    Posts
    5,694
    Rep Power
    28

    Re: PHP Help?

    Glad to help Beo...evidently Buddha has a better solution though. I tried!

    Buddha, do you know if case functions are generally preferable over if..else's in asp as well?

    /me combs hair and pats the former SIK members on teh head with a tennis racket.


    "(God) is constantly searching our hearts and minds. He's kind of like Santa Claus" - George W. Bush C.I.C.

  6. #6
    The Hiphopopotamus BuddhaMan's Avatar
    Join Date
    Jul 2002
    Location
    Joklahoma
    Posts
    10,708
    Rep Power
    10

    Re: PHP Help?

    IMO...nesting IF-THEN statements are a pain. I use CASE when there's a list of things to pick from. I use IF-THEN to check or set the value of boolean variables like "if this = 10 then x= true" kinda thing.

    As I mentioned above, adding another value into those nested PHP IF-THENs would be a PITA since you have to get setup just right with the brackets. It's much easier to add in another value to the CASE statements.

    I haven't programmed in a long time so this is all a hazy memory.
    BuddhaMan -- Showin' the ladies his "O" face since 1986


  7. #7
    Tahellwichoo =DBA=Ronin's Avatar
    Join Date
    Mar 2002
    Location
    wHisKEYonsin
    Posts
    5,694
    Rep Power
    28

    Re: PHP Help?

    Makes sense. Brackets can be a PITA sometimes. Other than that, I just wonder if there is a performance/reliability advantage to using CASE over IFs.


    "(God) is constantly searching our hearts and minds. He's kind of like Santa Claus" - George W. Bush C.I.C.

  8. #8
    The Hiphopopotamus BuddhaMan's Avatar
    Join Date
    Jul 2002
    Location
    Joklahoma
    Posts
    10,708
    Rep Power
    10

    Re: PHP Help?

    One performance advantage I can think of for the PHP CASE statements if it gets a match near the top of the stack it does the command underneath it and then hits a break and ends. If you get a match in one of the first if-then statements, it still has to go thru the rest of the if-thens to keep on checking for matches thereby "wasting time".

    If $content==south in your if statements, it would still need to go thru all the other if-then statements. In the CASE statements it would hity a match on the first case statement then hit the break and end....thereby "saving time".

    How much time that actually is, is probably uber minuscule...but it all adds up on heavily used web pages I guess.
    BuddhaMan -- Showin' the ladies his "O" face since 1986


  9. #9
    Registered User
    Join Date
    Mar 2002
    Location
    Brookline, MA
    Posts
    753
    Rep Power
    23

    Re: PHP Help?

    Quote Originally Posted by BuddhaMan View Post
    One performance advantage I can think of for the PHP CASE statements if it gets a match near the top of the stack it does the command underneath it and then hits a break and ends. If you get a match in one of the first if-then statements, it still has to go thru the rest of the if-thens to keep on checking for matches thereby "wasting time".

    If $content==south in your if statements, it would still need to go thru all the other if-then statements. In the CASE statements it would hity a match on the first case statement then hit the break and end....thereby "saving time".

    How much time that actually is, is probably uber minuscule...but it all adds up on heavily used web pages I guess.
    I'm a little bit late in this discussion but Buddha has the right idea.
    The case statements are much cleaner anyway so it makes looking and understanding them much easier.

    As a side note, I doubt if you're using an if-else statement (like what Ronin did) it will evaluate any other if statements once it finds one that is true. However, I haven't written a compiler/parser so I don't know for sure.

    If you do plan on using if else's like that it doesn't have to nest like that. Code below looks better but I'd still stick with the case.

    Also -- I'm assuming south/east/inland are meant to be string comparisons and are not constants.
    PHP Code:
    if ($content == "south")
      print (
    "image1");
    elseif (
    $content == "east")
      print (
    "image2");
    elseif (
    $content == "inland")
      print (
    "image3");
    elseif (
    $content == "north")
      print (
    "image4");
    elseif (
    $content == "central")
      print (
    "image5");
    elseif (
    $content == "centralcoastal")
      print (
    "image6");
    else
      print (
    "image7"); 

  10. #10
    XBOX Commy Beo's Avatar
    Join Date
    Mar 2002
    Location
    So Cal
    Posts
    1,963
    Rep Power
    24

    Re: PHP Help?

    I'm a bit late on coming back to this thread, but wanted to thank you all for your input. The site worked out using your input, and I got it off my desk!

    Using cases makes much sense too. Thanks for suggesting it Buddha.

  11. #11
    Tahellwichoo =DBA=Ronin's Avatar
    Join Date
    Mar 2002
    Location
    wHisKEYonsin
    Posts
    5,694
    Rep Power
    28

    Re: PHP Help?

    You got it off your desk, but now what about getting Josh out from underneath it? I am afraid I don't think anyone can offer any valuable input on that one!!


    "(God) is constantly searching our hearts and minds. He's kind of like Santa Claus" - George W. Bush C.I.C.

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
  •  

< Contact Us - >

Powered by: vBulletin Version
Copyright ©2000, 2001, Jelsoft Enterprises Limited.