Jump to content

Cleaner way of doing this?


ZionBludd
 Share

Recommended Posts

Hi guys. I am wanting to do breadcrumbs based on the page matching a condition. However, I'm wondering if there is a way for me to reduce the number of if else statements. Here is my code.


<? if ($page->city == 'Auckland' {
            $setcity = 'Auckland';  
             echo "<span class='topNav'> <a class='' href='/gyms' title='Gyms'>  <i class='fa fa-chevron-left'></i></span>";
             echo "All Gyms</a>";
             echo "<span class='topNav'> <a class='' href='/gyms/auckland-gyms/' title='Auckland Gyms'> <i class='fa fa-chevron-left'></i></span>";
             echo "Auckland Gyms</a>"; 
             echo "<span class='topNav'> <strong> <i class='fa fa-chevron-right'></i></span>";
             echo $page->title;
             echo "</strong></small>";

           }
   elseif ($page->city == 'Christchurch') { 
             $setcity = 'Christchurch';
             echo "<span class='topNav'> <a id='$page->id' class='' href='/gyms' title='Gyms'>  <i class='fa fa-chevron-left'></i></span>";
             echo "All Gyms</a>";
             echo "<span class='topNav'> <a id='$page->id' class='' href='/gyms/christchurch-gyms/' title='Christchurch Gyms'> <i class='fa fa-chevron-left'></i></span>";
             echo "Christchurch Gyms</a>"; 
             echo "<span class='topNav'id='$page->id' > <strong> <i class='fa fa-chevron-right'></i></span>";
             echo $page->title;
             echo "</strong></small>";

           }
 
?>
Where $setCity is used
 
<span class="pull-right hidden-sm hidden-xs">
<? // Populate a random gym in the same city
 $getRandomPages = $pages->find("template=listing-gym, city=$setcity, sort=random, limit=1")->first();

if($getRandomPages->id) {

    $q = $getRandomPages->title; // example field
    $a = $getRandomPages->url; // example field
    echo " <a title='$q' href='$a'><i class='fa fa-random'></i> Random Gym</a>\n";

}

?>

Essentially all I am wanting to do is print a breadcrumb trail if the city of the listing is equal to the city name. 

Edit: Sorry... Was not on my main machine when posting the original code. Updated.

Link to comment
Share on other sites

Yeah, answers will vary depending on your page and template structures. If there is a city template (each city has it's own unique page), then you could pull all or some of the cities using something $pages->find("template=cities") and then just echo out that city's url using a foreach loop.  I believe the skyscrapers profile has a pretty good example of that. Otherwise, you could use the good ol' switch statement instead of a bunch of if/elses.  Give us more details and we'll be able to help you further.

Link to comment
Share on other sites

maybe something like this?

if($page->city) {
	
	$setCity = $page->city;
	$cityURL = $sanitizer->pageName($page->city);
	
	echo "
		<span class='topNav'> <a class='' href='/gyms' title='Gyms'>  <i class='fa fa-chevron-left'></i></span> All Gyms</a>
		<span class='topNav'> <a class='' href='/gyms/{$cityURL}-gyms/' title='{$setCity} Gyms'> <i class='fa fa-chevron-left'></i></span> $setCity Gyms</a>
		<span class='topNav'> <strong> <i class='fa fa-chevron-right'></i></span> {$page->title} </strong>
	";

	$randomGym = $pages->find("template=listing-gym, city=$setCity")->getRandom();
	if($randomGym->id) echo " <a title='{$randomGym->title}' href='{$randomGym->url}'><i class='fa fa-random'></i> Random Gym</a>\n";
	
}
Link to comment
Share on other sites

maybe something like this?

if($page->city) {
	
	$setCity = $page->city;
	$cityURL = $sanitizer->pageName($page->city);
	
	echo "
		<span class='topNav'> <a class='' href='/gyms' title='Gyms'>  <i class='fa fa-chevron-left'></i></span> All Gyms</a>
		<span class='topNav'> <a class='' href='/gyms/{$cityURL}-gyms/' title='{$setCity} Gyms'> <i class='fa fa-chevron-left'></i></span> $setCity Gyms</a>
		<span class='topNav'> <strong> <i class='fa fa-chevron-right'></i></span> {$page->title} </strong>
	";

	$randomGym = $pages->find("template=listing-gym, city=$setCity")->getRandom();
	if($randomGym->id) echo " <a title='{$randomGym->title}' href='{$randomGym->url}'><i class='fa fa-random'></i> Random Gym</a>\n";
	
}

Thanks a lot! This reduced alot of lines of code :)

And for those interested, this is what the end result was:

http://imgur.com/Pip0BMu

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...