0

我需要查询数据库并为列返回不同的值。查询在 foreach 循环中,并根据数组而变化。

$get_all_col_names = $this->db->list_fields($table_by_product);
//This will return "X_SIZE", "X_PRINT", "X_QTY"

现在我有一个 foreach 需要分别获取“X_SIZE”、“X_PRINT”和“X_QTY”的不同值。

foreach ($X_types as $X) {
   $this->db->select($X);
   $this->db->distinct();
   $qX = $this->db->get($table_by_product);
   return $qX->result_object();
}

当前设置的问题在于它只返回 X_QTY 的 DISTINCT 值,这是列表中的最后一个数组。

我需要为数组中的所有键返回不同的值。我怎样才能使这项工作?谢谢您的帮助。

4

1 回答 1

0

问题是您的 foreach 将在循环的第一轮之后返回,请尝试以下操作:

$resultArray = array(); 
foreach ($X_types as $X) { 
    this->db->select($X)$this->db->distinct(); 
    $qX = $this->db->get($table_by_product); 
    resultArray[] = $qX->result_object(); 
} 
return $resultArray; 
于 2012-11-04T10:22:39.233 回答