I’m trying to run a foreach over an array pulled from a db query but for some reason it isn’t working. Is this a simple syntax error or something else?
global $user
$ras = db_query("SELECT * FROM {table} WHERE id='%s'", $user->name);
$sas = db_fetch_array($ras);
$arr = array("one",2,3,"four",5);
$form['questionnaire'] = array (
'#type'=>'fieldset',
'#title'=> 'test', );
foreach ($arr as $id => $value){
$form['questionnaire']['fill'.$id] = array(
'#type'=>'textfield',
'#title'=> $value,
);
}
However, the following wont work:
foreach ($sas as $id => $value){
$form['questionnaire']['fill'.$id] = array(
'#type'=>'textfield',
'#title'=> $value,
);
}
Unless your query returns a single result, which I doubt since you’re expecting an array of results, your syntax is missing something.
db_fetch_arraywill only fetch one row. So you need to do something like this.Update 07.02.2013: Using a
foreach()loop instead ofwhile()