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

    • BrowserOS 0.46.0 by Razvan Serea BrowserOS is a free, open-source Chromium-based browser that runs AI agents natively, offering a smarter, more productive browsing experience. It supports Chrome extensions and integrates AI agents to automate tasks, fill forms, and streamline workflows. Your data stays on your computer: you can use your own API keys or run local models via Ollama, making it a privacy-first alternative to tools like Perplexity, Comet, or Dia. With built-in productivity tools and app integrations, BrowserOS boosts efficiency while keeping control firmly in your hands. Being Chromium-based, BrowserOS lets you effortlessly import your bookmarks, passwords, and Chrome extensions in just a few clicks. BrowserOS works with OpenAI GPT models, Anthropic Claude, Google Gemini, and local AI models via Ollama or LMStudio. You can use your own API keys and effortlessly switch between providers. BrowserOS Agent Your AI productivity assistant that organizes and manages your browsing effortlessly Quickly list, group, or close tabs Save and resume browsing sessions Search your history and organize bookmarks Switch instantly to the tab you need BrowserOS Navigator – Automate web tasks with ease Navigate websites and search automatically Interact with pages without manual effort Handle repetitive tasks in seconds What makes BrowserOS special Feels like home - same familiar interface as Google Chrome, works with all your extensions AI agents that run on YOUR browser, not in the cloud Privacy first - bring your own keys or use local models with Ollama. Your browsing history stays on your computer Open source and community driven - see exactly what's happening under the hood MCP store to one-click install popular MCPs and use them directly in the browser bar (coming soon) Built-in AI ad blocker that works across more scenarios! BrowserOS 0.46.0 changelog: Run Claude Code & Codex right in your browser — We've extended the agent harness to bring full coding agents into BrowserOS. Claude Code and Codex now come bundled and plug straight into the assistant, so you can drive your browser with the agent — and the subscription — you already use. A brand new experience — A redesigned new tab, a calmer composer, and a rebuilt command center for switching between agents. The whole assistant is cleaner, faster to reach, and easier to live in. New MCP tools — We rebuilt the browser tool surface from the ground up — a tighter, more reliable set of tools for agents to drive the browser. Plus one-click install of BrowserOS as an MCP server into the agents you already run, with automatic URL sync. Chromium 148 — Updated to the latest Chromium base with all recent upstream fixes and security patches. Streamlined — We've pulled back a few features that weren't getting much use — Skills, Soul, and Memory — so we can focus and ship better versions of them soon. Download: BrowserOS 0.46.0 | 181.0 MB (Open Source) Download: BrowserOS for macOS | 485.0 MB Links: BrowserOS Homepage | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Microsoft finally admits its default Windows 11 25H2, 24H2 action broke key legacy component by Sayan Sen Microsoft last week released Windows 11 KB5094126 and KB5093998 as the latest Patch Tuesday updates. Following that the company also published the accompanying dynamic updates under KB5094149, KB5095971, and KB5094156. So far the company has acknowledged two known issues that have popped up after the release which include bugged-out Office apps as well as the Recycle Bin; though there could be more at play too. Speaking of bugs and issues, Microsoft seems to have finally acknowledged a problem that probably has been around for close to a year. That's because back in July of 2025 the company made a default change to the latest Windows 11 versions, wherein it switched to JScript9Legacy on Windows 11 24H2 and later releases. Hence following the release of version 25H2 in October 2025, JScript9Legacy also remained default-enabled. As a result there has been a compatibility issue ever since then. For those wondering, by switching to JScript9Legacy Microsoft intended to improve the security of modern Windows PCs by reducing vulnerabilities tied to legacy scripting like cross-site scripting (XSS), among others. XSS exploits can allow cyber-attackers to attach malicious code onto legitimate websites and use them to execute the code when a potential victim loads such a website. Hence the new JScript9Legacy engine enforced stricter execution policies and improved object handling, which should help mitigate such attacks. Microsoft today has published a new support article detailing the problem. Neowin spotted it while browsing. The company says that JScript global definitions and execution context may fail to persist across scripts, potentially breaking older dependent apps and web-based components that relied on this legacy behavior. In the article Microsoft has confirmed that the issue stems from its move away from the older jscript9.dll engine in favor of jscript9legacy.dll. As mentioned above, while the newer engine was designed to address vulnerabilities and strengthen security it also changes how JScript handles execution context. As a result functions and definitions loaded by one script could no longer remain available to subsequent scripts once execution ended. The company notes that some applications worked correctly on earlier Windows versions because the older JScript engine automatically retained global definitions and execution state between scripts. Under the newer model though that behavior is disabled by default causing certain legacy workloads and polyfill-dependent scripts to fail. Microsoft says it addressed the problem via the KB5077241 update though the fix had not been enabled automatically in the following updates. As such admins must explicitly turn on persistent JScript execution context using a Registry setting that the tech giant shared today. The configuration can be applied to individual processes or system-wide through the FEATURE_ENABLE_PERSISTENCE registry key. The steps have been outlined below: Run the following command to create the feature control registry key: reg add "HKLM\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ENABLE_PERSISTENCE" Under this key, create a new DWORD (32-bit) value. Configure the value as follows: To enable persistence for specific processes only: Set the value to 1 for each target process name. To enable persistence for all processes: Add * as the key name and set its value to 1. You can find the official support article here on Microsoft's website.
    • The possibility that milk gathers back into a glass implies that gravity can be 'reversed'.
    • VidCoder 12.20 by Razvan Serea  VidCoder is a DVD/Blu-ray ripping and video transcoding application for Windows. It uses HandBrake as its encoding engine. Calling directly into the HandBrake library gives it a more rich UI than the official HandBrake Windows GUI. VidCoder can rip DVDs but does not defeat the CSS encryption found in most commercial DVDs. You’ll need the NET 8 Desktop Runtime. If you don’t have it, VidCoder will prompt you to download and install it. The Portable version is self-contained and does not require any .NET Runtime to be installed. You do not need to install HandBrake for VidCoder to work. Feature list: Multi-threaded MP4, MKV containers Completely integrated encoding pipeline: everything is in one process and no huge intermediate temporary files H.264, H.265, MPEG-4, MPEG-2, VP8, Theora video Hardware-accelerated encoding with AMD VCE, Nvidia NVENC and Intel QuickSync AAC, MP3, Vorbis, AC3, FLAC audio encoding and AAC/AC3/MP3/DTS/DTS-HD passthrough Target bitrate, size or quality for video 2-pass encoding Decomb, detelecine, deinterlace, rotate, reflect, chroma smooth, colorspace filters Powerful batch encoding with simultaneous encodes Customizable Pickers to automatically pick audio and subtitle tracks, destination, titles and more Instant source previews Creates small encoded preview clips Pause, resume encoding VidCoder 12.20 changes: Updated HandBrake core to 1.11.2. Download: VidCoder 12.20 | 47.0 MB (Open Source) Download: Portable VidCoder 12.19 | 89.3 MB Link: VidCoder Home Page | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Jordan Smith earned a badge
      Week One Done
    • Reacting Well
      BizSAR earned a badge
      Reacting Well
    • First Post
      AndreaB earned a badge
      First Post
    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      590
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      76
    4. 4
      Michael Scrip
      73
    5. 5
      Steven P.
      66
  • Tell a friend

    Love Neowin? Tell a friend!