KingdomX Posted January 2, 2011 Share Posted January 2, 2011 OK, so i have this class: <?php class Lang extends Db { var $lang = array(); var $chosen_lang; var $lang_dir; var $img_dir; function setLang ($chosen_lang) { $this->lang_dir = $_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'; $this->img_dir = $_SERVER['DOCUMENT_ROOT'].'/gl_imgs/'.$this->chosen_lang.'/'; $this->chosen_lang = $chosen_lang; unset($chosen_lang); } public function loadLang ($file){ if( file_exists( $_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'.$file.'.php' ) ) { include( $_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'.$file.'.php' ); $this->lang[$file] = array(); foreach ($lang as $k => $v) { $this->lang[$file][$k] = $v; } } unset($lang, $file, $k, $v); } } ?> And on the index page of the website i have this: <?php include($_SERVER['DOCUMENT_ROOT'].'classes/class_Db.php'); include($_SERVER['DOCUMENT_ROOT'].'classes/class_Lang.php'); $lang = new Lang; $lang->setLang('en-us'); $lang->loadLang('website_account'); $lang->loadLang('website_main-menu'); ?> My question is: why the script won't work if i add the command: $lang->loadLang('website_main-menu'); If i only leave the following, the script works as intended: $lang = new Lang; $lang->setLang('en-us'); $lang->loadLang('website_account'); After some debugging, i found out that the script executes the first loadLang command on the 'website_account', but when it executes the second loadLang command ('website_main-menu'), it freezes at this line on the class: $this->lang[$file] = array(); Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/ Share on other sites More sharing options...
requinix Posted January 2, 2011 Share Posted January 2, 2011 You quoted yourself... from another forum? Why? All it shows it that you don't trust those people. It's been 2.5 hours since you've posted, thus >2.5 hours since the one you're quoting, so I'm thinking you got an answer already on one of those other sites. Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/#findComment-1154015 Share on other sites More sharing options...
KingdomX Posted January 2, 2011 Author Share Posted January 2, 2011 o.O Not really... Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/#findComment-1154020 Share on other sites More sharing options...
jcbones Posted January 2, 2011 Share Posted January 2, 2011 Did you set the variable $lang in your included file? Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/#findComment-1154024 Share on other sites More sharing options...
Anti-Moronic Posted January 3, 2011 Share Posted January 3, 2011 I think the person above is reading far too deep into this simple copy and paste Lots of people post on multiple forums, nothing to do with trust - more to do with increases your responses. You might also try stackoverflow.com! Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/#findComment-1154027 Share on other sites More sharing options...
KingdomX Posted January 3, 2011 Author Share Posted January 3, 2011 Did you set the variable $lang in your included file? Hmmmm, i'm not sure what u're asking. I have the $lang variable both inside and outside the classes just like the codes above... why? When the script tries to set the second array, it stops executing, but there's no error. I'm trying to set the variables as follows: Setting $lang->lang['website_account']['register'] on the $lang->loadLang('website_account') run... And setting $lang->lang['website_main-menu']['home'] on the $lang->loadLang('website_main-menu') run... Any ideas of what's going on? Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/#findComment-1154032 Share on other sites More sharing options...
jcbones Posted January 3, 2011 Share Posted January 3, 2011 public function loadLang ($file){ if( file_exists( $_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'.$file.'.php' ) ) { //if the file exists include( $_SERVER['DOCUMENT_ROOT'].'/lang/'.$this->chosen_lang.'/'.$file.'.php' ); //include the file. $this->lang[$file] = array(); //create a new entry into the existing class array $lang. foreach ($lang as $k => $v) { //$lang is NOT set inside this function, it must come from the included file, and it MUST be an array. $this->lang[$file][$k] = $v; //Appends the $lang array, to the class $lang array. } } unset($lang, $file, $k, $v); //destroy the $lang array, and the current $k and $v. } Like I asked, do you have the $lang array created in the included file? Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/#findComment-1154035 Share on other sites More sharing options...
KingdomX Posted January 3, 2011 Author Share Posted January 3, 2011 Like I asked, do you have the $lang array created in the included file? Oh, yes yes... i found the bug. Thanks a lot, . The problem was that the included file was like this: <?php $lang = array( 'home' => 'Home' 'contact' => 'Contact' 'aboutus' => 'About Us' 'games' => 'Games' 'movies' => 'Movies' 'tv' => 'TV' 'music' => 'Music' ); ?> The problem was that i forgot to separate the indexes with the comma, that was why one file was working (had commas) and the other wasn't (no commas). It should look like this: <?php $lang = array( 'home' => 'Home', 'contact' => 'Contact', 'aboutus' => 'About Us', 'games' => 'Games', 'movies' => 'Movies', 'tv' => 'TV', 'music' => 'Music' ); ?> Thanks, ! Quote Link to comment https://forums.phpfreaks.com/topic/223218-need-help-with-array/#findComment-1154053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.