Autoload Config for Pagination in CodeIgniter not working - codeigniter

I am trying to implement pagination in my CI webapp. Now I put the config for pagination inside a config file like this...
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = "http://example.com/index.php/home/index";
$config['num_links'] = "9";
$config['per_page'] = "20";
$config['total_rows'] = "200";
/* End of file pagination.php */
/* Location: ./system/application/config/pagination.php */
In my controller, I have loaded the library
$this->load->library("pagination");
And I have defined the pagination config file to be autoload in config/autoload.php
$autoload['config'] = array('pagination');
At last I called the method to create links in my view template:
<?php echo $this->pagination->create_links(); ?>
This did not create any links. The configuration is being autoloaded correctly. I checked using...
<?php echo $this->config->item("num_links"); ?> <!-- this dislayed 9 -->
What am I missing here? Just for the record, putting the config inside the controller didn't work either.
Update #1- I have found out that the config settings are loading correctly but they are not reaching the library or something like that. Inside the pagination library - I did some hard coding to find out that per_page parameter was zero in there.
Update #2- I was mistaken when I said that putting the config inline wasn't working. It is working fine. The autoload isn't working.
Regards

Finally used this code to solve my problem...
$this->config->load("pagination");
$page_limit = $this->config->item("per_page");
$config['total_rows'] = $var; // Some variable count
$this->pagination->initialize($config);
This lets me define the config items in a file as well as initialize the items I want in controller like in my case, the total no. of rows - retrieved from database.
Regards

Your autoload line in your config file should be this
$autoload['libraries'] = array('pagination');
And you must have this line in you controller after your config array, before you use create_links() etc.
$this->pagination->initialize($config);

Related

Change Codeigniter url

I'm using Codeigniter.I want to set href attr to something like :
<a href="/contact.html" >Contact</a>
But i get 404 error because i should write
Contact.
Where is some thing to fix this problem.
Any help please.
Assuming that you have a controller by the name Contact and you successfully extend the CI_Controller class, go to application/config folder and in config.php find:
$config['base_url'] = 'http://www.youdomain.com/';
Then in your internal links you should do:
Contact
If you are using javascript to make the redirect, put on top of the js file:
var host = 'http://www.yourdomain.com/';
Again:
window.location.href = host + 'contact';
If you're using codeigniter, you do not want to point to an .html file.
If you're using codeigniter correctly, you should use the helper methods that exist in codeigniter.
Instead of writing the anchor tag yourself, try this:
<?php echo anchor('contact', 'Contact'); ?>
to add the suffix to your controller in calling go to config/config.php and search for
$config['url_suffix'] = '';
and assign html to it to become
$config['url_suffix'] = 'html';

Pagination on Pyrocms pages

I am currently displaying a list of products on pyrocms page. Using plugin i get the products from db tables and display them as a list i want to use pagination but have no idea how to use it and where to start from. Does any one know any tutorial or have some suggstion?
You won't need to load pagination library or initialize it. It is a little different from you do in regular codeigniter, also you can surely use the way you do it in codeigniter pagination class
I usually do this for pagination.
in your controller use this
....
// Create pagination links
$total_rows = $this->products_m->count_all_products();
$pagination = create_pagination('products/list', $total_rows);
//notice that product/list is your controller/method you want to see pagination there
// Using this data, get the relevant results
$params = array(
'limit' => $pagination['limit']
);
$this_page_products = $this->product_m->get_all_products($params);
....
//you got to pass $pagination to the view
$this->template
->set('pagination', $pagination)
->set('products', $this_page_products)
->build('products/view');
obviously, you will have a model named products_m
At your model this would be your get_all_products function, I am sure you can build the count_all_products() function too
private function get_all_products($params){
//your query to get products here
// use $this->db->limit();
// Limit the results based on 1 number or 2 (2nd is offset)
if (isset($params['limit']) && is_array($params['limit']))
$this->db->limit($params['limit'][0], $params['limit'][1]);
elseif (isset($params['limit']))
$this->db->limit($params['limit']);
//rest of your query and return
}
Now at your view you have to foreach in your passed $products variable and to show pagination links use this line in your view
<?php echo $pagination['links'];?>
I use this in my works, hope it help.
Pyrocms used codeigniter framework this code works perfectly in case of module but i have never tried this on a plugin but still you can try this.
Load pagination library in Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Use this code in view to genrate the links
echo $this->pagination->create_links();

CodeIgniter: howto override Pagination configuration in controller code?

I recently jumped on the CodeIgniter train because I finally want to embrace the MVC architecture for my growing PHP/MySQL projects.
Right now I encounter a probolemin the PAgination configuratino that is apparently not described earlier as far as I can ssee.
Probably the issue is not PAgination only related but more general. In my first attempt, I just filled the confiuration in the controller method and passed it to the initialize() method:
$config['base_url'] = $this->config->base_url() . "/categorie/{$catName}/";
$config['total_rows'] = $this->event_model->get_nrofevents_for_categorie($categorieObject->id, TRUE);
$config['per_page'] = 12;
$config['full_tag_open'] = '<div class="paging">';
$config['full_Tag_close'] = '</div>';
$config['prev_link'] = 'Vorige';
$config['next_link'] = 'Volgende';
$this->pagination->initialize($config);
And this works fine. But I have a buch of other pages that use the same pagination with most of its parameteres identical, only the base_url and the total_rows config properties are different for each page.
I have put the common configuration in the config/pagination.php configuration file but see no option to add the page-dependent properties in the code.
More general, is it possible to put generic configuration in a config file and add some customized properties in the controller method?
For me this seems logical but cannot figure out how...
I tried something like:
$this->load->config('pagination');
$this->config->set_item('next_link', 'blablabla');
but it seems that the Pagination is initialized immediately after reading the config file and this code has no effect.
Please any suggestions?
Since the initialize() only replaces the keys you provide, you can have your config/pagination.php hold the default values and call initialize() with the changing ones.
config/pagination.php
// put default values here
$config['next_link'] = 'Volgende';
$config['prev_link'] = 'Vorige';
// ...
controller
$this->pagination->initialize(array(
'base_url' => base_url().'categorie/'.$catName.'/',
'total_rows' => $totalRows,
// ...
)));
don't use base_url()... I always use site_url();
$config['base_url'] = site_url('');

can I use joomla's onAfterRender for a module rather than a plugin?

I want to insert some code to Joomla when any page is loaded.
For this I created a module that inserts code.
I am trying to use
<?php
// $Id: helper.php
defined('_JEXEC') or die;
jimport( 'joomla.plugin.plugin' );
jimport( 'joomla.environment.response' );
class modInsertCode
{
function onAfterRender($params)
{
$code = 'some code';
$documentbody = JResponse::getBody();
$documentbody = str_replace ("</body>", $code." </body>", $documentbody);
JResponse::setBody($documentbody);
return true;
}
}
?>
but JResponse::getBody(); returns an empty string. Any ideas, solutions of fixes to this code?
Thank you,
You have to do it using a plugin, you won't be able to do it using a module because the HTML response has not been generated by the time the code of the module gets executed.
I hope it helped!
I know this is a bit old but for future reference this can be done with jQuery:
$doc = JFactory::getDocument();
$js = 'jQuery(document).ready( function() {
jQuery("#module'.$module->id.'").appendTo(document.body);
})';
$doc->addScriptDeclaration($js);
This is assuming that you have wrapped the content in you module in something like the following, including the module id to support multiple instances of the module.
<div id="module<?php echo $module->id; ?>"> Your content </div>

Magento - link to a frontend skin js file from an admin file

I'm already using jQuery in my frontend skin files. I've now added some extra functionality in an admin phtml file, that also needs to use jQuery. I don't want to have to include it twice, but how can I link to the existing jQuery file dynamically, assuming I don't know what theme package name is being used on the frontend (because obviously that can change, so I don't want it hardcoded)?
For example, I tried this, but it gives me the admin theme package name, not the frontend package:
<?php
// Get the package name
$configData = Mage::getStoreConfig('design');
$package = $configData['package']['name'];
?>
Anyone?
OK, this seems to be working. I can access the db directly, look for the "design/package/name" in the core_config_data table and then build the url to the js file using that:
<?php // Get the current theme being used, so we can build the url link to our jQuery file
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$tablename = Mage::getSingleton('core/resource')->getTableName('core_config_data');
$results = $connection->fetchAll("SELECT * FROM $tablename WHERE path='design/package/name';");
foreach($results as $row) { $theme = $row['value']; };
?>
<script type="text/javascript" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN).'frontend/'.$theme; ?>/default/js/jquery.min.js"></script>
for me helped to set $storeId as 1 not 0
$package = Mage::getStoreConfig('design/package/name', 1);
$theme = Mage::getStoreConfig('design/theme/default', 1);

Resources