Jump to content

$language->title not displaying?


Godfrey
 Share

Recommended Posts

This is probably horrendous code, but why is it that $language->title in the following code block won't display?

$savedLanguage = $user->language; 
 
   if( $savedLanguage->id == $languages->get("default")->id){
      $user->language = $languages->get("english"); 
  } else {
    $user->language = $languages->get("default");
  }
 
  echo "<li><a href='$page->url'>$language->title: $page->title</a></li>";
  
$user->language = $savedLanguage;

This probably isn't the most elegant code, but I wanted to simply switch between two languages. 
However, if I use the given (default) code below, $language-title is displayed. 
 

// remember what language is set to
$savedLanguage = $user->language; 
 
foreach($languages as $language) {
 
  // if user is already viewing the page in this language, skip it
  if($language->id == $savedLanguage->id) continue; 
 
  // if this page isn't viewable (active) for the language, skip it
  if(!$page->viewable($language)) continue;
 
  // set the user's language, so that the $page->url and any other
  // fields we access from it will be reflective of the $language
  $user->language = $language; 
 
  // output a link to this page in the other language
  echo "<li><a href='$page->url'>$language->title: $page->title</a></li>";
}
// restore the original language setting 
$user->language = $savedLanguage; 


 

This seems really weird, because the echo statements are exactly identical. For both, $page->title works. I'm also curious as to how $page->title and $language->title are printed even though they are not escaped from the double quotes in the echo statement...

Thanks for any help! This is probably a rookie error, I just can't seem to find it... 

Link to comment
Share on other sites

Your first non-working code block doesn't foreach through the $languages array and so $language is probably never defined.

As for the variables being printed in the double quotes - that is how php works. In double quotes it works as is. In single quotes you need to do something like:

echo '<li><a href="'.$page->url.'">'.$language->title.': '.$page->title.'</a></li>';

Hope that helps.

  • Like 1
Link to comment
Share on other sites

Ah, I knew about the single quote method but didn't know about the double quotes.

Yep, this helped! Indeed I got it to work after changed $language-title to $user->language->title. I know I missed something. Many thanks adrian. 

 

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