Simple PHP IF - THEN

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dslax27
Forum Newbie
Posts: 3
Joined: Fri Nov 26, 2010 1:00 pm

Simple PHP IF - THEN

Post by dslax27 »

Simple PHP question: I would like an image1.jpg to display if the user is viewing 'events' and the dynamic image carousel to display if the user is viewing the home page.

Here is the working PHP code for the carousel:

Code: Select all

<?php 
  $content = "[smooth=id: 1; width:500; height:300; timed:true; arrows:true; carousel:true; links:false; info:false; align:center; frames:true; delay:35000; transition:fade; open:false; text:Pictures;]"; 
  smooth_show($content); 
?>
... and my failed attempt at making this work :(

Code: Select all

	<?php if(is_page('events')) { ?>
				<img src="http://www.fakeurl.com/image1.jpg">
				<?php } else { ?>
				<?php } ?>
			
				<?php if(is_home()  ){ 
					 $content = "[smooth=id: 1; width:580; height:300; timed:true; arrows:true; carousel:false; links:false; 						info:true; align:center; frames:true; delay:9000; transition:fade; open:true; text:;]"; 
				  smooth_show($content); ?>
				   }
				<?php } else { ?>
				<?php }	?>


Any ideas on where I went wrong?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Simple PHP IF - THEN

Post by greyhoundcode »

How about this:

Code: Select all

if (is_page('events'))
	echo '<img src="http://www.fakeurl.com/image1.jpg">';

elseif(is_home())
{ 
	$content = "[smooth=id: 1; width:580; height:300; timed:true; arrows:true; carousel:false; links:false; info:true; align:center; frames:true; delay:9000; transition:fade; open:true; text:;]";
	smooth_show($content); 
}
If your first condition is met (we're on the events page) then it will try to display image1.jpg, whereas if the second condition is met (on the home page I assume) then the smooth_show() function will be called.
dslax27
Forum Newbie
Posts: 3
Joined: Fri Nov 26, 2010 1:00 pm

Re: Simple PHP IF - THEN

Post by dslax27 »

That would work; however, I did not explain that there are more than two pages on my website. How could I have two IF statements (the first directing the user to an image and the second directing the user to the carousel) and then ELSE returns blank?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Simple PHP IF - THEN

Post by greyhoundcode »

dslax27 wrote:How could I have two IF statements (the first directing the user to an image and the second directing the user to the carousel) and then ELSE returns blank?
If your final Else isn't returning anything then I'm not sure what the point of it is, perhaps I'm misunderstanding you?

Code: Select all

// Direct the user to an image
if (is_page('events'))
	echo '<img src="http://www.fakeurl.com/image1.jpg">';

// Direct the user to the carousel
elseif(is_home())
{ 
	$content = "[smooth=id: 1; width:580; height:300; timed:true; arrows:true; carousel:false; links:false; info:true; align:center; frames:true; delay:9000; transition:fade; open:true; text:;]";
	smooth_show($content); 
}

// Or do nothing
else
{
	// This clause is pretty superfluous, but you could
	// stick any alternative logic in here
}
dslax27
Forum Newbie
Posts: 3
Joined: Fri Nov 26, 2010 1:00 pm

Re: Simple PHP IF - THEN

Post by dslax27 »

This code will reside in my header.php file, thus, it will be called upon everytime anyone views any page on my site. Since there are more than two pages on my site, I would like a jpg to be associated with one page, a carousel with another, and then no images for any other page.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Simple PHP IF - THEN

Post by greyhoundcode »

I take it this is a WordPress theme you're talking about (or something similar)?

Even so, from what you are saying, you should only need the following structure:

Code: Select all

IF     (we are on the events page)
THEN:  display a single image
ELSEIF (we are on the home page)
THEN:  display the carousel
If in all other cases you don't need to display anything, then the above alone should suffice. No need for an additional else or elseif For example, if you have a Home Page, Events Page and a Banana Page, and the visitor lands on the Banana Page, then neither condition is met and nothing is displayed.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Simple PHP IF - THEN

Post by greyhoundcode »

But if you really wanted to you could add in the unnecessary else clause as per my code above. It's just that it wouldn't do anything.

Perhaps we are talking at cross-purposes?
Post Reply