Jump to content

Checkboxes that store array data in mySQL table and modify them with images

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
Thevening

Thevening

    Newbie

  • Members
  • Pip
  • 3 posts
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:
<?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">

The checkboxes could be chosen all 4 or also 3, or 2, or 1.
As you can see the data in the database table are inserted as an array, in fact I have, for example:

Quote

id_language
English
German
French

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.

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:
<?php
if ($id_language == "Spanish") {
?>
				<img src="flags/esp.png" />

You already suggested me this way:
<?
$id_language = array("Spanish", "English", "German", "French");
if (in_array("English", $id_language)) {
?>
				<img src="flags/uk.png" />

This way work, but it obviously prints all the flag.
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.

#2
mr.tha2r

mr.tha2r

    Newbie

  • Members
  • PipPip
  • 17 posts
thats easy

you can make an arary

for the language and its flag

ad an example

$flags_array=array("English" => "uk","Spanish" => "esp","German"=>"gr","French"=>"fr");

Now on the sql_query fetching ..

$query=mysql_query("select ...etc");
while($row=mysql_fetch_array($query))
{
$flag="<img src="flags/".$flags_array[$row[language]].".png" />";
}

I Guess the idea is clear ??

#3
Thevening

Thevening

    Newbie

  • Members
  • Pip
  • 3 posts
First of all thank you.
I'm near the solution, but.......

I've done so:
$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
}

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"):

Quote

flags/uk.png flags/esp.png
but it prints this:

Quote

flags/.png flags/.png

P.S.: as usual I know that S_ELECT is wrong but otherwise I couldn't Submit the post.

#4
mr.tha2r

mr.tha2r

    Newbie

  • Members
  • PipPip
  • 17 posts
whats the value of
$row['id_language']

#5
Thevening

Thevening

    Newbie

  • Members
  • Pip
  • 3 posts
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: .........

#6
mr.tha2r

mr.tha2r

    Newbie

  • Members
  • PipPip
  • 17 posts
the country is germany

but in array
"German"=>"gr"

you have to match ..

#7
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
So I had to put somthing like:

Quote

"German" => "Germany"
?

#8
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
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:
$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);
Now instead of the OLD:

Quote

id_language
Spanish
English

I have this NEW one:

Quote

id_language
Spanish,English

And to display:
$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";
 }
 }
But in this way I have to make lots of "if" cases to match all the possible combinations.
There's no way to make it easier?

#9
mr.tha2r

mr.tha2r

    Newbie

  • Members
  • PipPip
  • 17 posts
why are you compicating your code ??

instead of
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;
 }

use

      echo "I found: ".$row[id_language];

will till you the stored languages

#10
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
I don't wanna complicate it, it was just a brutal changes in the code. :rolleyes:

But the main problem remains, I can't print the flags of the languages array I found in my row[0] (example: Spanish,English)

#11
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Done easily using strpos() :thumbup:

#12
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts

Thevenin said:

Done easily using strpos() :thumbup:
Hey I'm back again, cause I've fixed my problem using strpos(), but today after a lot of time I've found a bug.

I've written the following code:

<?php

$select_language_written = "SELECT id_language_written FROM MyTable WHERE id_nation = '".$id_nation."'";

$query_language_written = mysql_query($select_language_written);


while($row = mysql_fetch_array($query_language_written))

 {	 

	 $pattern_select_language_written = $row['id_language_written'];

	 

	 $pos = strpos($pattern_select_language_written, "English");

	 $pos1 = strpos($pattern_select_language_written, "German");

	 $pos2 = strpos($pattern_select_language_written, "French");

	 $pos3 = strpos($pattern_select_language_written, "Spanish");

	 

	 if($pos !== false)

	 	echo '<img style="padding-left:5px" src='flags/uk.png /> ';

	 if($pos1 !== false)

	 	echo '<img style="padding-left:5px" src='flags/de.png /> ';

	 if($pos2 !== false)

	 	echo '<img style="padding-left:5px" src='flags/fr.png /> ';

	 if($pos3 !== false)

		echo '<img style="padding-left:5px" src='flags/es.png /> ';	 

 }

?>

It works quite good, if I have only one result it works, but if I have more then one result, it prints in the field all the flag of all the results.

For example in the mySQL database I have:

Quote

ID 1:
id_language_written
French,Spanish

ID 2:
id_language_written
English,German

In the result page it prints:

Quote

ID 1:
Language Written: French Spanish English German

ID 2:
Language Written: French Spanish English German

Any hint about how to fix this problem?