0

Pagination and a Few More Items

Posted by Aaron on Dec 28, 2008 in Agrivation, CakePHP

Pagination is quite easy in CakePHP, until you need some WHERE clause filtering, then it is almost as easy. Notice the second parameter to the paginate() function. This is actually documented in the Cake Cookbook (http://book.cakephp.org/view/165/Controller-Setup @ the very bottom of the page), but I missed it the first read through.

controller: /app/controllers/adopt_controller.php

class AdoptController extends AppController {

        ...

public $paginate = array(
'limit' => 20,
'order' => array('Adopt.date_added' => 'DESC'),
'fields' => array('id', 'type', 'interest',
                    'your_name',
                        'UNIX_TIMESTAMP(date_added) AS date_added')
);

       ...

function index() {
$search = array();

if (!empty($this->data['Adopt']['interest'])) {
$search['interest LIKE'] = '%' .
                            $this->data['Adopt']['interest'] . '%';
}

if (!empty($this->data['Adopt']['your_name'])) {
$search['your_name LIKE'] = '%' .
                            $this->data['Adopt']['your_name'] . '%';
}

$this->set('apps', $this->paginate('Adopt', $search));
}

}


I was installing WordPress 2.7 on the same site as a CakePHP app. I installed WordPress to /app/webroot/blog/. I expected this to work just fine. The problem was using http://www.mydomain.com/blog/ (trailing slash) worked just fine, but http://www.mydomain.com/blog (no trailing slash) redirected me to http://www.mydomain/com/app/webroot/blog/. After quite some time of aggravation I finally fixed it by changing my /public_html/.htaccess files to this:


   RewriteEngine on

   RedirectMatch temp ^/blog/wp-admin$ http://www.furryfriendsrescue.org/blog/wp-admin/

   RewriteRule blog$ /blog/ [L]
   RewriteRule blog/wp-admin$ /blog/wp-admin/ [L]
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]

This is the standard CakePHP root .htaccess file, but with the RewriteRule for “blog” and “blog/wp-admin” added. I later found out I needed to add the RedirectMatch to have WP admin left nav work. Thanks to Jeff Loiselle’s post.


So as not to end on a negative note, I wanted to mention a cool FireFox Add-on I learned of. It si called smush.it. This add-on will analyze all the images on the page, running each through a series of compression algorithms. Then it lists each image, and the optimal format for each. You are given a complete report of each image and how much space is saved, along with a link to download a zip with all the new images.


You will have noticed the site has a new look. I upgraded to WordPress 2.7 so I figured I was time for a “fresh coat of paint”, and I did not have to bother my wife for a custom header image.

Share and Enjoy:
  • Digg
  • NewsVine
  • Reddit
  • Slashdot
  • Technorati
  • TwitThis
  • Yahoo! Buzz

Tags: ,

Copyright © 2010 Aaron Thies All rights reserved.
Desk Mess Mirrored v1.4.3.1 theme from BuyNowShop.com.