4

i am using codeigniter, i am trying to create a page where if the url entered example.com/mobil/bekas/toyota/avanza it shows all used car that has toyota as brand and avanza as model, and if the url entered example.com/mobil/bekas/toyota it shows all used car that has toyota as brand.

Here is my controller :

public function bekas($brand_nama,$model_nama='NULL')
        {      
               $this->load->model('listing_model');
               $data['cars'] = $this->listing_model->viewListingByBrandAndModel($brand_nama, $model_nama);
               $this->load->view('product_listing.php', $data);
        }

Here is the model :

function viewListingByBrandAndModel($brand_nama, $model_nama)
    {

        $this->load->library('pagination');
            $this->load->library('table');
            $config['base_url'] = 'http://example.com/mobil/bekas/'.$brand_nama.'/'.$model_nama;
            $config['total_rows'] = $this->db->select('*')
                            ->join('car_list_tbl','car_list_tbl.car_list_ID = user_listing_tbl.car_list_ID')
                            ->join('member_tbl','member_tbl.mID = user_listing_tbl.mID')
                            ->join('model_tbl','model_tbl.model_ID = car_list_tbl.model_ID')
                            ->join('series_tbl','series_tbl.series_ID = car_list_tbl.series_ID')
                            ->join('body_type_tbl','body_type_tbl.body_type_nama = car_list_tbl.body_type_nama')
                            ->join('brand_tbl','brand_tbl.brand_name = car_list_tbl.brand_name')
                            ->where('car_list_tbl.brand_name',$brand_nama)
                            ->like('model_tbl.model_nama', $model_nama)
                            ->where('user_listing_tbl.listing_type','BEKAS')
                            ->get('user_listing_tbl')->num_rows();
            $config['per_page'] = 20;
            $config['num_links'] = 10;
            $config['display_pages'] = TRUE;
            $config['full_tag_open'] = '<ul class="pagination">';
            $config['full_tag_close'] = '</ul>';
            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['first_link'] = FALSE;
            $config['last_link'] = FALSE;
            $config['prev_link'] = false;
            $config['next_link'] = false;
            $config['next_tag_open'] = '<li><a href="#"><i class="fa fa-chevron-left">';
            $config['next_tag_close'] = '</i></a></li>';
            $config['prev_tag_open'] = '<li><a href="#"><i class="fa fa-chevron-right">';
            $config['prev_tag_close'] = '</i></a></li>';
            $this->pagination->initialize($config);

             //Pagination End

            $sql = $this->db->select('*')
                            ->join('car_list_tbl','car_list_tbl.car_list_ID = user_listing_tbl.car_list_ID')
                            ->join('member_tbl','member_tbl.mID = user_listing_tbl.mID')
                            ->join('brand_tbl','brand_tbl.brand_name = car_list_tbl.brand_name')
                            ->join('model_tbl','model_tbl.model_ID = car_list_tbl.model_ID')
                            ->join('series_tbl','series_tbl.series_ID = car_list_tbl.series_ID')
                            ->where('car_list_tbl.brand_name',$brand_nama)
                            ->like('model_tbl.model_nama', $model_nama)
                            ->where('user_listing_tbl.listing_type','BEKAS')
                            ->get('user_listing_tbl', $config['per_page'], $this->uri->segment(5));
            return $sql->result();

I am still newbie at web programming, can i have inputs on which part i am lacking ? Because it works when i type example.com/mobil/bekas/toyota/avanza but it wont show anything when i type example.com/mobil/bekas/toyota

2 Answers 2

1

1 ) You are passing NULL as a string in parameter

2) Please use the if condition base on the $model_name at the time of the database query. Do not pass the extra conditions in query with like model_name LIKE '';

$this->db->select('*')
    ->join('car_list_tbl','car_list_tbl.car_list_ID = user_listing_tbl.car_list_ID')
    ->join('member_tbl','member_tbl.mID = user_listing_tbl.mID')
    ->join('brand_tbl','brand_tbl.brand_name = car_list_tbl.brand_name')
    ->join('model_tbl','model_tbl.model_ID = car_list_tbl.model_ID')
    ->join('series_tbl','series_tbl.series_ID = car_list_tbl.series_ID')
    ->where('car_list_tbl.brand_name',$brand_nama);

if($model_nama){
    $this->db->like('model_tbl.model_nama', $model_nama);
}
    $this->db->where('user_listing_tbl.listing_type','BEKAS');
    ->get('user_listing_tbl', $config['per_page'], $this->uri->segment(5));
return $this->db->result(); 
Sign up to request clarification or add additional context in comments.

Comments

0

Your function declaration has a small syntax error. The value NULL is a special value in PHP and as such, should not be surrounded in quotes. For this reason, you were querying the database for the type 'toyota' with the make 'NULL' rather than NULL. Changing your controller code should fix the issue:

 public function bekas($brand_nama,$model_nama=NULL) 
 {
 //...
 }

2 Comments

Thanks for the info, but it still wont show anything if i type in example.com/mobil/bekas/toyota/
Make condition if ($model_nama == NULL){/* just grab the $brand */} else {/* grab both */}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.