Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过数据库查询实现Drupal 6表单迭代_Php_Mysql_Drupal_Foreach_Drupal 6 - Fatal编程技术网

Php 通过数据库查询实现Drupal 6表单迭代

Php 通过数据库查询实现Drupal 6表单迭代,php,mysql,drupal,foreach,drupal-6,Php,Mysql,Drupal,Foreach,Drupal 6,我试图在从db查询中提取的数组上运行foreach,但由于某些原因,它不起作用。这是一个简单的语法错误还是其他什么 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

我试图在从db查询中提取的数组上运行foreach,但由于某些原因,它不起作用。这是一个简单的语法错误还是其他什么

  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,
  );
  }
但是,以下方法行不通:

  foreach ($sas as $id => $value){ 
  $form['questionnaire']['fill'.$id] = array(
    '#type'=>'textfield',
    '#title'=> $value,
  );
  }

语法看起来不错,但显而易见的答案是查询没有返回任何数据。您是否可能忘记了全局$user?Devel模块可以提供帮助。

除非您的查询返回一个结果(我怀疑这是因为您需要一个结果数组),否则您的语法会遗漏一些内容
db\u fetch\u数组
将只获取一行。所以你需要这样做

$form['questionnaire'] = array(
  '#type' => 'fieldset',
  '#title' => 'test',
);

global $user;
$result = db_query("SELECT * FROM {table} WHERE id='%s'", $user->name);

// Remember that $row is an array where the key names map the column names in your table
// So if you have a column 'id' and another one 'value' you can access the
// value for one row like this $row['id] and $row['value']
while ($row = db_fetch_array($result)) {
  $form['questionnaire']['fill' . $row['id']] = array(
    '#type' => 'textfield',
    '#title' => $row['column_name'],
  );
}
更新07.02.2013:使用
foreach()
循环代替
while()


全球用户不会忘记。我还使用了一个whileloop。这就是为什么我认为我得到了foreach的语法错误,我实际上是想看看如何使用foreach来实现这一点。我能够让while循环工作。在这个上下文中,使用
while
更有意义,正如您将在整个Drupal上下文中看到的那样,它是遍历行数组的方法。但是如果您喜欢使用
foreach
,我也添加了这个选项。
foreach (db_fetch_array ($row) as $row) {
    echo $row['name'];
}