• 0

Foreach language add value


Question

I am trying to return each description the user has posted for each language the site supports that is active.

I have the data posting into the database ok but there is a function I need, for validating, if the user has disabled there javascript on there browser.

This piece of code checks which languages are active on the site.

/* Detect active languages on the site. */
$qLang = mysql_query("SELECT * FROM languages WHERE `is_active`='Y'") or die("ERROR: ".mysql_error());
while($languages = mysql_fetch_array($qLang)){
	$Lang = array($languages['lang_code'] => $languages['lang_code']);
	$lang = $Lang[$languages['lang_code']];
	$description[$lang] = mysql_real_escape_string($_POST['description_'.$lang.'']);
	$convert = array("\r\n", "\n", "\r", "  "); /* Remove bad code to a friendly text */
	$replace = " "; /* Don't replace anything just leave it blank to remove bad content. */
	$description[$lang] = str_replace($convert, $replace, $_POST['description_'.$lang.'']); /* Processes \r\n's first so they aren't converted twice. */
	$description[$lang] = strip_tags($description[$lang]," "); /* Removes any HTML Tags. */
	if(!empty($description[$lang])){ $addDescription[$lang] = "description_$lang=$description[$lang]&"; } /* If user input for description is not empty then description in that language will be added. */
}

The top part works for the other functions in my script, but this bottom piece of code does not.

foreach($Lang as $languages['lang_code'] => $description[$lang]){
	$addDescription = $addDescription[$lang];
}

I only get 1 of them returning whether or not the user has entered a description for another language.

I looks like this when it is echoed: description_en=description text goes here&

I need it to be able to echo more than one for each language the site has provided like so: description_en=description text goes here&description_fr=texte de la description va ici&

If any one can help me figure this out I would mostly appreciate it.

Thank you.

Link to comment
https://www.neowin.net/forum/topic/925504-foreach-language-add-value/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Well, you are setting your addDescription variable to addDescription[lang] which means at the end of the loop, since you don't do anything else with it that we can see, it's going to error when it gets to the next language since it's no longer an array. Change the variable name you're setting it to in that array and it will be fine.

  • 0

I have made some changes to my code as you suggested.

/* Detect active languages on the site. */
$qLang = mysql_query("SELECT * FROM ".TBL_LANGUAGE." WHERE `is_active`='Y'") or die("ERROR: ".mysql_error());
while($languages = mysql_fetch_array($qLang)){
	$Lang = array($lang => $languages['lang_code']);
	$language = $Lang[$lang];
	$description[$language] = mysql_real_escape_string($_POST['description_'.$language.'']);
	$convert = array("\r\n", "\n", "\r", "  "); /* Remove bad code language to a friendly text */
	$replace = " "; /* Don't replace anything just leave it blank to remove bad content. */
	$description[$language] = str_replace($convert, $replace, $_POST['description_'.$language.'']); /* Processes \r\n's first so they aren't converted twice. */
	$description[$language] = strip_tags($description[$language]," "); /* Removes any HTML Tags. */
	foreach($Lang as $key => $language){
	/* If user input for description is not empty then description in that language will be added. */
		if(!empty($description[$language])){
			$addDescription[$language] = "description_$language=$description[$language]&";
		}
	}
}

/* For each description added, add data to the header before returning. */
foreach($Lang as $languages['lang_code'] => $description[$language]){
	$addDescription = $addDescription[$language];
}

Now i just need each $addDescription to show in the header line so that the data is returned back to the user if they have missed something required in my form.

This is how it is for my header line.

header("Location: post.php?$addDescription"); exit;

It returns only one of them so all I get in the address bar is this: post.php?description_en=description goes here&

I need it to be able to display the others if the user has entered it in the form like so: post.php?description_en=description goes here&description_fr=texte de la description va ici&

  • 0

Like I said, look at this line: $addDescription = $addDescription[$language];

Also, I'm fairly certain there's something strange with your foreach as I'm not even sure what that's doing.

The code below detects the first part of the code.

/* For each description added, add data to the header before returning. */
foreach($Lang as $languages['lang_code'] => $description[$language]){
        $addDescription = $addDescription[$language];
}

The code does work for detecting user input for each description in the languages they have filled it for. I just can't transfer each description to the header if there is more than 1. That is what I don't understand. You are not giving me much to work with as you are not explaning what I need to do.

  • 0

The code below detects the first part of the code.

/* For each description added, add data to the header before returning. */
foreach($Lang as $languages['lang_code'] => $description[$language]){
        $addDescription = $addDescription[$language];
}

The code does work for detecting user input for each description in the languages they have filled it for. I just can't transfer each description to the header if there is more than 1. That is what I don't understand. You are not giving me much to work with as you are not explaning what I need to do.

You're seeting $addDescription to a value that it had inside it. After that, it's no longer an array...it's a string with whatever the first language was. You're overwriting the contents of the variable. Change it on the left side of the = to another variable name and you won't overwrite it. Also, you would need to append it. Something like:

$descriptions .= $addDescription[$language]

Note the . in front of the equals. That's the same as:

$descriptions = $descriptions . $addDescription[$language]

The reason I think your foreach is weird is because you don't see to do anything with the $x => $y part. You could easily put that back in the while loop.

Change it to something more like:

/* Detect active languages on the site. */
$qLang = mysql_query("SELECT * FROM ".TBL_LANGUAGE." WHERE `is_active`='Y'") or die("ERROR: ".mysql_error());
$descriptions = "";
while($languages = mysql_fetch_array($qLang)){
        $language = $languages['lang_code'];
        $description[$language] = mysql_real_escape_string($_POST['description_'.$language.'']);
        $convert = array("\r\n", "\n", "\r", "  "); /* Remove bad code language to a friendly text */
        $replace = " "; /* Don't replace anything just leave it blank to remove bad content. */
        $description[$language] = str_replace($convert, $replace, $_POST['description_'.$language.'']); /* Processes \r\n's first so they aren't converted twice. */
        $description[$language] = strip_tags($description[$language]," "); /* Removes any HTML Tags. */
        /* If user input for description is not empty then description in that language will be added. */
        if(!empty($description[$language])){
                $descriptions .= "description_{$language}={$description[$language]}&";
        }
}
header("Location: post.php?{$descriptions}"); exit;

I'm not sure what you were trying to accomplish with the $Lang array variable. But it ends up being pointless.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.