php 在codeigniter中从数据库中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38867937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:44:31  来源:igfitidea点击:

Fetch data from database in codeigniter

phpmysqlcodeigniter

提问by sammy001

I have 2 cases where i am fetching the entire data and total number of rows of a same table in codeigniter, I wish to know that is there a way through which i can fetch total number of rows, entire data and 3 latest inserted records from the same table through one code

我有两种情况,我在 codeigniter 中获取同一个表的整个数据和总行数,我想知道有没有一种方法可以从中获取总行数、整个数据和 3 个最新插入的记录一码同桌

Controller code for both cases is as given below (although i am applying it for each case seperately with different parameters)

两种情况的控制器代码如下所示(尽管我使用不同的参数分别为每种情况应用它)

public function dashboard()
    {
        $data['instant_req'] = $this->admin_model->getreq();
        $this->load->view('admin/dashboard',$data);
    }

1) to fetch the entire data from a table in codeigniter

1)从codeigniter中的表中获取整个数据

Model Code

型号代码

public function getreq()
    {
        $this->db->where('status','pending');
        $query=$this->db->get('instanthire');
        return $query->result();
    }

View Code

查看代码

foreach ($instant_req as $perreq) 
    {
        echo $perreq->fullname;
        echo "<br>";
    }

2) to fetch number of rows from a table in codeigniter

2)从codeigniter中的表中获取行数

public function getreq()
    {
        $this->db->where('status','pending');
        $query=$this->db->get('instanthire');
        return $query->num_rows();
    }

View Code

查看代码

echo $instant_req;

采纳答案by Dhaval Purohit

You can make only one function that gives you the all data at once total number of rows, entire data and 3 latest inserted records

您只能创建一个函数,一次为您提供所有数据的总行数、整个数据和 3 个最新插入的记录

for example in the model

例如在模型中

public function getreq()
{
    $this->db->where('status','pending');
    $query=$this->db->get('instanthire');
    $result=$query->result();
    $num_rows=$query->num_rows();
    $last_three_record=array_slice($result,-3,3,true);
    return array("all_data"=>$result,"num_rows"=>$num_rows,"last_three"=>$last_three_record);
}

in controller dashboard function

在控制器仪表板功能中

public function dashboard()
{
    $result = $this->admin_model->getreq();
    $this->load->view('admin/dashboard',$result);
}

in view

鉴于

foreach ($all_data as $perreq) 
{
    echo $perreq->fullname;
    echo "<br>";
}
//latest three record
foreach ($last_three as $perreq) 
{
    echo $perreq->fullname;
    echo "<br>";
}
//total count
echo $num_rows;

回答by Danyal Sandeelo

Raw query may work here.

原始查询可能在这里工作。

$resultSet =  $this->db->query("select * from table_name"); 
$queryCount = count($resultSet );

回答by Kerel

Function

功能

function getData($limit = 0){

    //Create empty array
    $data = [];

    //Where clause
    $this->db->where('status','pending');

    //Order Data based on latest ID
    $this->db->order_by('id', 'DESC');
    if($limit != 0){
        $this->db->limit($limit);
    }

    //Get the Data
    $query = $this->db->get('instanthire');
    $data['count'] = $query->num_rows();
    $data['result'] = $query->result();
    return $data;

}

Calls

通话

//Last 3 Inserted
$data = getData(3);
//All Data
$data = getData();

CodeIgniter Database Documentation

CodeIgniter 数据库文档

回答by Mandip Vora

Try this logic :

试试这个逻辑:

Model code :

型号代码:

public function getreq()
{
    $this->db->where('status','pending');
    $this->db->order_by('id', 'DESC');  //actual field name of id
    $query=$this->db->get('instanthire');

    return $query->result();
}

Controller Code :

控制器代码:

public function dashboard()
{
    $data['instant_req']  = $this->admin_model->getreq();
    $data['total_record'] = count($data['instant_req']);
    $this->load->view('admin/dashboard',$data);

}

View Code:

查看代码:

 $i=0;
foreach ($instant_req as $perreq) 
{
    if($i<3){
         echo $perreq->fullname;
         echo "<br>";
    }
    $i++;
}
Echo 'Total record : '.$total_record;

回答by Aman Maurya

Model:

模型:

public function getreq()
{
     $res = $this->db->order_by("<place column primary id>","desc")->get_where('instanthire',['status'=> 'pending']);
     $latest_3 = [];
     if(count($res)){
        $i=1;
        foreach($res as $r){
            $latest_3[]=$r;
            if($i == 3)
               break;
            $i++;
        }
     }

     $arr = [
        'latest_3' => $latest_3,
        'count' => count($res),
        'total_result' => $res,
     ];
     return $arr;
}

回答by Lorenzo

Here is a simple solution that I can first think of but if you want me to maybe improve I can.

这是我首先想到的一个简单解决方案,但如果您希望我改进,我可以。

Just stick with your first code(Model) and in the view count how many items are iterated through.

只需坚持使用您的第一个代码(模型),并在视图中计算迭代了多少项。

$count = 0;
foreach ($instant_req as $perreq) 
{
    echo $perreq->fullname;
    echo "<br>";
    $count++;
}
echo $count;

Am I still missing something? just let me know

我还缺少什么吗?让我知道

EDIT:

编辑:

This is another solution, return an array

这是另一种解决方案,返回一个数组

public function getreq()
{
    $this->db->where('status','pending');
    $query=$this->db->get('instanthire');
    $data['results'] = $query->result();
    $data['count'] = $query->num_rows();

    return $data
}

I'm not very confident and haven't really tried this but on top of my head I think it can work.

我不是很自信,也没有真正尝试过这个,但在我的脑海中,我认为它可以工作。