ELSE IF Issue

I’m working on a bit of code which checks at the end if a keyword, description & title exists. If it doesn’t it displays the default value but the default value even though it exists wont display.

Any tips?

	if($hotel['tab'] == 'accommodation'){
			$template->assign('pagetitle', $hotel['hotel']['hotel_accommodation_title']);
			$template->assign('keywords', $hotel['hotel']['hotel_accommodation_keywords']);
			$template->assign('description', $hotel['hotel']['hotel_accommodation_description']);
			} 
	elseif ($hotel['tab'] == 'facilities') {
			$template->assign('pagetitle', $hotel['hotel']['hotel_facilities_title']);
			$template->assign('keywords', $hotel['hotel']['hotel_facilities_keywords']);
			$template->assign('description', $hotel['hotel']['hotel_facilities_description']);
			}
	elseif ($hotel['tab'] == 'activities') {
			$template->assign('pagetitle', $hotel['hotel']['hotel_activities_title']);
			$template->assign('keywords', $hotel['hotel']['hotel_activities_keywords']);
			$template->assign('description', $hotel['hotel']['hotel_activities_description']);
			}
	elseif ($hotel['tab'] == 'map') {
			$template->assign('pagetitle', $hotel['hotel']['hotel_resortplan_title']);
			$template->assign('keywords', $hotel['hotel']['hotel_resortplan_keywords']);
			$template->assign('description', $hotel['hotel']['hotel_resortplan_description']);
			} 
	else 	{
			if(isset($hotel['hotel']['hotel_overview_title'])){
				$template->assign('pagetitle', $hotel['hotel']['hotel_overview_title']);
			} else {
				$template->assign('pagetitle', $hotel['hotel']['hotel_title']);
				var_export($hotel['hotel']['hotel_title']);
			}
			if(isset($hotel['hotel']['hotel_overview_keywords'])){
				$template->assign('keywords', $hotel['hotel']['hotel_overview_keywords']);
			} else {
				$template->assign('keywords', $hotel['hotel']['hotel_keywords']);
			}
			if(isset($hotel['hotel']['hotel_overview_description'])){
				$template->assign('description', $hotel['hotel']['hotel_overview_description']);
			} else {
				$template->assign('description', $hotel['hotel']['hotel_description']);
			}
	}
		

Don’t know about your exact question but wouldnt $template->assign(‘hotel’, $hotel); remove the need for that entire block?

I think to start you could simplify this part at the top, at the bottom i didn’t monkey with


if (isset($hotel['tab']))
{
        // Since map has a different name I did this here..
        if ($hotel['tab'] == 'map')
        {
               $template->assign('pagetitle', $hotel['hotel']['hotel_resortplan_title']); 
               $template->assign('keywords', $hotel['hotel']['hotel_resortplan_keywords']); 
               $template->assign('description', $hotel['hotel']['hotel_resortplan_description']);              
        }
        else
        {
                $template->assign('pagetitle', $hotel['hotel']["hotel_{$hotel['tab']}_title"]);
                $template->assign('keywords', $hotel['hotel']["hotel_{$hotel['tab']}_keywords"]);
                $template->assign('description', $hotel['hotel']["hotel_{$hotel['tab']}_description"]);
        }
}
else { 
 // The rest of your else statement from your code
}  

Thanks, Yep defo makes a difference if you shrink the code however still getting the same issue the last part of the else statement wont work?

if (isset($hotel['tab']))	{	
		if ($hotel['tab'] == 'map') {
				   $template->assign('pagetitle', $hotel['hotel']['hotel_resortplan_title']); 
				   $template->assign('keywords', $hotel['hotel']['hotel_resortplan_keywords']); 
				   $template->assign('description', $hotel['hotel']['hotel_resortplan_description']);              
		}
		else {
				$template->assign('pagetitle', $hotel['hotel']["hotel_{$hotel['tab']}_title"]);
				$template->assign('keywords', $hotel['hotel']["hotel_{$hotel['tab']}_keywords"]);
				$template->assign('description', $hotel['hotel']["hotel_{$hotel['tab']}_description"]);
		}
	}
	else {
	  $template->assign('pagetitle', $hotel['hotel']['hotel_title']);
	  $template->assign('keywords', $hotel['hotel']['hotel_overview_keywords']);
	  $template->assign('description', $hotel['hotel']['hotel_overview_description']);
	}

I would have used a switch block. I just find them easier to mentally grasp, especially with my few remaining live brain cells.


switch ($hotel['tab']){
	case 'accommodation':
		$template->assign('pagetitle', $hotel['hotel']['hotel_accommodation_title']);
		$template->assign('keywords', $hotel['hotel']['hotel_accommodation_keywords']);
		$template->assign('description', $hotel['hotel']['hotel_accommodation_description']);
		break;
	case 'facilities':       
		$template->assign('pagetitle', $hotel['hotel']['hotel_facilities_title']);
		$template->assign('keywords', $hotel['hotel']['hotel_facilities_keywords']);
		$template->assign('description', $hotel['hotel']['hotel_facilities_description']);
		break;
	//etc... 
	default:		
		$template->assign('pagetitle', $hotel['hotel']['hotel_title']);
		$template->assign('keywords', $hotel['hotel']['hotel_overview_keywords']);
		$template->assign('description', $hotel['hotel']['hotel_overview_description']);
}

You could assign the default keywords to start, and get them out of that php else. In addition, just have ‘map’ remap the variable… and you can shorten the code considerably. Just like this.


// Assign Defaults
$template->assign('pagetitle', $hotel['hotel']['hotel_title']);
$template->assign('keywords', $hotel['hotel']['hotel_overview_keywords']);
$template->assign('description', $hotel['hotel']['hotel_overview_description']);

if (isset($hotel['tab'])) {
    
    $tab = $hotel['tab']; /* Create a copy to work with.. incase you are using the real $hotel['tab'] var elsewhere */

    // Remap to this one
    if ($tab == 'map') {
        $tab = 'resortplan';
    }
    

    if (isset($hotel['hotel']["hotel_{$tab}_title"])) {
        $template->assign('pagetitle', $hotel['hotel']["hotel_{$tab}_title"]);
        $template->assign('keywords', $hotel['hotel']["hotel_{$tab}_keywords"]);
        $template->assign('description', $hotel['hotel']["hotel_{$tab}_description"]);
    }
}

i’m with cydewaze its easier mentally to use case switch than a bunch of else if statements