Godfrey Posted August 29, 2013 Share Posted August 29, 2013 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 More sharing options...
adrian Posted August 30, 2013 Share Posted August 30, 2013 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. 1 Link to comment Share on other sites More sharing options...
Godfrey Posted August 30, 2013 Author Share Posted August 30, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now