alternative mysql query

is there a other way to write this?

[php]

		mysql_query("UPDATE $lang_ro SET val = '$nav_home' WHERE var = 'nav_home'");
		mysql_query("UPDATE $lang_ro SET val = '$nav_project' WHERE var = 'nav_project'");
		mysql_query("UPDATE $lang_ro SET val = '$nav_forum' WHERE var = 'nav_forum'");
		mysql_query("UPDATE $lang_ro SET val = '$nav_db' WHERE var = 'nav_db'");
		mysql_query("UPDATE $lang_ro SET val = '$nav_contact' WHERE var = 'nav_contact'");
		mysql_query("UPDATE $lang_ro SET val = '$nav_faq' WHERE var = 'nav_faq'");

[/php]

Maybe,

Can show us what kind of data you entering?
What’s the value of $lang_ro is it dynamic?

well what i did there was a db where i store all the variables and values (valeus are all text ) witch i use for language for a site, i got 2 tables. lang_ro and lang_hu each table has 4 columns id,var,valand page in var i store the variable name, in val the content of that variable and in page the page that they belong (a keyword of the page); so to retrive data from the db i do it like this
[php]if (isset($_SESSION[‘lang’])) {
if ($_SESSION[‘lang’] == ‘ro’) {

	$sql = mysql_query("SELECT var, val FROM $lang_ro");
	$lang = array();
	while ( $row = mysql_fetch_assoc($sql)) {
		$lang[$row['var']] = $row['val'];
	}
	

}else if ($_SESSION['lang'] == 'hu') {

	$sql = mysql_query("SELECT var, val FROM $lang_hu");
	$lang = array();
	while ( $row = mysql_fetch_assoc($sql)) {
		$lang[$row['var']] = $row['val'];
	}
	

}

}else{

$sql = mysql_query("SELECT var, val FROM $lang_ro");
$lang = array();
while ( $row = mysql_fetch_assoc($sql)) {
	$lang[$row['var']] = $row['val'];
}

}[/php]

now the reason i made the table like that is: i made an admin panel where the admin can enter, he selects the page then the language he wants to edit and after he selected everything, all the the values from the page he selected will appear in inputs and he can easily edit what he wants and then click save and update the db only in the fields he edited.

P.S. now i try to make it in such way that it will check to see if the array that is needed was already retrieved from the DB so it wont retrieve it every-time when the page changes

you can leave the below code they way it is it’d fine.
[php]
mysql_query(“UPDATE $lang_ro SET val = ‘$nav_home’ WHERE var = ‘nav_home’”);
mysql_query(“UPDATE $lang_ro SET val = ‘$nav_project’ WHERE var = ‘nav_project’”);
mysql_query(“UPDATE $lang_ro SET val = ‘$nav_forum’ WHERE var = ‘nav_forum’”);
mysql_query(“UPDATE $lang_ro SET val = ‘$nav_db’ WHERE var = ‘nav_db’”);
mysql_query(“UPDATE $lang_ro SET val = ‘$nav_contact’ WHERE var = ‘nav_contact’”);
mysql_query(“UPDATE $lang_ro SET val = ‘$nav_faq’ WHERE var = ‘nav_faq’”);
[/php]

however to avoid retrieving the data from database multiple times you can set a condition so the data will retrieved once.

for example when the data is retrieved you can set a cookie or a session and only retreive if the session is not yet set.

the below code
[php]
if ((isset($_SESSION[‘lang’])) && (!isset($_SESSION[‘retrieved’])))
{
if ($_SESSION[‘lang’] == ‘ro’) {

	$sql = mysql_query("SELECT var, val FROM $lang_ro");
	$lang = array();
	while ( $row = mysql_fetch_assoc($sql)) {
		$lang[$row['var']] = $row['val'];
	}
	
	$_SESSION['retrieved'] = "already retrieved once":

}else if ($_SESSION['lang'] == 'hu') {

	$sql = mysql_query("SELECT var, val FROM $lang_hu");
	$lang = array();
	while ( $row = mysql_fetch_assoc($sql)) {
		$lang[$row['var']] = $row['val'];
	}
	
	$_SESSION['retrieved'] = "already retrieved once":

}

}else{

$sql = mysql_query("SELECT var, val FROM $lang_ro");
$lang = array();
while ( $row = mysql_fetch_assoc($sql)) {
	$lang[$row['var']] = $row['val'];
}

}
[/php]

finally once you update the records from admin user you can happlu unset that session so that if he he wants to edit something else the scriipt will load all is info again

[php]
unset($_SESSION[‘retrieved’]);
[/php]

don’t forget to use session_start();

cheers

thank you for your help

I’m curious to know if it worked?

can you let us know if it did work ?

well my boss told me to use a CMS instead, did not have the chance to see if it works sorry guys

haha well get started and good luck

post when you get stuck

i sure will :slight_smile: i am new to web developing and i learned a lot from u guys :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service