Hi all, I'm back again to rewrite my answer, unfortunatelly it was deleted due the hackers attack.
I have a PHP form that inserts in a database table what an user choose from 4 checkboxes.
This is the code that I use:
The checkboxes could be chosen all 4 or also 3, or 2, or 1.Code:<?php
$id_language = ($_POST['id_language']);
$tot_value = "";
foreach ($id_language as $value) {
$tot_value .= "$value\n";
}
$sql_run = "I_NSERT INTO MyTable (id_language) VALUES ('$tot_value')";
$sql_result = mysql_query($sql_run);
?>
Languages:<br />
<input type="checkbox" value="Spanish" name="id_language[]">Spanish<br />
<input type="checkbox" value="English" name="id_language[]">English<br />
<input type="checkbox" value="German" name="id_language[]">German<br />
<input type="checkbox" value="French" name="id_language[]">French<br />
<br>
<input type="submit" name="submit" id="submit" value="Submit">
As you can see the data in the database table are inserted as an array, in fact I have, for example:
Then I have a frontend part where the user can choose from 4 countires (Spain, UK, Germany, France) and after this the user can search deep.id_language
English
German
French
The frontend of my "program" is a table where I print these infos stored in my database:
Category of the plant......
Name of the plant.....
Country.....
Languages.....
Instead of the languages I'd like to print the flag of the languages, something like this:
You already suggested me this way:Code:<?php
if ($id_language == "Spanish") {
?>
<img src="flags/esp.png" />
This way work, but it obviously prints all the flag.Code:<?
$id_language = array("Spanish", "English", "German", "French");
if (in_array("English", $id_language)) {
?>
<img src="flags/uk.png" />
I'd like to take the array with the X languages stored in my database table and then compare or search in it the languages insertes.
Any help?
I'm really disperate about this.
P.S.: the languages are max 4 and setted by me as checkboxes.
P.P.S: I know that "I_NSERT" is wrong but otherwise I couldn't post the thread.
thats easy
you can make an arary
for the language and its flag
ad an example
Now on the sql_query fetching ..Code:$flags_array=array("English" => "uk","Spanish" => "esp","German"=>"gr","French"=>"fr");
I Guess the idea is clear ??Code:$query=mysql_query("select ...etc");
while($row=mysql_fetch_array($query))
{
$flag="<img src="flags/".$flags_array[$row[language]].".png" />";
}
First of all thank you.
I'm near the solution, but.......
I've done so:
The little problem is that it can't find and place the language code for the flag, it has to print for example (assume these languages: "English Spanish"):Code:$flags_array=array("English" => "uk","Spanish" => "esp","German"=>"gr","French"=>"fr");
$query = mysql_query("S_ELECT id_language FROM MyTable WHERE id_nation = '".$id_nation."'");
while($row = mysql_fetch_array($query))
{
echo "<img src=flags/".$flags_array[$row['id_language']].".png />";
// echo $row['id_language']; //Just for test
}
but it prints this:flags/uk.png flags/esp.png
P.S.: as usual I know that S_ELECT is wrong but otherwise I couldn't Submit the post.flags/.png flags/.png
whats the value of
$row['id_language']
It's an array with the languages choosen by the users in the admin section.
They can add a record with these fields:
id_category (category of the plants)
id_name_plant (name of the plant)
id_nation (country where you can find it)
id_language (scientific name and translation of the name of the plant).
So id_language is an array with max 4 languages and the user can add the languages by choosing them ticking the checkbox or the checkboxes in another page and the records are putted in the database, in the MyTable.
For example, a possible record could be:
id_category
Bushes
id_name_plant
Ash Leaf Spirea
id_nation
Germany
id_language
Scientific name: Sorbaria sorbifolia
Translations:
Spanish flag: .........
English flag: ..........
German flag: .........
the country is germany
but in array
you have to match ..Code:"German"=>"gr"
So I had to put somthing like:
?"German" => "Germany"
Really can't figure it out, I'm writing code in the last 6 continuous hours.
I've changed the inserting query of the language in the database:
Now instead of the OLD:Code:$id_language = ($_POST['id_language']);
$tot_value = "";
foreach ($id_language as $value) {
$value="'".$value."'";
}
$datos_uno=implode(",", $_POST['id_language']);
$sql_run = "I_NSERT INTO MyTable (id_language) VALUES ('$datos_uno')";
$sql_result = mysql_query($sql_run);
I have this NEW one:id_language
Spanish
English
And to display:id_language
Spanish,English
But in this way I have to make lots of "if" cases to match all the possible combinations.Code:$test_sql= "S_ELECT id_language FROM MyTable WHERE id_nation = '".$id_nation."'";
$query_example = mysql_query($test_sql);
$found = false;
while($row = mysql_fetch_array($test_sql))
{
if (in_array("Spanish,English", $row)){
// echo "<img src=flags/esp.png /> ";
// echo "<img src=flags/en.png /> ";
echo "I found: Spanish,English";
$found = true;
break;
}
if (!$found) {
echo "not found";
}
}
There's no way to make it easier?
why are you compicating your code ??
instead of
useCode:if (in_array("Spanish,English", $row)){ // echo "<img src=flags/esp.png /> "; // echo "<img src=flags/en.png /> "; echo "I found: Spanish,English"; $found = true; break; }
will till you the stored languagesCode:echo "I found: ".$row[id_language];
I don't wanna complicate it, it was just a brutal changes in the code.
But the main problem remains, I can't print the flags of the languages array I found in my row[0] (example: Spanish,English)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks