• 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

    • Weekend PC Game Deals: Cyberpunk 2077, Split Fiction, Sonic Racing, and more by Pulasthi Ariyasinghe Weekend PC Game Deals is where the hottest gaming deals from all over the internet are gathered into one place every week for your consumption. So kick back, relax, and hold on to your wallets. The Epic Games store brought along two games from wildly different genres this week for PC gamers to claim. Robobeat is a rhythm-based action game that lets you become a bounty hunter that can wall run, slide, and bunny hop around his opponents. All you have to do is stick to the beat for the built-in or custom songs. Next, Citizen Sleeper is a sci-fi RPG adventure taking place in a ruined space station. It uses tabletop RPG-inspired elements like dice rolls and timers to change up how players approach its activities, factions, and storylines. The Citizen Sleeper and Robobeat giveaways end on June 25. On the same day, RollerCoaster Tycoon 3 and Voidwrought will become the next freebies. The bundle space expanded with two more collections from Humble this week too. The June 2unes bundle is up first, carrying plenty of rhythm games. This carries Kill the Music and Rhythm Witch in the $5 starting tier, followed by Trombone Champ, Spin Rhythm XD, and Thumper in the $7 tier. Paying at least $12 gets you the complete bundle, which adds on Kalpa: Cosmic Symphony, Everhood 2, NOISZ, and Sixtar Gate: StarTrail. The next bundle is for virtual reality fans. This carries Among Us 3D: VR and Zero Caliber VR for $10. The next tier brings in Tactical Assault VR, Ancient Dungeon, and Arizona Sunshine Remake for $15. VTOL VR, Zero Caliber 2 Remastered, Metro Awakening, and Thief VR land to finish things off for $18. Free Events It's a big week for free event fans, as Valve kicked off another one of its Next Fest events. This one carries thousands of gameplay slices from upcoming indie games The promotion is set to run until June 22. Standard free events are also ongoing this weekend. This includes the sci-fi grand strategy experience Stellaris from Paradox and the hit SEGA management game Two Point Museum. Asymmetric multiplayer horror title Dead by Daylight and the hit mech shooter MechWarrior 5: Mercenaries are also free-to-play over the weekend. Big Deals The Steam Summer Sale is a week away from launch, but there are plenty of publishers already putting their wares on sale to prepare for the event. Here's our hand-picked big deals list for this weekend: Battlefield 6 – $34.99 on Steam Sonic Racing: CrossWorlds – $34.99 on Steam Split Fiction – $32.49 on Steam Arma Reforger – $27.99 on Steam Sniper Elite: Resistance – $24.99 on Steam DayZ – $22.49 on Steam Two Point Museum – $20.09 on Steam Atomfall – $19.99 on Steam No More Room in Hell 2 – $19.49 on Steam Cyberpunk 2077 – $17.99 on Steam Sonic Frontiers – $17.99 on Steam Dinkum – $15.99 on Steam Stellaris – $14.99 on Steam Hi-Fi RUSH – $14.99 on Steam My Little Puppy – $14.99 on Steam FINAL FANTASY XII THE ZODIAC AGE – $14.99 on Steam SONIC X SHADOW GENERATIONS – $14.99 on Steam EA SPORTS FC 26 – $13.99 on Steam STAR WARS Jedi: Survivor – $13.99 on Steam FINAL FANTASY VII REMAKE INTERGRADE – $13.99 on Steam FINAL FANTASY XV – $13.99 on Steam It Takes Two – $11.99 on Steam FINAL FANTASY X/X-2 HD Remaster – $11.99 on Steam Axiom Verge 2 – $9.99 on Steam [REDACTED] – $9.99 on Steam Sniper Elite 5 – $9.99 on Steam Holdfast: Nations At War – $9.99 on Steam Arma 3 – $8.99 on Steam The Callisto Protocol – $8.99 on Steam A Way Out – $8.99 on Steam LIGHTNING RETURNS: FINAL FANTASY XIII – $7.99 on Steam MechWarrior 5: Mercenaries – $7.49 on Steam Slackers - Carts of Glory – $7.14 on Steam MIMESIS – $6.99 on Steam Need for Speed Unbound – $6.99 on Steam FINAL FANTASY XIII – $6.39 on Steam Sniper Elite 4 – $5.99 on Steam Tyranny – $5.99 on Steam Immortals of Aveum – $5.99 on Steam Far Cry 3 – $4.99 on Steam Zombie Army 4: Dead War – $4.99 on Steam Sonic & All-Stars Racing Transformed Collection – $4.99 on Steam Mass Effect Legendary Edition – $4.79 on Steam Titanfall 2 – $4.49 on Steam SimCity 4 Deluxe Edition – $3.99 on Steam Far Cry 3 - Blood Dragon – $3.74 on Steam Wreckfest – $2.99 on Steam Crime Boss: Rockay City – $1.99 on Steam theHunter: Call of the Wild – $1.99 on Steam The Saboteur – $1.99 on Steam Battlefield 1 – $1.99 on Steam Sonic Mania – $1.99 on Steam Golf With Your Friends – $1.49 on Steam Sid Meier's Alpha Centauri Planetary Pack – $0.99 on Steam Dungeon Keeper 2 – $0.99 on Steam Populous: The Beginning – $0.99 on Steam Citizen Sleeper – $0 on Epic Store ROBOBEAT – $0 on Epic Store DRM-free Specials The DRM-free store GOG has already kicked off its own summer sale. Here are some highlights: S.T.A.L.K.E.R. 2: Heart of Chornobyl - $41.99 on GOG Indiana Jones and the Great Circle - $41.99 on GOG Cronos: The New Dawn - $35.99 on GOG SILENT HILL 2 - $34.99 on GOG SILENT HILL f - $34.99 on GOG Kingdom Come: Deliverance II - $29.99 on GOG MENACE - $29.99 on GOG Cairn - $23.99 on GOG Frostpunk 2 - $22.49 on GOG The Alters - $20.99 on GOG Resident Evil Classic Bundle - $20.99 on GOG System Shock 2: 25th Anniversary Remaster - $17.99 on GOG Banishers: Ghosts of New Eden - $16.99 on GOG Legacy of Kain: Defiance Remastered - $16.25 on GOG METAL EDEN - $15.99 on GOG REPLACED - $15.99 on GOG Hollow Knight: Silksong - $14.99 on GOG Tomb Raider I-III Remastered Starring Lara Croft - $11.99 on GOG Chants of Sennaar - $11.99 on GOG Alpha Protocol - $9.99 on GOG DREDGE - $9.99 on GOG Crow Country - $9.99 on GOG Warhammer 40,000: Dawn of War - Anniversary Edition - $2.99 on GOG Keep in mind that availability and pricing for some deals could vary depending on the region. That's it for our pick of this weekend's PC game deals, and hopefully, some of you have enough self-restraint not to keep adding to your ever-growing backlogs. As always, there are an enormous number of other deals ready and waiting all over the interwebs, as well as on services you may already subscribe to if you comb through them, so keep your eyes open for those, and have a great weekend.
    • Lilly-Livered American Media Are Scared
    • Really? Despite the memory price rises, nothing can kill it? I thought something would.
    • I think there will be a 27H1 for actual users of 26H1 The 25h2 supports ARM too : Snapdragon X, Snapdragon X Plus and Snapdragon X Elite
  • Recent Achievements

    • Week One Done
      Genuinetonerink- Dubai earned a badge
      Week One Done
    • One Month Later
      Genuinetonerink- Dubai earned a badge
      One Month Later
    • One Year In
      hhgygy earned a badge
      One Year In
    • One Month Later
      AMV earned a badge
      One Month Later
    • Week One Done
      AMV earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      514
    2. 2
      +Edouard
      171
    3. 3
      PsYcHoKiLLa
      82
    4. 4
      Steven P.
      74
    5. 5
      Michael Scrip
      72
  • Tell a friend

    Love Neowin? Tell a friend!