php codeigniter 3中的分页并发送到API

n6lpvg4x  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(115)

我有以下功能。

public function sendstocktobl()
{
    if ($_SERVER['REMOTE_ADDR'] !== $this->localhost) {
        echo $_SERVER['REMOTE_ADDR'];
    } else {
        $this->load->database();

        $url = get_integration_url('baselinker');
        $this->db->select('user_id');
        $this->db->where('bl_send_stock', 1);
        $this->db->where('is_baselinker', 1);
        $this->db->where('is_active', 1);
        $resArr = $this->db->get('ci_users')->result_array();

        foreach ($resArr as $row) {

            $user_id = $row['user_id'];

            $limit = 1000;
            $offset = 0;
            $where = "quantity is NOT NULL";
            $this->db->where($where);
            $this->db->where('is_active', 1);
            $this->db->where('user_id', $user_id);
            $total_results = $this->db->count_all('ci_products');
            $total_pages = ceil($total_results / $limit);

            $token = get_bl_token($user_id);
            $inventoryid = get_bl_inventory($user_id);
            $blwh = get_bl_warehouse($user_id);

            for ($page = 1; $page <= $total_pages; $page++) {

                $arr = [
                    "inventory_id" => $inventoryid,
                    "products" => [],
                ];

                $this->db->select('*');
                $where = "quantity is NOT NULL";
                $this->db->where($where);
                $this->db->where('is_active', 1);
                $this->db->where('user_id', $user_id);
                $prodArr = $this->db->get('ci_products')->result_array();

                foreach ($prodArr as $prod) {
                    $arr['products'][$prod['id']] =  [$blwh => $prod['quantity']];
                }

                $offset += $limit;
            }
            
            $preparebl = json_encode($arr);
            echo $preparebl;
        }
    }
}

长话短说,我需要从sql中抓取数据并发送到一个API给每个用户以满足一些需求,我需要准备一个apicall,像这样,现在是$arr,但问题是分页不起作用,对于用户1,我有1077条记录,我只得到一个包含产品的json数组(1077个产品),而不是2个最大值为1 k的数组。
有人能帮我一点忙吗?因为我不知道我的脚本中有什么错误。

xqk2d5yq

xqk2d5yq1#

忘记在此处添加$limit和偏移量:

$prodArr = $this->db->get('ci_products')->result_array();

正确:

$prodArr = $this->db->get('ci_products', $limit, $offset)->result_array();

并在"for"循环中添加了apicall。
我把我的函数放在这里,现在它工作得很好,也许它会帮助一些人得到启发。

public function sendstocktobl(){
    if ($_SERVER['REMOTE_ADDR'] !== $this->localhost) {echo $_SERVER['REMOTE_ADDR'];}
    else{
        
        $this->load->database();
        $url = get_integration_url('baselinker');
        $this->db->select('user_id');
        $this->db->where('bl_send_stock',1);
        $this->db->where('is_baselinker',1);
        $this->db->where('is_active',1);
        $resArr = $this->db->get('ci_users')->result_array();

            foreach ($resArr as $row){
        
             $user_id = $row['user_id'];
 
             $limit = 1000;
             $offset = 0;
             $where = "quantity is NOT NULL";

             $total_results = $this->db->where('user_id',$user_id)->where('is_active',1)->where($where)->from("ci_products")->count_all_results();
             $total_pages = ceil($total_results / $limit);

             $token = get_bl_token($user_id);
             $inventoryid = get_bl_inventory($user_id);
             $blwh = get_bl_warehouse($user_id);

             for ($page = 1; $page <= $total_pages; $page++) {

             $arr = [
             "inventory_id" => $inventoryid,
             "products" => [],
             ];

             $this->db->select('*');
             $where = "quantity is NOT NULL";
             $this->db->where($where);
             $this->db->where('is_active',1);
             $this->db->where('user_id',$user_id);
             $prodArr = $this->db->get('ci_products', $limit, $offset)->result_array();

             foreach($prodArr as $prod){
             $arr['products'][$prod['id']] =  [$blwh => $prod['quantity']];
             }

             $preparebl = json_encode($arr);
             echo $preparebl;
             $offset += $limit;

            }
        }
    }
}

相关问题