I have a model a method in my Model as
public function selectWhereLimitOffset($table_name, $where = NULL, $limit, $offset)
{
if ($where) {
$this->db->where($where);
}
$this->db->order_by('post_id', "desc");
if($limit)
$this->db->limit($limit, $offset);
$result = $this->db->get($table_name);
return $result->result();
}
I'm calling this method from this function in my controller:
public function exams($page_id = NULL)
{
$offset = 0;
if($page_id == NULL)
$offset = 1;
else
$offset = ($page_id * 10);
$limit = 10;
$data['main_content'] = 'dashboard/exams';
$data['page_errors'] = FALSE;
$where = NULL;
$data['results_exam'] = $this->Fetch->selectWhereLimitOffset('exam', $where, $limit, $offset);
$data['results_count_total'] = $this->Fetch->getCount('exam');
$this->load->view('includes/template_su', $data);
}
Now the weird thing here is, the method selectWhereLimitOffset() works fine if called from other similar methods in my controller. But when called from this method exams(), it doesn't fetch the data from the table. A similar method that work perfectly fine is:
public function posts($page_id = NULL)
{
$offset = 0;
if($page_id == NULL)
$offset = 1;
else
$offset = ($page_id * 10);
$limit = 10;
$data['main_content'] = 'dashboard/posts';
$data['page_errors'] = FALSE;
$where = NULL;
$data['results_post'] = $this->Fetch->selectWhereLimitOffset('post', $where, $limit, $offset);
$data['results_count_total'] = $this->Fetch->getCount('post');
$this->load->view('includes/template_su', $data);
}
One more thing, the next statement:
$data['results_count_total'] = $this->Fetch->getCount('exam');
does return the count of rows from the table, so that proves that there's no issue with the table name. The table has only one row, if that might help.
The output of the data array is as follows:
Array
(
[main_content] => dashboard/exams
[page_errors] =>
[results_exam] => Array
(
)
[results_count_total] => 1
)
Please help.
Solved the problem. Just had to change the offset value from 1 to 0. Thats it.
Related
I have following function.
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;
}
}
}
Long story short, i need to grab data from sql and send to an api for each users which meets some requirements. I need to prepare apicall like this it's now $arr , but the point it's that pagination it's not working, for user 1 i have 1077 records and i get just one json array with products (1077 products), instead of 2 with maximum 1k .
Could someone help me a bit cause i'm not getting where is the error in my script..
Forgotted to add $limit and offset there:
$prodArr = $this->db->get('ci_products')->result_array();
Correct:
$prodArr = $this->db->get('ci_products', $limit, $offset)->result_array();
And added also apicall in "for" loop.
I let here my function which, now it's working fine, maybel it will help someone to inspire.
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;
}
}
}
}
I'm a Symfony beginner & I'm trying to load my contact from the database using the Doctrine Query Builder as follows:
ContactRepository.php:
public function getcontactByLimit($offset, $limit)
{
$queryBuilder = $this->_em->createQueryBuilder()
->select('contact')
->from($this->_entityName, 'contact')
->setFirstResult( $offset )
->setMaxResults( $limit )
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return $results;
}
DefaultController.php:
$contacts = $repository->getContactByLimit(0, 3);
Now I want to get all contacts I have in the database but just 3 by 3, what means I'm supposed to change the offset value at every loop (to 3 then 6 then 9...)
Have you any idea how to do it ?
It seems for me, that you want to load objects in parts to save memory.
For that, you can use an Iterator:
$query = $queryBuilder->getQuery();
$iterator = $query->iterate();
return $iterator;
And iterate in your Controller
foreach ($iterator as $row) {
}
What will happen, is that doctrine uses PDO and select your result but not read it out directly. There is a cursor on the ResultSet of the Database, which is moving on each iteration. So your PHP-Application will get each Row from Database one by one.
To get 3 by 3 packages, you could use the internal position:
$currentPackage = array();
foreach ($iterator as $position => $row) {
if($position % 3){
// do here something with your 3er package before unset
$currentPackage = array();
$entityManager->clear(); // clear doctrine, could free memory
}
$currentPackage[] = $row;
}
That what I did, & it works fine !
DefaultController.php:
$offset = 0;
$limit = 2;
$sizeData /= $limit;
for( $i = 0; $i < $sizeData; $i++)
{
$contacts = $repository->getListByLimit($offset, $limit);
$sender->setContacts($contacts);
$offset += $limit;
}
ContactRepository.php:
public function getListByLimit($offset, $limit)
{
$queryBuilder = $this->_em->createQueryBuilder()
->select('contact')
->from($this->_entityName, 'contact')
->setFirstResult( $offset )
->setMaxResults( $limit );
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return $results;
I am creating a pagination using Codeigniter and my problem is I have an error in displaying the page number links in my URL.
example I go to page 2 my URL would be like this:
http://localhost/my_project/inbound/listing/1
if page 3
http://localhost/my_project/inbound/listing/2
Here's my controller
$config = array();
$config['base_url'] = base_url('inbound/listing');
$config['total_rows'] = $this->mod->countList();
$config['per_page'] = 1;
$config['uri_segment'] = 3;
//fd($config);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
fp($page, 'pink'); //print out result
$order = ($this->input->get('order'))? $this->input->get('order'): '';
$sort = ($this->input->get('sort'))? $this->input->get('sort'): '';
// Query data
$data['data_list'] = $this->mod->listing($config['per_page'], $page); //where, limit, page, field, sort
fP($data['data_list']); //print out result
$data['pagination'] = $this->pagination->create_links();
Then my model for generating links:
function listing($limit, $start)
{
//DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
$this->db->select('*');
$this->db->from('inventory');
$this->db->limit($limit, $start);
$query = $this->db->get();
//$rows = $query->result_array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Can you help me with this?
If you want to go to page number based on your url, modify your listing function to be like this:
function listing($limit, $start)
{
$offset_1 = $start - $limit;
$offset_2 = $offset_1 < 0 ? 0 : $offset_1;
//DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
$this->db->select('*');
$this->db->from('inventory');
$this->db->limit($limit, $offset_2);
$query = $this->db->get();
//$rows = $query->result_array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
I am little confused here.
I have a modal like this :
public function selectRequestPerUser($nama_user, $start_row, $limit) {
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user), $start_row, $limit);
return $query->result_array();
}
So, I use this modal to create a pagination in CI like this :
$nama = $this->session->userdata('nama');
$start_row = $this->uri->segment(2);
$per_page = 3;
if(trim($start_row) == ''){
$start_row = 0;
};
$this->load->library('pagination');
$config['base_url'] = base_url().'control_closing/';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$request = $this->model_request->selectRequestPerUser($nama, $start_row, $per_page);
$data['data_request'] = $request;
$this->load->view('view_closing', $data);
in view, just :
<?php echo pagination ?>
It just give me a blank page. I think in my modal get_where is the problem. Anybody can help ?
The syntax of get_where() is
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
You need to change this line in your model (alter $start_row and $limit) to make it work,
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user), $limit, $start_row);
hi every one i am trying to display mysql database records using codeigniter pagination..for that i written the following below codes..and when i run this it shows me all the results in one page while the page numbers are displayed right according to the given per_page limit..
Model:
function get_records($user_name, $limit, $start) {
$this->db->limit($limit, $start);
$this->db->select('GROUP_CONCAT(cid) AS cIDs');
$this->db->where('user_name', $user_name);
$this->db->group_by("company_user");
$this->db->from('company_records');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
function count_records($user_name) {
$this->db->select('GROUP_CONCAT(cid) AS cIDs');
$this->db->where('user_name', $user_name);
$this->db->group_by("company_user");
return $this->db->count_all('company_records');
}
Controller:
$this->load->model('Records', 'Records', TRUE);
$this->load->library('pagination');
$config['base_url'] = base_url() . 'records/index';
$config['total_rows'] = $this->Records->count_records($this->session->userdata('user'));
$config['per_page'] = 10;
$config["uri_segment"] = 4;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
/* $start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; */
if ($this->uri->segment(4) > 0) {
$offset = ($this->uri->segment(4) + 0) * $config['per_page'] - $config['per_page'];
} else {
$offset = $this->uri->segment(4);
}
$data['recordsDetails'] = $this->Records->get_records($this->session->userdata('user'), $config["per_page"], $offset);
$data['pages'] = $this->pagination->create_links();
i have tried google search and stackoverflow.. but not yet found any solutions to this..may be i have issue with the query or i dont know..
please help me...
I used this code for pagination.
In the model
//Search for results
public function search($id, $table, $pageNumber) {
//Get the number of pages
$numOfPage = $this->findResults($id, $table, RETURN_NUM_OF_PAGES, $pageNumber);
if ($numOfPage < 1) {
return false; //If there are no search results return false
} else {
$row = array();
$res = $this->findResults($id, $table, RETURN_RESULTS, $pageNumber);
for ($j = 0; $j < $res->num_rows(); $j++) {
$row[$j] = $res->row($j);
}
return $row; //Return the results
}
}
// Find the results from DB and return number of pages/ results
function findResults($id, $table, $returnType, $pageNumber) {
$this->db->where('id', $id);
$this->db->order_by('reputation', 'desc'); //Order the users by reputation
$itemsPerPage = 4;
$startingItem = ($pageNumber - 1) * $itemsPerPage;
if ($returnType == RETURN_RESULTS) {
$res = $this->db->get($table, $itemsPerPage, $startingItem);
return $res; //Return the results
} else { //Return the number of pages
$res = $this->db->get($table);
$count = $res->num_rows(); //Get the total number of results
$numofPages = (int) ($count / $itemsPerPage);
if ($count % $itemsPerPage != 0)
$numofPages = $numofPages + 1; //If the number of items < $itemsPerPage there should be 1 page
return $numofPages; //Return the number of pages
}
}
So from the controller you have to call the function search($id, $table, $pageNumber).
Also the constants were defined in config/constants.php file as follows.
define('RETURN_NUM_OF_PAGES', 0);
define('RETURN_RESULTS', 1);