Form posting with '_GET' parameters

by Marcelius
A while ago, I was working on a searchengine for my site. When a search is performed, the url should show all the parameters so it is possible for user to add the search to their favorites. This code snippet shows how I accomplished this. More details of this problem are described here: [url]http://groups.google.com/group/cake-php/browse_thread/thread/d1c13c9338b8fe26[/url]
The trick is to submit a form using 'POST', and than redirect to an URL with named parameters (cake's alternative to regular _GET variables) as suggested by Stephen Orr. I havn't fully tested it so if you find any bugs, your free to make a comment about it.

Here's my code:

Controller Class:

<?php 
class MySearchController extends AppController{
    function 
search(){
        if (empty(
$this->data)){
            if (!empty(
$this->params["named"])){
                
//unserialize passed params
                
$this->data $this->unSerializeData($this->params["named"]);

                
//do search logic
                
$this->doSearchLogic();
            } else {
                
//do nothing, show search form
            
}
        } else {
            
//serialize and redirect
            
$searchParams $this->serializeData($this->data);
            
$this->redirect(array("action"=>"index"$searchParams));
        }
    }

        
/**
         * Converts named params to array
         *
         * @param array $namedParams
         * @return array
         */
        
function unSerializeData($namedParams){
            
$result = array();
 
            if (
is_array($namedParams)){
                foreach(
$namedParams as $fieldName=>$value){
                    
$field explode("."$fieldName);
                    if (
count($field) == 2){
                        
$result[$field[0]][$field[1]] = $value;
                    } elseif(
count($field) == 1) {
                        
$result[$field[0]] = $value;
                    }
                }
            }
 
            return 
$result;
        }
 
 
        
/**
         * Converts the this->data array to named parameters
         *
         * @param array $data
         * @return string
         */
        
function serializeData($data){
            
$result = array();
 
            if (
is_array($data)){
                foreach (
$data as $model=>$values) {
                    if (
is_array($values)){
                        foreach(
$values as $name=>$val){
                            
$result[] = sprintf("%s.%s:%s"$model$name$val);
                        }
                    } else {
                        
$result[] = sprintf("%s:%s"$model$values);
                    }
                }
            }
 
            
$result implode("/"$result);
 
            return 
$result;
        }
}
?>

Report

More on Snippets

Advertising

Comments

  • webcentro posted on 12/17/09 11:01:37 PM
    just created a simple component, and it works fine
    also integrated with ajax pagination, also tried another query string solutions but this is the one that fits bether

    Component Class:

    <?php 
    <?php 
    class SerializeComponent extends Object
    {
            
    /**
             * Converts named params to array
             *
             * @param array $namedParams
             * @return array
             */
            
    function unSerializeData($namedParams){
                
    $result = array();
     
                if (
    is_array($namedParams)){
                    foreach(
    $namedParams as $fieldName=>$value){
                        
    $field explode("."$fieldName);
                        if (
    count($field) == 2){
                            
    $result[$field[0]][$field[1]] = $value;
                        } elseif(
    count($field) == 1) {
                            
    $result[$field[0]] = $value;
                        }
                    }
                }
     
                return 
    $result;
            }
     
     
            
    /**
             * Converts the this->data array to named parameters
             *
             * @param array $data
             * @return string
             */
            
    function serializeData($data){
                
    $result = array();
     
                if (
    is_array($data)){
                    foreach (
    $data as $model=>$values) {
                        if (
    is_array($values)){
                            foreach(
    $values as $name=>$val){
                                
    $result[] = sprintf("%s.%s:%s"$model$name$val);
                            }
                        } else {
                            
    $result[] = sprintf("%s:%s"$model$values);
                        }
                    }
                }
     
                
    $result implode("/"$result);
     
                return 
    $result;
            }
    }
    ?>
    ?>

    Controller Class:

    <?php 
    <?php
    class UsersController extends AppController {

        var 
    $name 'Users';
        var 
    $helpers = array('Html''Form','Ajax');
        var 
    $components = array('RequestHandler','Serialize');
        var 
    $paginate = array(
            
    'limit' => 1,
            
    'order' => array(
            
    'User.nome' => 'asc',
            
    'User.sobrenome' => 'asc',
            )
        );
        var 
    $namedArgs TRUE
        
        function 
    search(){
            
    $where = array();
            if (empty(
    $this->data)){
                if (!empty(
    $this->params["named"])){
                    
    //unserialize passed params
                    
    $this->data $this->Serialize->unSerializeData($this->params["named"]);
                } else {
                    
    //do nothing, show search form
                
    }
            }
            
    $where $this->User->generateWhere($this->data);
            
        
    $this->User->recursive 0;
        
    $searchParams $this->Serialize->serializeData($this->data);
        
    $this->set('searchParams'$searchParams);
        
    $this->set('users'$this->paginate('User',$where));
        }
    ?>

    View Template:


    ...
    $paginator->options(array('url' => $searchParams));
    ...
  • harking posted on 03/10/09 04:39:29 PM
    This seems like it would be a valuable addition to the core as bookmarking form queries is a often used feature.
login to post a comment.