Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

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

  1. #1
    Thevening is offline Newbie
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

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

    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:
    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">
    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:
    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:
    Code:
    <?php
    if ($id_language == "Spanish") {
    ?>
                    <img src="flags/esp.png" />
    You already suggested me this way:
    Code:
    <?
    $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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    mr.tha2r is offline Newbie
    Join Date
    Feb 2010
    Posts
    17
    Rep Power
    0

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

    thats easy

    you can make an arary

    for the language and its flag

    ad an example

    Code:
    $flags_array=array("English" => "uk","Spanish" => "esp","German"=>"gr","French"=>"fr"); 
    Now on the sql_query fetching ..

    Code:
    $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 ??

  4. #3
    Thevening is offline Newbie
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

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

    First of all thank you.
    I'm near the solution, but.......

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

    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"):
    flags/uk.png flags/esp.png
    but it prints this:
    flags/.png flags/.png
    P.S.: as usual I know that S_ELECT is wrong but otherwise I couldn't Submit the post.

  5. #4
    mr.tha2r is offline Newbie
    Join Date
    Feb 2010
    Posts
    17
    Rep Power
    0

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

    whats the value of
    $row['id_language']

  6. #5
    Thevening is offline Newbie
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

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

    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: .........

  7. #6
    mr.tha2r is offline Newbie
    Join Date
    Feb 2010
    Posts
    17
    Rep Power
    0

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

    the country is germany

    but in array
    Code:
    "German"=>"gr"
    you have to match ..

  8. #7
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

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

    So I had to put somthing like:
    "German" => "Germany"
    ?

  9. #8
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

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

    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:
    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); 
    Now instead of the OLD:
    id_language
    Spanish
    English
    I have this NEW one:
    id_language
    Spanish,English
    And to display:
    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";
     }
     } 
    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?

  10. #9
    mr.tha2r is offline Newbie
    Join Date
    Feb 2010
    Posts
    17
    Rep Power
    0

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

    why are you compicating your code ??

    instead of
    Code:
    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

    Code:
          echo "I found: ".$row[id_language];
    will till you the stored languages

  11. #10
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

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

    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)

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 10-28-2011, 08:21 AM
  2. How to store array values into mysql database
    By Astha in forum PHP Development
    Replies: 4
    Last Post: 09-18-2010, 12:16 AM
  3. Replies: 21
    Last Post: 08-16-2010, 09:15 PM
  4. Multilanguage data (PHP data in MySQL table)
    By Thevenin in forum Database & Database Programming
    Replies: 0
    Last Post: 02-17-2010, 07:03 AM
  5. How to have PHP get data from websites and store in mySQL
    By Scottyf11 in forum PHP Development
    Replies: 1
    Last Post: 08-06-2008, 11:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts