Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/223218-need-help-with-array/
Share on other sites

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.

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?

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?

Like I asked, do you have the $lang array created in the included file?

 

Oh, yes yes... i found the bug. Thanks a lot, :P.

 

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, :D!

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.