Hi;

The code below dynamically creates queries, retrieves the data from DB and returns the results.

I am pretty sure there are almost unlimited ways to achieve the same results and every coder has his her own style but a few things here:

1 - What's the first thing you get from this code? "Good code done properly" or "newbie trying too hard"?

2 - Is it ok to use rely on sessions for "user search data"? As you can see I am passing the data within controllers.

3 - I can make the same thing MUCH MUCH messier, does it matter much what the code looks like? Or it's enough if it just works fine.

4 - How would you improve this?

5 - Data has been sanitised.

PHP Code:
class Results extends CI_Controller {
    
    protected 
$where='';
    protected 
$search_data='';
    protected 
$value='';
    protected 
$sql='';
    
    public function 
search_results($link=NULL,$record_start=0)
        {
            
$this->load->model('model_products');
            
$this->search_data $this->session->userdata('data');
            
$this->sql['record_start'] = $record_start;
            
            
##Create ORDER BY##
            
if($link=='prev')
                {
                    
$this->sql['next_prev_sign'] = " < ";
                    
$this->sql['order'] = "DESC";
                }
            else
                {
                    
$this->sql['next_prev_sign'] = " > ";
                    
$this->sql['order'] = "ASC";
                }
            
##Create ORDER BY end##
            
            ##Create WHERE##
            
$conditions $values = array();
            if(
array_key_exists("price_from"$this->search_data ))
                {
                    
$conditions[] = "price BETWEEN ? AND ?";
                    
$this->values[] =  $this->search_data['price_from'];
                    
$this->values[] = $this->search_data['price_to'];
                    unset(
$this->search_data['price_from']);
                    unset(
$this->search_data['price_to']);
                }
            foreach(
$this->search_data as $val=>$row)
                {
                    
$conditions[] = $val." = ?";
                    
$this->values[] = $row;
                }
            if(
$conditions)
                {
                    
$this->where " AND " implode(" AND "$conditions);
                }
            
##Create WHERE end##
            
            ##Retrieve data from model
            
if($link=='prev')
                {
                    
$data['results'] = array_reverse($this->model_products->search_results($this->search_data$this->sql$this->where$this->values));
                }
            else
                {
                    
$data['results'] = $this->model_products->search_results($this->search_data$this->sql$this->where$this->values);
                }
            
##Retrieve data from model end
            
            ##Links and pagination
            
if($this->next_link($data['results'], 'search_results'))
                {
                    
$data['next'] = $this->next_link($data['results'], 'search_results');    
                }
            else
                {
                    
$data['next'] = "";    
                }
            
            if(
$record_start!=0)
                {
                    
$data['prev'] = $this->previous_link($data['results'], 'search_results');    
                }
            else
                {
                    
$data['prev']='';    
                }
            
$this->load->view('header');
            
$this->load->view('results',$data);
            
$this->output->enable_profiler(TRUE);    
        }
        
    protected function 
next_link(&$array$source)
        {
            
$tmp=end($array);
            return 
"<a class = \"yellow\" href=\"".base_url('results/'.$source.'/next/'.$tmp['id'])."\">Next Page >></a>";
        }
    protected function 
previous_link(&$array$source)
        {
            
$tmp=reset($array);
            return 
"<a class = \"yellow\" href=\"".base_url('results/'.$source.'/prev/'.$tmp['id'])."\"><< Previous Page</a>";
        }    

Model
PHP Code:
public function search_results($data$sql,$where$values)
    {
        
$select_sql "SELECT q2.id,
               q2.code,
               q2.class,
               q2.category,
               q2.price,
               q2.production_date,
               q4.title AS STATUS
        FROM   (SELECT q1.id,
                       q1.status,
                       code,
                       class,
                       category,
                       price,
                       production_date
                FROM   products_table AS q1
                WHERE  q1.id  "
.$sql['next_prev_sign']." ".$sql['record_start']."
                "
.$where."
                ORDER  BY q1.id "
.$sql['order'].
                LIMIT  20) AS q2
               INNER JOIN products_table AS q3
                       ON q3.id = q2.id
               JOIN products_status AS q4
                 ON q4.id = q3.status "
;
                
        
$results $this->db->query($select_sql$values); 
        return 
$results->result_array();
    } 
Thanks