Jump to content

Persistant MySQL Data Across Multiple Pages


JustinK101

Recommended Posts

I am currently storing all my applications language, i.e. all button text, body text, etc in MySQL in the form of:

 

key - varchar(255)         value - varchar(255)

'first_name'                   'First Name'

'error_occured'               'An Error Has Occured. Click Here To Continue...'

'submit'                         'Submit!'

 

I want a way to load up all the values from this table ONCE, and store everything persistant, meaning I don't have to query and load it again every page, since the language data will barely change. Then I would like to be able to reference things like:

 

$lang->first_name

$lang->submit

 

How do I go about doing this? Thanks.

I think perhaps, here is what I am thinking, but I am not sure this will work.

 

$language = array();

$sql = "SELECT key, value FROM languages WHERE culture = 'en_us' ORDER BY key ASC";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($result)) {
   array_push($language, $row->key => $row->value);
}

$_SESSION['language'] = $language;

 

 

Well technically this data inst static because as I add new features and even translate it, it needs to get updated.  I am curious though, how do you mantain classes that are static or constant across multiple pages? Do you simply store them in a session as well?

Ok bear with me, here is a class I came up with, does it make sense what I am doing?

 

class Language {
	private lang_array;

	public function Language() {
		$this->lang_array = array();
	}

	public function add($k, $v) {
		$this->lang_array[$k] = $v;
	}

	public function get($k) {
		return ($this->lang_array[$k]);
	}
}

Here is my final class, I think this should work nicely.

 

class Language {
	private $lang_array;

	public function Language($culture) {
		$this->lang_array = array();

		$sql = "SELECT key, value FROM languages WHERE culture = '" . $culture . "' ORDER BY key ASC";
		$result = mysql_query($sql) or trigger_error(mysql_error(), E_USER_ERROR);

		while($row = mysql_fetch_assoc($result)) {
			add($row['key'], $row['value']);
		}
	}

	public function add($k, $v) {
		$this->lang_array[$k] = $v;
	}

	public function get($k) {
		return ($this->lang_array[$k]);
	}

	public function getArray() {
		return ($this->lang_array);
	}
}

Archived

This topic is now archived and is closed to further replies.

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