【问题标题】:CodeIgniter: Why does my array of unique table data contain no values?CodeIgniter:为什么我的唯一表数据数组不包含任何值?
【发布时间】:2014-09-23 15:19:53
【问题描述】:

我的数据库肯定包含值,我正在尝试将特定列(名称)的这些值读取到一个数组中,如下所示:

public function listcli()
{
 $this->db->distinct();
 $this->db->select('name');
}
}

然后像这样引用这个函数:

public function clist() {
    $this->load->model('list_model');
    $fields = $this->list_model->listcli();
    $fieldl = $fields;
    $data= array();
    $data['fieldl'] = $fieldl;
    $this->load->view('clientlist', $data);
    }

这基本上完成了模型中的数据库查询,将信息传递给控制器​​,在那里它将数组放入另一个带有键的数组中(键与数组同名),这样我就可以将它传递到我的视图中,那么我的观点是这样的:

<html>
<body>
    <p> <?php print_r($fieldl); ?></p>
    <ul>
        <?php
            $fieldl = array();
            ?>
            <p> <?php print_r($fieldl); ?> </p>
            <?php
            foreach($fieldl as $l) {
        ?>
            <li> 
                <?php echo $l;?>
            </li>
            <?php
            }

然后列出来

但即使我知道我在“名称”中有数据,print_r 显示我的数组是空的?请帮忙,谢谢!

【问题讨论】:

  • listcli 不返回值?
  • 不行,这个问题我不知道怎么解决。

标签: php mysql arrays codeigniter


【解决方案1】:

您确定您的模型返回任何值吗?如果没有,返回一些:

public function listcli()
{
    $this->db->distinct();
    $this->db->select('name');
    return $this->db->get('table_name')->result_array(); // return value
}

【讨论】:

  • 输出了这个 CI_DB_pdo_result 对象 ( [num_rows] => 0 [conn_id] => PDO Object () [result_id] => PDOStatement Object ([queryString] => SELECT DISTINCT name FROM sales_lead) [result_array ] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [row_data] => ) 对不起,我是 codeigniter 的新手。
  • @seanyt123 SELECT DISTINCT name FROM sales_lead 基本上产生了 0 行,尝试评论不同的行,你确定不同
  • 哎呀这是我的数据库名称,我输入了我的表名并得到: CI_DB_pdo_result Object ( [num_rows] => 2 [conn_id] => PDO Object ( ) [result_id] => PDOStatement Object ( [ queryString] => SELECT DISTINCT name FROM clients ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [row_data] => ) 它应该产生结果虽然
  • @seanyt123 那是数据库名称?哈哈,检查我的修改
【解决方案2】:

请尝试以下方法

 public function listcli(){
     $this->db->distinct(); 
     $this->db->select('name');
     $query = $this->db->get('table_name');
     return ($query->num_rows > 0 ? $query->result() : array());
 }

如果没有帮助... 请给我一些虚拟日期的表格结构,以便我可以帮助您解决这个问题。

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多