vendredi 30 novembre 2018

How to make a double loop to populate an echo statement

I have values written in a database. These value come from a checkbox form.
I try to get these values from the database and write them in a new page.
I use the following code that works :

$con = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$pw") or die ("Error in Selecting ");

    $sql = pg_query($con,"SELECT language FROM texte where bhl='17'");
    if(pg_num_rows($sql) > 0){
      $result = pg_fetch_assoc($sql);
      $checked_arr = explode(";",$result['language']);
    }
    // Gives "item1"

    // Create checkboxes
    foreach ($checked_arr as &$value) { 
        $array1 = array("item1", "item2", "item3");
        $id = array("id-item1", "id-item2", "id-item3");
          $checked = "";
          if(in_array($value, $array1)){
            $checked = "checked";
          }
          echo '<input type="checkbox" name="lang[]" value="'.$value.'" '.$checked.' > '.$value.' <br/>';
    }

But actually something is missing : the id value that characterizes the checkbox. So I inserted in the code an array composed of the id values.
The goal is to associate id-item1 to item1, id-item2 to item 2 and so on.
To make it possible I guess I have to create a double loop to write in the echo statement both values and id's.
But after many attempts I didn't manage to make it possible.

Any help would be appreciated.
Thanks !




Aucun commentaire:

Enregistrer un commentaire