gpt4 book ai didi

php - Codeigniter,使用 URL 作为查询参数

转载 作者:可可西里 更新时间:2023-10-31 23:38:07 25 4
gpt4 key购买 nike

我正在使用 codeigniter,我正在尝试创建一个页面,如果 url 输入 example.com/mobil/bekas/toyota/avanza,它会显示所有以 toyota 为品牌和 avanza 为型号的二手车,如果 url输入 example.com/mobil/bekas/toyota 它会显示所有以 toyota 为品牌的二手车。

这是我的 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);
}

这是模型:

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 时它不会显示任何内容

最佳答案

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

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();

关于php - Codeigniter,使用 URL 作为查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35713387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com