我正在使用codeigniter,我正在尝试创建一个页面,如果网址输入example.com/mobil/bekas/toyota/avanza它显示所有二手车有丰田作为品牌和avanza作为模型,如果网址输入example.com/mobil/bekas/toyota它显示所有使用丰田作为品牌的二手车。
这是我的控制器:
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);
}
以下是模型:
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();
我仍然是网络编程的新手,我可以输入我缺少的部分吗?因为它在我键入example.com/mobil/bekas/toyota/avanza时有效,但当我键入example.com/mobil/bekas/toyota
时它不会显示任何内容答案 0 :(得分:1)
1)您将NULL
作为参数
2)请在数据库查询时使用$ model_name的if条件。不要在查询中传递额外条件,例如model_name LIKE&#39;&#39;;
$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();
答案 1 :(得分:0)
您的函数声明语法错误很小。值NULL
是PHP中的特殊值,因此不应该用引号括起来。因此,您使用make 'toyota'
而不是'NULL'
向数据库查询类型NULL
。
更改控制器代码应解决问题:
public function bekas($brand_nama,$model_nama=NULL)
{
//...
}