PDA

View Full Version : PHP Help?



Beo
03-06-2008, 06:39 PM
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:





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. :)

=DBA=Ronin
03-06-2008, 09:45 PM
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..


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...


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.

Beo
03-06-2008, 11:17 PM
Ronin, you are the best. That's exactly what I needed. :)

Thanks!!!


/me pets Ronin with a rake for old times sake

BuddhaMan
03-07-2008, 05:58 AM
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
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");
}
?>

=DBA=Ronin
03-07-2008, 07:20 AM
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.

BuddhaMan
03-08-2008, 07:45 AM
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.

=DBA=Ronin
03-08-2008, 08:58 AM
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.

BuddhaMan
03-08-2008, 06:59 PM
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.

Rico_Suave
03-11-2008, 02:54 PM
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.


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");

Beo
04-01-2008, 03:52 PM
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. :)

=DBA=Ronin
04-01-2008, 10:40 PM
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!!