Archive for the ‘etc’ Category

CakePHP: complex SELECT queries

Wednesday, September 3rd, 2008

As the project grows I had to work on some more complex queries to provide users with better searching facilities.

In this case, you might use two options:

  • Straightforward find() function from App::Model (where you’ll have to handle the outpu of data yourself, and trying to fit the search results in your websites layout)
  • Use Pagination functionality (which is designed for handling big chunks of data for you)

A simple example: from a small search menu, I need to get the data about item’s price, its type etc, so at this point, find() solution would look like:

$condition = array(‘OR’ => array(
                   ‘Item.type’   => $this->data[‘Item’][‘type’],
                   ‘Item.qty’     => $this->data[‘Item’][‘qty’],
                              ),
  array(‘Item.price BETWEEN ? AND ?’ => array($start_price,$end_price))
);
$this->(‘results’, $this->Item->find(‘all’, $condition));

Once you set results array in the view template, will cause you few hours on how to rearrange data presentation, meanwhile you can use :

/*
* I’m going to use the same $condition
*  the difference will be at the view level and the way of setting the data "results"
*/

$this->set(‘results’, $this->paginate(‘Item’, $condition);

And in the view you might add some code like:

<?
echo $paginator->counter(array(
‘format’ => __(‘%page% of %pages%, showing %current%
records out of %count% total, starting on record %start%, ending on %end%’
, true)
));

foreach($results as $i => $item):
/*
* Items output
*/

endforeach;
?>
<? echo $paginator->prev(‘<< ‘.__(‘Previous Page’, true),
array(), null, array(‘class’=>‘disabled’));?>
 |     <? echo $paginator->numbers();?>
    <? echo $paginator->next(__(‘Next Page’, true).‘ >>’,
 array(), null, array(‘class’=>‘disabled’));?>

Last lines of the code would manage the results listing for you, which has to be defined in your Controller:

var $paginate = array(
                         ‘Item’ => array(
                           ‘limit’ => 5,
                           ‘order’=> array(‘Item.added’ => ‘ASC’)
                           )
            )

Done, now you can easily handle your search outputs. ;)

CakePHP: lists, beautiful bits

Monday, August 18th, 2008

These tiny bits of beauty really make the development enjoyable:

$names = $this->find(‘list’, array(
‘conditions’=> null,
‘order’       => ‘Developer.id ASC’,
‘fields’       => array(‘Developer.id’,‘Developer.’.$name.),
‘recursive’ => 0
));

as the result, getting:

Array = (
[1] => ‘Pafilia’,
[2] => ‘Vashiotis’
);

I’ll miss these features if I move from this framework :)

CakePHP: Dynamic select boxes with AJAX

Thursday, August 7th, 2008

Working with CakePHP1.2 I had to implement some techniques in the framework, mainly dealing with onChange behavior of the form components, here is a small example of how we can use multiple selectboxes.

Note: For simplicity reasons, I didn’t include queries, and just used option-arrays:

//inside controller
<?
       class FooController extends AppController{
                var $name = ‘Fooes’;
                var $components = array(‘RequestHandler’);
                var $helpers = array(‘Html’,‘Form’,‘Javascript’,);

              function beforeRender(){
                          if($this->RequestHandler->isAjax()){
                                   Configure::write(‘debug’,0);       
                                  //prevent useless warnings for
                         
                          }
                          $this->set(‘foobar’,null);
                         // initiate an array of options (otherwise, you’ll get a warning)

             }
             function updateDistricts(){
                                   $this->layout = ;
                                   $this->beforeRender();
                                   $this->render(‘updateDistricts/’,);
            }

      }
?>

This is just a simple layout for calling methods, without database, sofisticated layout handling, routes etc (for these reasons we got API, and GoogleGroup)

//inside viewer (test-data): <em>foo/update_districts.ctp</em>
<option>Limassol</option>
<option>Nicosia</option>
<option>Larnaca</option>
<option>Famagusta</option>

The actual form would look like:

//this code was used as an element from /view/elements/*.ctp

<ul id="navlinks">
                 <li><? echo $form->create(‘foo’);?></li>
                 <li>
                         <?
         print $form->input(‘Districts.name’,
                                     array(‘type’=>’select’,
                                      ‘options’=>array(‘Limassol’,‘Nicosia’,‘Larnaka’),
                                       ‘id’=>‘district_name’,
                                       ‘empty’=>‘Choose District’,
                                       ‘label’=>‘District’));
       print ‘<span class="ajax_update" id="ajax_indicator" tyle="display:none;">’.$html->image(-loader.gif’).‘</span>’;
            ?>
                 </li>
                 <li>
                   <?
       print $form->input(‘Towns.name’,
                                   array(‘type’=>’select’,
                                   ‘options’=> $foobar,
                                   ’style’=>‘display:none’,
                                    ’showEmpty’=>true,
                                  ‘empty’=>‘City’,
                                   ‘id’=>‘city_name’));
                                                                 
        print $->observeField(‘district_name’,
                                   array(  ‘url’=>‘updateTowns/’,
                                   ‘update’=>‘city_name’,
                                  ‘loading’=>"Element.show(’city_name’);           
                                  Element.show(’ajax_indicator’)"
,
                                  ‘complete’=>"Element.hide(’ajax_indicator’);
                                 Effect.Appear(’city_name’)"
,
                                 ‘onChange’=>true))
         ?>                                         
         </li>           
         <li><? echo $form->end(‘Search’);?></li>
</ul>

As the result, once you change the default value of the first select box, the second one automatically calls the controller’s method, which renders the values (gets the option list from the view file) and renders in manner

.

A little bit of success

Tuesday, May 20th, 2008

Just passed my driving test:

  • Now I got wheels
  • Now have to drink less.

Real Microsoft Vista code leaked!

Thursday, May 8th, 2008
/*
TOP SECRET Microsoft(c) Project:Longhorn(TM) SP1
Estimated release date:2008
*/
#include “win95.h”
#include “win98.h”
#include “leopard.h”

char chew_up_some_ram[10000000];

void main () {
while (!CRASHED) {

if (first_time_install) {
make_10_gigabyte_swapfile();
do_nothing_loop();
search_and_destroy(FIREFOX | OPENOFFICEORG | ANYTHING_GOOGLE);
hang_system();
}

if (still_not_crashed) {
basically_run_windows_xp();
do_nothing_loop();
}
}

if (!DX10GPU()) {
set_graphics(aero, very_slow);
set_mouse(reaction, sometimes);
}

// printf(”Welcome to Windows 2000?);
// printf(”Welcome to Windows XP”);
printf(”Welcome to Windows ”);

while (something) {
sleep(10);
get_user_input();
sleep(10);
act_on_user_input();
sleep(10);
flicker_led_promisingly(hard_disk);
}

creat_general_protection_fault();
}

Brands show your intelligence

Monday, January 21st, 2008
and now a study proves that “Mac people” indeed are more liberal and open-minded than average folks.

If you’re using PC, you’re conservative and close-minded average person living on this planet. The quote is a bit stupid, isn’t ?