Jump to content

Help with simple if / else statement


Mr.Canuck

Recommended Posts

Hey everyone. I am a PHP novice and I was wondering if anyone can assist me with this if else statement. I don't have something right, because it doesn't work. So, if conditions are true, it won't display the <div> code and if the conditions are false, then it will display the <img> instead. Thanks.

 

<?php	
$cpage = $_GET['cpage']; 
?>

	<?php
if ($cpage != 'intro' && $cpage !='directions' && $cpage !='hours' && $cpage !='jam' && $cpage !='pointe'  && $cpage !='about'  && $cpage !='tc' && $cpage !='privacy' && $xlspg !='contact_us')  { 
?>
	  <div id="contain2"><?php $this->crumbTrail->Render(); ?></div>
            
      <?php else {
	  
<img class="img1" src="css/images/bcreplace.jpg" width="695" height="29" alt="bcreplace" />   
}
?>

Link to comment
Share on other sites

Try this:

 

	

<?php
if ($cpage != 'intro' && $cpage !='directions' && $cpage !='hours' && $cpage !='jam' && $cpage !='pointe'  && $cpage !='about'  && $cpage !='tc' && $cpage !='privacy' && $xlspg !='contact_us')  { 
?>

  <div id="contain2"><?php $this->crumbTrail->Render(); ?></div>
            
      <?php }else {

<img class="img1" src="css/images/bcreplace.jpg" width="695" height="29" alt="bcreplace" />   
}
?>

Link to comment
Share on other sites

You have no closing braket on the IF condition. But, I would clean that code up by doing this:

 

<?php

$cpage = $_GET['cpage'];
$cpage_list = ('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy');

if (!in_array($cpage, $cpage_list) && $xlspg !='contact_us')
{
    echo "<div id=\"contain2\">" . $this->crumbTrail->Render() . "</div>";
}
else
{
    echo "<img class=\"img1\" src=\"css/images/bcreplace.jpg\" width=\"695\" height=\"29\" alt=\"bcreplace\" />";
}

?>

Link to comment
Share on other sites

Thanks for the replies guys. However, it does not seem to be working. If I run this code (without the "else") the page loads and it does what it is supposed to do for the "if". When I use the code above with the "else", the page does not load:

  <?php	
$cpage = $_GET['cpage']; 
?>

<?php

if ($cpage != 'intro' && $cpage !='directions' && $cpage !='hours' && $cpage !='jam' && $cpage !='pointe'  && $cpage !='about'  && $cpage !='tc' && $cpage !='privacy' && $xlspg !='contact_us')  { 
?>

<?php

}
?> 

Link to comment
Share on other sites

You have no closing braket on the IF condition. But, I would clean that code up by doing this:

 

<?php

$cpage = $_GET['cpage'];
$cpage_list = ('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy');

if (!in_array($cpage, $cpage_list) && $xlspg !='contact_us')
{
    echo "<div id=\"contain2\">" . $this->crumbTrail->Render() . "</div>";
}
else
{
    echo "<img class=\"img1\" src=\"css/images/bcreplace.jpg\" width=\"695\" height=\"29\" alt=\"bcreplace\" />";
}

?>

 

I think you used ( ) instead of " " on your string declaration:

 

EDIT: I see it is an array.. just missed the "array"


<?php

$cpage = $_GET['cpage'];
//$cpage_list = ('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'); // unexpected ',' ....
//$cpage_list = "'intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'"; // not supposed to be a string...

$cpage_list = array('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'); // much better

if (!in_array($cpage, $cpage_list) && $xlspg !='contact_us')
{
    echo "<div id=\"contain2\">" . $this->crumbTrail->Render() . "</div>";
}
else
{
    echo "<img class=\"img1\" src=\"css/images/bcreplace.jpg\" width=\"695\" height=\"29\" alt=\"bcreplace\" />";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.