database - fetch id and name using postres, create php array with key as id and value as name -
i querying postgres database , getting name , id so:
$datesquery = "select date_ridden, id dates user_id=$userid"; //query $thedates = pg_query($db, $datesquery); //execute query $dates=array(); //want use array have key id , value date_ridden i want make $dates array id key , date_ridden value.
currently i'm doing following (which not want do):
while( $date = pg_fetch_row($thedates) ){ $dates[]['value'] = $date[1]; //date id value $dates[]['label'] = $date[0]; //date (in text form) label } i feel should simple thing do, reason can't figure out.
i believe loop structure looking is:
while( $date = pg_fetch_row($thedates) ){ $dates[$date[1]] = $date[0]; } if misunderstood, issue current attempt appends id , label separate array indexes in $dates.
instead try this:
while( $date = pg_fetch_row($thedates) ){ $dates[] = array('value' => $date[1], 'label' => $date[0]); } with example, access entry value = $dates[0]['value'], label = $dates[0]['label']
hope 1 of helps.
Comments
Post a Comment