【问题标题】:codeigniter dynamically passing page url not working after page reload页面重新加载后,codeigniter 动态传递页面 url 不起作用
【发布时间】:2021-10-07 07:55:18
【问题描述】:

在我的 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/products,这使得产品页面显示所有没有类别的产品,谁能告诉我如何解决这个问题,提前谢谢

【问题讨论】:

  • 缺少最重要的部分,即材质过滤器的表单。

标签: php codeigniter codeigniter-3


【解决方案1】:

尝试在您的视图中使用 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 中传递的值作为函数参数检索

请注意,当您提交表单时,您不会向您的 products 函数参数发送值 这会导致错误 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);
}

希望能解决你的问题

【讨论】:

  • 兄弟,我们如何获得这个变量 $productName
  • 我们在 url 中传递它并将其作为方法参数获取,如果您查看链接,它看起来像这个域/控制器/方法/变量
  • 兄弟我试过你的代码,但我得到以下错误 Homecontroller::products() 函数的参数太少,C:\xampp\htdocs\shopping\system\core\CodeIgniter.php 中传递了 0在第 532 行,正好是 1 个预期
  • 你有没有试过像这样让产品功能参数等于null $productName = null
【解决方案2】:

也试试

<form method="post">

没有action属性来保持当前的url。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2017-11-03
    • 1970-01-01
    • 2020-05-02
    • 2014-03-28
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多