Bug with pagination when 2 variables in the url on CodeIgniter - codeigniter

I am still learning with the CodeIgniter framework. Today, I find myself faced with the problem of pagination.
Indeed I would have access to data via an id which requires a variable in the url and another one for the pagination.
This is my foos_by_day function (this is not the real name, it's just an example) in my controller called foo that returns an array of foos on a specific day.
These foos come from my database and go to my controller get_foos_by_day ($day) via the model called model_foos.
function foos_by_day($day) {
$configpages['base_url'] = 'http://www.exemple.com/ci/index.php/foo/foos_by_days/'.$foo."/";
$configpages['total_rows'] = count($this->model_foos->get_foos_by_day($foo));
$configpages['per_page'] = 10;
$config['uri_segment'] = 4;
$this->pagination->initialize($configpages);
$data['foos'] = $this->model_foos->get_foos_by_day($jour, (int)$this->uri->segment(4), $configpages['per_page'] );
$this->load->view('foo_view.php', $data);
}
And this is my view:
<?php foreach ($foos as $element): ?>
<div>
Name of the foo : <?=$element->name?><br />
Date of the foo : <?=$element->date?><br />
</div>
<?php endforeach; ?>
<?php echo $this->pagination->create_links(); ?>
I get something strange in my view when I input : http://www.example.com/ci/index.php/foo/foos_by_day/2011-09-28/
The numbering of my 32 foos starts at 4 (the last page). It displays correctly 10 foos per pages but when I click on another page, the selected page stays the fourth.
Moreover, the links behind the page numbers gives me the following statement:
First
<
2
3
<strong>4</strong>
So : nothing, then 20, 10 and 20 (again). In short there is a bug in the pagination.
Have you ever been faced to this kind of situation (with a pagination error)? If so, how did you solve it?
How do you think I should proceed to find a regular pagination?
Thanks in advance.
P.S. : I didn't delete the index.php file but I have learned to do it on an other CodeIgniter.

Change
$config['uri_segment'] = 4;
to
$configpages['uri_segment'] = 4;

Related

How to convert CodeIgniter urls into user friendly ones?

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>
I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?
You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.
Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

How do i display the selected value in multiselect while editing and updating the options(codeigniter)

How do i display the selected value in multiselect while editing and updating the options.
my multiselect function is:
foreach ($subjects as $sub){
$subject_selected[]=$sub['subject_name'];
}
echo form_multiselect('subject[]',$subject_list,'',$js,$subject_selected); ?>
if I m wrong please correct me..
It should be like
echo form_multiselect('subject[]',$subject_list,$subject_selected,$js); ?>
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected. The parameter usage is identical to using form_dropdown() above, except of course that the name of the field will need to use POST array syntax, e.g. foo[].
I have searched for this thing and have failed so many times. But at last find a way around to resolve the issue.
This is the way that I have came across with.
<?php
$manpower_list = array();
$manpowers = $this->db->select('m.id, m.manpower_name')
->from('task_manpower tm')
->join('manpower m', 'tm.manpower_id = m.id')
->where('tm.deleted', 0)->where('m.deleted', 0)
->where('tm.task_id', $model_info->id)
->get()->result();
foreach ($manpowers as $power) {
$manpower_list[] = $power->id;
}
?>
<?php echo form_dropdown("manpower_id[]", $manpower_dropdown, $manpower_list, "class='select2 validate-hidden form-control' id='manpower_id' multiple='multiple'"); ?>
Hope it helps someone.

Magento and decimels in the cart

I'm changed some functions in magento to strip decimals in price.
The solution appears to work on simple products with no options, but the products with options still display the .00 when the option is chosen. Ironically the drop down for the option displays the additional cost of the option without the decimels, but the main price with the option selected still shows the decimels. Could this be in a js file? configurable.js has reloadOldPrice() method i tried to dump it, but the price var is always 0
any ideas?
NEW EDIT
My previous code won't work indeed. I tested the following and it works:
// Wrap original reloadPrice function
spConfig.reloadPrice = spConfig.reloadPrice.wrap(function(original){
// Call original reloadPrice() function
original();
// Get the DOM-element that contains the price
var priceSpan = $('product-price-'+this.config.productId).down();
// Get the current value
var oldP = priceSpan.innerHTML;
// Change the value
var newP = oldP.sub('.00','');
// Update the element
priceSpan.update(newP);
});
In Magento the spConfig object defined like var spConfig = new Product.Config(...);, so be sure to add the code I give here after instantiation of spConfig.
There is still work to be done:
I'd recommend changing the var line newP = oldP.sub('.00',''); to something that also catches ,00, since in some locales that will be the price format.
Furthermore, if you choose to, for example, show prices including and excluding tax the code above won't work because $('product-price-'+this.config.productId).down() will contain two elements (I think).
If you rather append the code to the configurable.js file, you should append it like:
Product.Config.prototype.reloadPrice = Product.Config.prototype.reloadPrice.wrap(...);
(notice the .prototype which I forgot in my first answer).
OLD POST (doesn't work)
In case people wonder why it doesn't work, first of all it should've been Product.Config.prototype.formatPrice instead of Product.Config.formatPrice and, secondly, the formatPrice function apparently isn't responsible for how the price html is outputted.
configurable.js also has a function formatPrice, which is probably called when the price is updated.
So you could try:
Product.Config.formatPrice = Product.Config.formatPrice.wrap(function(originalFormatPrice, price, showSign) {
var str = originalFormatPrice(price, showSign);
return str.slice(0, -3); // remove last three characters (.00)
});
**Go your price.phtml file**
line no 201
<?php echo $_coreHelper->currency($_price, true, true) ?>
replace this code
<?php $_prix = $_coreHelper->currency($_price,true,true) ?>
<?php $_prix = str_replace(".00", "", $_prix); ?>
<?php echo $_prix ?>
Try to use free extension ET Currency Manager. In this extension this function is realized.

How to echo page number with codeigniter pagination library?

I am using the codeigniter pagination library to page my search results. I am using a limit and an offset to generate the results.
I would like to know how, apart from in the pagination links, echo the number of the page the user is currently viewing.
For example;
You are currently viewing page X out of Y results.
How is this possible??
This also gives the current page no
$currentPage = floor(($this->uri->segment(n)/$config['per_page']) + 1);
where n is the segment
echo "Current Page:".$currentPage;
You could extend the Pagination class (system/libraries/Pagination.php).
Create a file called MY_Pagination.php and put it in application/libraries/
Add this code to your extend pagination class;
class MY_Pagination extends CI_Pagination {
public function __construct() {
parent::__construct();
}
public function current_place() {
return 'You are currently viewing page '.$this->cur_page.' out of '.ceil(($this->total_rows/$this->per_page)). 'results';
}
}
Don't forget to make sure the class extension prefix is set correctly to MY_ in the config.php (about line 109)
Then you can access it in your controller like;
$current = $this->pagination->current_place();
Which you can pass into your view.
Depending on how you are setting up the offset. Ours is setup with an offset of the perpage. So our default is 10 per page. So for page one the offset is 0. For page two the offset is 10 and so on. So for me to get the current page I would take the offset divided by the current limit plus 1. So ($cur_page/$per_page)+1 would get me the current page.

Changing the URL/URI of pagination in Codeigniter

I'm using the pagination class in Codeigniter to page out some info from my database.
It's all working perfectly, however, i'm trying to tidy up the URLs that people use to access the pages.
Currently, i'm pulling out the offset from the URL and have set it to 20 results per page:
http://www.mysite.com/page/20
So if you go to (in this instance) page two, it starts from db entry 20.
The trouble is that firstly, since i've set it to 20 results per page, it means I get odd URLs like http://www.mysite.com/page/60 which would be page 4.
Secondly it's a double figure which i'd like to tidy up.
Basically want I want to do is set the URLs to look like:
http://www.mysite.com/page/2
http://www.mysite.com/page/3
http://www.mysite.com/page/4
etc...
I set the use page numbers to TRUE, but the trouble is it then sets the offset to 2, 3, 4 etc when it should be 20, 40, 60.
Any ideas? I was thinking of some kind of calculation in the model to work out how many entries it should offset based on the individual page number from the URL (when use page numbers is set to TRUE). I can't seem to get it right though...
In codeigniter number in url means: row per page
So if you need to change per page number with page number change your code like this:
Controller
class news extends CI_Controller{
function index($page = 'page', $page_number = 0){
$row_per_page = 20; // define page per page
// for example if page number == 4 then offset 80
$offset = $page_number * $row_per_page
// we need to request data from database
// we need this query "SELECT * FROM `mytable` limit 80,20"
// offset = 80 AND per page = 20
$this->db->get('mytable', $offser, $row_per_page);
$this->load->library('pagination');
// you can change this url by URI Routing and other solutions.
/*
change application/config/routes.php to nice url
$route['news/page/(:num)'] = "news/index/page/$1";
*/
$config['base_url'] = 'http://example.com/index.php/news/index/page/';
// Change $count_of_rows with your query num_rows you can use SQL_CALC_FOUND_ROWS
$config['total_rows'] = $count_of_wors;
$config['per_page'] = $row_per_page;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
This is very simple. Enjoy CI

Resources