PHP If Else correction for charity website

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
Martin Dickson
Forum Newbie
Posts: 3
Joined: Wed Apr 04, 2012 4:58 am

PHP If Else correction for charity website

Post by Martin Dickson »

Hi there,

Thanks for having a look at my question. I am a newbie but I have managed to built a site for my charity (see www.youthheroes.org.uk) and I want to display a different menu depending on the the location of the project we are working on. I have managed to get close to what I want using the following code which calls a Wordpress menu:

<?php if ( get_post_meta($post->ID, 'main', true) ) { ?>
<?php do_action('wp_menubar','Main Menu'); ?>
<?php } ?>

<?php if ( get_post_meta($post->ID, 'wirral', true) ) { ?>
<?php do_action('wp_menubar','Main Menu'); ?>
<?php } ?>

<?php if ( get_post_meta($post->ID, 'salford', true) ) { ?>
<?php do_action('wp_menubar','Salford'); ?>
<?php } ?>

However, I would like to include a catch all and tidy up the code a bit to make expansion easier. I have this code below but it's incorrect and returns an error :banghead: :

<?php if ( get_post_meta($post->ID, 'salford', true) ) { ?>
<?php do_action('wp_menubar','salford'); ?>
<?php elseif ( get_post_meta($post->ID, 'wirral', true) ) ?>
<?php do_action('wp_menubar','wirral'); ?>
<?php else ?>
<?php do_action('wp_menubar','Main Menu'); } ?>

Any help much appreciated.

Thanks,

Martin
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: PHP If Else correction for charity website

Post by litebearer »

Mighth consider using a SWITCH statment
User avatar
azycraze
Forum Commoner
Posts: 56
Joined: Mon Oct 24, 2011 12:08 pm
Location: India

Re: PHP If Else correction for charity website

Post by azycraze »

what is the need of increasing php tags if you can just include your code in one.

<?php
if ( get_post_meta($post->ID, 'salford', true) )
{
do_action('wp_menubar','salford');
}
elseif ( get_post_meta($post->ID, 'wirral', true) )
{
do_action('wp_menubar','wirral');
}
else
{
do_action('wp_menubar','Main Menu');
}

?>

will work.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP If Else correction for charity website

Post by social_experiment »

There are 2 options you can use but could you paste the error that you are receiving
http://php.net/manual/en/control-structures.elseif.php
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Martin Dickson
Forum Newbie
Posts: 3
Joined: Wed Apr 04, 2012 4:58 am

Re: PHP If Else correction for charity website

Post by Martin Dickson »

Thanks all! Much appreciated.

I'll look in to those other options litebearer and social_experiment; in the meantime Azycraze has solved my problem - I messed up the { and } locations and I had no idea that I could have it all in one PHP tag - two lessons learned today!
Martin Dickson
Forum Newbie
Posts: 3
Joined: Wed Apr 04, 2012 4:58 am

Re: PHP If Else correction for charity website

Post by Martin Dickson »

I wonder if I can get one other quick answer to a similar thing. This works:

<?php if ( get_post_meta($post->ID, 'main', true) ) { ?>
<div class="main_table">
<?php } ?>

<?php if ( get_post_meta($post->ID, 'wirral', true) ) { ?>
<div class="main_table_wirral">
<?php } ?>

<?php if ( get_post_meta($post->ID, 'salford', true) ) { ?>
<div class="main_table_salford">
<?php } ?>

But I would like to use something like this because it allows for a default using the else:

<?php if ( get_post_meta($post->ID, 'salford', true) ) {
'<div class="main_table_salford">' ; }
elseif ( get_post_meta($post->ID, 'wirral', true) ) {
'<div class="main_table_wirral">' ; }
else {
'<div class="main_table">' ; }
?>

There are no errors in the code but the ' before and after the div class means that it just ignores this line. Without the ' I get an error presumably because I have < and > in the middle of the php.

Does that make any sense at all? I am such a novice but I am really enjoying this.

Cheers,

Martin
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP If Else correction for charity website

Post by social_experiment »

Have a look at a switch statement as litebearer suggested http://hu.php.net/manual/en/control-str ... switch.php
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
azycraze
Forum Commoner
Posts: 56
Joined: Mon Oct 24, 2011 12:08 pm
Location: India

Re: PHP If Else correction for charity website

Post by azycraze »

use echo to output div tags
===============================
echo '<div class="main_table_salford">' ;
Post Reply