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
https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/
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" />   
}
?>

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\" />";
}

?>

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

}
?> 

 

A different coding style isn't going to magically fix the fact that he has a missing bracket, it is just a different way.

 

OP: Try using a syntax highlighting editor, to find your errors.

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\" />";
}

?>

Archived

This topic is now archived and is closed to further replies.

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