在我的 codeigniter 網站中,我創建了一個產品頁面并使用 url 中的類別名稱顯示其中的產品:
<?php foreach($category as $cat): ?>
<li><a href="<?php echo base_url()?>homecontroller/products/<?php echo $cat->name;?>"><?php echo $cat->name;?></a></li>
<?php endforeach; ?>
這作業正常。現在要過濾產品,我在控制器中執行了以下操作:
public function products()
{
$id = $this->uri->segment(3);
$material = $this->input->post('material1');
$material2 = $this->input->post('material2');
$data['products'] = $this->product->findAll($id,$material,$material2);
$this->load->view('products', $data);
}
我的模型:
function findAll($id,$material,$material2)
{
$this->db->where('cname', $id);
if(!empty($material) || !empty($material2)) {
$this->db->or_where('material', $material);
$this->db->or_where('material', $material2);
}
return $this->db->get('product')->result();
}
和我的過濾器產品視圖:
<form action="<?php echo base_url()?>products" method="post">
<input type="checkbox" name="material1" value="cotton" class="form-check-input filled-in" id="new" onChange="this.form.submit()">
<input type="checkbox" name="material2" value="polyster" class="form-check-input filled-in" id="used" onChange="this.form.submit()">
</form>
這里的問題是,當用戶選擇過濾器選項時,頁面會重新加載并進行過濾,但 url 成為我的基本 url/產品,這使產品頁面顯示所有沒有類別的產品,誰能告訴我如何解決這個問題,提前致謝
uj5u.com熱心網友回復:
嘗試在您的視圖中使用 site_url 而不是 base_url,base_url 從您的配置中回傳基本 URL site_url 使用 index.php 提供正確的 URL
<form action="<?php= site_url('homecontroller/products')?>" method="post">
<input type="checkbox" name="material1" value="cotton" class="form-check-input filled-in" id="new" onChange="this.form.submit()">
<input type="checkbox" name="material2" value="polyster" class="form-check-input filled-in" id="used" onChange="this.form.submit()">
</form>
也嘗試在這里使用它
<?php foreach($category as $cat){?>
<li><a href="<?= site_url('homecontroller/products/'.$cat->name)?>"><?= $cat->name;?></a></li>
<?php }?>
您應該考慮將 URL 中傳遞的值作為這樣的函式引數進行檢索
請注意,當您提交表單時,您不會向產品函式引數發送值,這會導致函式 Homecontroller::products(), 0 傳遞的引數太少的錯誤
你可以解決這個問題
public function products($productName = null)
{
$id = $productName;
$material = $this->input->post('material1');
$material2 = $this->input->post('material2');
$data['products'] = $this->product->findAll($id,$material,$material2);
$this->load->view('products', $data);
}
我希望它能解決你的問題
uj5u.com熱心網友回復:
也試試
<form method="post">
沒有操作屬性來保持當前的 url。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315050.html
