Voir un article

postgres array to php array

how to use postgres array and php array

pays_id is an integer array field

// select datas :
$r = pg_query( $db,  "select array_to_string(pays_id,',') as pays_id_tab from ...
$t = pg_fetch_array($r,0); ...


// in php, transform postgres array to php array :
 $pays_id = explode(',' , $t["pays_id_tab"]) ;


// so you can generate html checkbox :
// you can select values form child table, and for each item you can output the checked attribute if needed :

$r = pg_query( $db,  "select * from childtable");
for ($i... ) {
   $t=pg_fetch_array($r,$i);
   if ( in_array( $t["id"], $pays_id ) $checked = "checked=checked"; else $checked= "";
   echo '<input type="checkbox" name="pays_id[]" value="'.$t["id"]." $checked />';
}

 

// on submit , your php script can handle the php variable and transform it, for postgres sql :
    if ( $pays_id=='') $pays_id='null'; else
         $pays_id ="string_to_array('".implode(',',$pays_id). "',',')::integer[]";

   pg_query( $db, "update table set pays_id = $pays_id ...

 



 



3 829 clics - Créé le 30/10/2012 par Tito



Réagissez, commentez, discutez ...

Soyez le premier à réagir !



Partager ?

diaspora  G+  facebook  twitter  Digg  Yahoo  Delicious  Technorati  myspace


Voir d'autres articles en rapport avec celui-ci ?



Stats des clics sur cet article : cliquez ici »



Vous voulez contribuer et publier un article dans cette rubrique ?

Merci de vous identifier ou de vous créer un compte si ce n'est pas déjà fait.