Codeigniter,使用URL作为查询参数

时间:2022-08-16 11:23:55

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.

我正在使用codeigniter,我正在尝试创建一个页面,如果网址输入example.com/mobil/bekas/toyota/avanza它显示所有二手车有丰田作为品牌和avanza作为模型,如果网址输入示例。 com / mobil / bekas / toyota它显示了所有使用丰田作为品牌的二手车。

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

我仍然是网络编程的新手,我可以输入我缺少的部分吗?因为它在我键入example.com/mobil/bekas/toyota/avanza时有效,但当我键入example.com/mobil/bekas/toyota时它不会显示任何内容

2 个解决方案

#1


1  

1 ) You are passing NULL as a string in parameter

1)您在参数中传递NULL作为字符串

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 '';

2)请在数据库查询时使用$ model_name的if条件。不要在查询中传递额外条件,例如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(); 

#2


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:

您的函数声明语法错误很小。值NULL是PHP中的一个特殊值,因此不应该用引号括起来。出于这个原因,您使用make'NULL'而不是NULL来查询数据库中的'toyota'类型。更改控制器代码应该可以解决问题:

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

#1


1  

1 ) You are passing NULL as a string in parameter

1)您在参数中传递NULL作为字符串

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 '';

2)请在数据库查询时使用$ model_name的if条件。不要在查询中传递额外条件,例如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(); 

#2


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:

您的函数声明语法错误很小。值NULL是PHP中的一个特殊值,因此不应该用引号括起来。出于这个原因,您使用make'NULL'而不是NULL来查询数据库中的'toyota'类型。更改控制器代码应该可以解决问题:

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