codeigniter pagination not working as expected - codeigniter

I tried a lot but cant seem to figure out this, any help appreciated. My articles have votes, and I have a page which is supposed to show most voted articles. Am using codeigniter btw.
The controller:
function most_voted()
{
$per_page = 3;
$cur_page = $this->uri->segment(4);
/*
if($cur_page == "") $cur_page = 1;
else $cur_page = (integer)$cur_page;
*/
$offset = ($cur_page - 1) * $per_page;
if($offset < 0) $offset = 0;
$this->load->model('article_model');
$result_rows = $this->article_model->GetMostVoted($per_page,$cur_page);
$total_rows = sizeof($result_rows) + 10;
echo "total rows is : ".$total_rows.'<br>';
echo "cur page is : $cur_page <br>";
//$this->load->library('pagination');
$config['base_url'] = base_url().'articles/most_voted/page/';
$config['uri_segment'] = 4;
$config['num_links'] = 3;
$config['first_link'] = '<<First';
$config['last_link'] = 'Last>>';
$config['prev_link'] = '< Previous';
$config['next_link'] = 'Next >';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['articles'] = $result_rows;
$data['view_file_name'] = 'articles/all_articles_view';
$this->load->view('includes/template',$data);
//echo $this->db->last_query();
}
The model :
function GetMostVoted($limit,$offset)
{
$this->db->order_by('votes','desc');
$q=$this->db->get('cgh_articles',$limit,$offset);
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
The problem :
Issue is that though I get pagination like
< Previous 1 2 3 4 5 Next >
but clicking on 2 goes to url : page/3
clicking on 3 goes to url : page/6
clicking on 4 goes to url : page/9
and so on.
I want a click on 2 to go to page/2, 3 to page/3, and so on. Any suggestions as to whats going wrong?
If you need any more info, please let me know, thanks.

The way the pagination class works is that it returns the item at which the page should start.
For example, url : hostanme/controller/page/9 means that the page should be rendered starting with the 9th most voted article. See the example in the codeigniter documentation.
Also, you have $total_rows = sizeof($result_rows) + 10;. Why is that ?

There is an excellent tutorial for this on nettuts.

Related

How to create a product in Prestashop 1.7

I create my first module in Prestashop 1.7.4. This is my code:
public function createProductsObject()
{
$product = new Product;
$product->name = $productName;
$product->ean13 = '';
$product->reference = '';
$product->id_category_default = $getCategoryID;
$product->category = $getCategoryID;
$product->indexed = 1;
$product->description = $description;
$product->condition = 'new';
$product->redirect_type = '404';
$product->visibility = 'both';
$product->id_supplier = 1;
$product->link_rewrite = $link_rewrite;
$product->quantity = $singleStock;
$product->price = $price;
$product->active = 1;
$product->psoft_hurtobergamo_id = $productID;
$product->add();
$product->addToCategories($getCategoryID);
This function in not complite. But this is not important right now. Variable productName hase this value:
Array ( [0] => Test [1] => Test) )
Beacause I have two language. The problem is. Why I dont have the name after the product is create ?
thanks for help.
To resolve this problem I add new index to my array.
$productName = array('0' => '');
$link_rewrite = array('0' => '');
and then add values to array start from index 1. Because the first language has identity value set to 1. The second, 2.
for ($i = 1; $i <= $getNumberOfAvailableLanguage; $i++)
{
array_push($productName, $name);
array_push($link_rewrite, $clean1);
}

Codeigniter pagination first_url base_url issue

My pagination is not working properly. The problem is when I'm clicking on 2nd page or any other page it does the right thing. But when I click on 1st page number or the page number on which I'm right now it redirects me to base_url like localhost/project/project. It should not happen it should go localhost/project/project/0 or localhost/project/project/1 or localhost/project/project/2 or that page number.
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$total_rows = $this->Product_model->fetch_data('','',$keyword);
//$offset = 0;
$config = array();
$config["base_url"] = base_url().'product';
$config["total_rows"] = $total_rows->num_rows();
$config["cur_page"] = $page;
$config["per_page"] = 1; //display atleast 6 per page
$config["num_link"] = 2;
$config["uri_segment"] = 4;
$config['first_url'] = base_url() . 'product/0';

How to add Out of Stack Label to Product in Magento 1.9

I'm looking for the correct solution to label the products which are out of stock but until now didn't find the right solution. I'm using the ULTIMO template from INFORTIS. I've tried the following without success:
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
$qty $stock->getQty();
if($qty <= 0)
{
<span class="out-of-stock"><span>Sold Out</span></span>
}
Any idea what else could I try?
thanks
$model = Mage::getModel('catalog/product');
$_product = $model->load($product_id);
$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')
->loadByProduct($_product)->getQty();
if($stocklevel <= 0)
{
<span class="out-of-stock"><span>Sold Out</span></span>
}
$product_model = Mage::getModel('catalog/product');
$product_id = $product_model ->getIdBySku($productsku);
$productload = $product_model ->load($product_id);
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productload );
if($stock <= 0)
{
<span class="out-of-stock"><span>Sold Out</span></span></pre>
}

Codeigniter Pagination Links Error After ID

sorry if my question is duplicate, i have a problem with my codeigniter pagination. My url is: http://mysite/news/category/26/(page number), i have only 5 data and the page limit is 10, but the generated links active on page 3, i believe that is because of id/26/
This is my model :
function categpry($id){
$string_query = "SELECT * FROM news n JOIN category c ON n.category_id = c.id_category AND n.category_id = $id ORDER BY n.category_id DESC";
$query = $this->db->query($string_query);
$config['base_url'] = site_url('news/category/' . $id);
$config['total_rows'] = $query->num_rows();
$config['per_page'] = '10';
$num = $config['per_page'];
$offset = $this->uri->segment(4);
$offset = ( ! is_numeric($offset) || $offset < 1) ? 0 : $offset;
if(empty($offset)){
$offset = 0;
}
$this->pagination->initialize($config);
$news = $this->db->query($string_query." limit $offset,$num");
return $nes;
}
the generated links is 1 - 2 - 3, its should be no link because i only have 5 data
please help me on this, thank you
finally I fixed it with change the script with this :
function category($id){
$string_query = "SELECT * FROM news n JOIN category c ON n.category_id = c.id_category AND n.category_id = $id ORDER BY n.category_id DESC";
$query = $this->db->query($string_query);
$config['base_url'] = site_url('news/category/' . $id);
$config['total_rows'] = $query->num_rows();
$limit = $config["per_page"] = 10;
$config["uri_segment"] = 4;
$config["use_page_numbers"] = TRUE;
$this->pagination->initialize($config);
if($this->uri->segment(4))
$page = ($this->uri->segment(4)-1)*$limit;
else
$page = 1;
$news= $this->db->query($string_query." limit $page,$limit");
return $news;
}
Thank you

customized pagination in codeigniter

I have codeigniter applicatin which have pagination code that work like a charm. Pagination allow the visitor to select how much records he would like to see per page. Now, I would like to add the following:
displaying X to Y from total of Z records.
Now, Z is easy...i have that solved. Can anyone tell me how could I achive this?
Here is the function that i have:
public function displayAllUsers()
{
if ($this->uri->segment(3) !="") {
$limit = $this->uri->segment(3);
} else {
$limit = 10;
}
$offset = 4;
$offset = $this->uri->segment(4);
$this->db->limit($limit, $offset);
$data['users'] = $this->backOfficeUsersModel->get();
$totalresults = $this->db->get('back_office_users')->num_rows();
$this->session->set_userdata('totalresults', $totalresults);
//initializing & configuring paging
$this->load->library('pagination');
$config['base_url'] = site_url('/backOfficeUsers/displayAllUsers/'.$limit.'/');
$config['total_rows'] = $totalresults;
$config['per_page'] = $limit;
$config['uri_segment'] = 4;
$config['full_tag_open'] = '<div class="dataTables_paginate paging_bootstrap pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['cur_tag_open'] = '<li><a href=# style="color:#ffffff; background-color:#258BB5;">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
//....irelevant content.....
} // end of function displayAllUsers
Any help will be deeply appreciated.
It shouldn't be that hard. In fact, it's kinda obvious how you can get the value for X and Y if you understand pagination.
First of all, the difference between X and Y will of course be equal to $limit - 1. The value of X should be equal to the variable, $offset, plus 1 (Offset generally starts from 0 not 1). And from there, I'm sure that you understand that Y is equal to $offset + $limit.
$X = $offset + 1;
$Y = $offset + $limit;
In case, you're at the last page, you'll want to make sure you have the right value for Y. You'll need to check the total rows you grab from the database, if it's less than the limit per page, then change it to something like this
if (fetched_rows < $limit)
{
$Y -= ($limit - fetched_rows);
}

Resources