1

我需要在 codeigniter 中获取其类别的子类别。我被困在将类别 ID 传递给我的模型方法

我的代码如下:

这是我的观点:index.php

<ul class="list-inline">

                    <?php 
                        if($category->num_rows() > 0)
                        {
                            foreach ($category->result() as $row) 
                            {
                                ?>
                                    <li><a href="<?php echo base_url();?>main/getsubcategory<?php $row->id; ?>"><img class="img-responsive center-block" width="80px" src="<?php echo base_url(); ?>assets/admin/uploads/category/<?php echo $row->cat_home_image; ?>" alt="icon" title="icon" class="img-responsive" /><p style="font-size:16px;"><?php echo $row->category_description; ?> </p></a></li>

                                <?php 
                            }
                        }
                    ?>

                </ul>

这是我的控制器:Main.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {

    public function index()
    {
        $this->load->model("category_model");
        $data["category"] = $this->category_model->fetch_category();
        $this->load->model("ads_model");
        $data["ads"] = $this->ads_model->fetch_home_ads();
        $this->load->view('index',$data);
        //$this->load->view('category');
    }

    public function getsubcategory()
    {       
        $this->load->model("category_model");
        $data["subcategory"] = $this->category_model->fetch_subcategory();
        $this->load->view('category',$data);
    }
}

这是我的模型:category_model.php

    <?php 

class Category_model extends CI_Model
{

    function fetch_category()
    {
        $query = $this->db->query("SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id IS NULL && lang_id='1' && c.id!='4'  ORDER BY category_description");
        return $query;
    }

    function fetch_subcategory()
    {
        $subcat = $this->db->query("SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id='1' && lang_id='1'  ORDER BY category_description");
        return $subcat;
    }
}

?>

如何将类别id传递给模型以获得category_id的适当子类别

4

1 回答 1

1

希望对你有帮助 :

view应该是这样的:

<ul class="list-inline">
  <?php 
      if($category->num_rows() > 0)
      {
          foreach ($category->result() as $row) 
          {?>
            <li>
               <a href="<?=site_url('main/getsubcategory/'.$row->id);?>">
               <img class="img-responsive center-block" width="80px" src="<?=site_url('assets/admin/uploads/category/'.$row->cat_home_image); ?>" alt="icon" title="icon" class="img-responsive" />
               <p style="font-size:16px;"><?php echo $row->category_description; ?> </p>
               </a>
            </li>

          <?php 
          }
      }
  ?>
</ul>

你的控制器的getsubcategory方法应该是这样的:

public function getsubcategory($id)
{    
    if ($id) 
    {   
      $this->load->model("category_model");
      $data["subcategory"] = $this->category_model->fetch_subcategory($id);
      $this->load->view('category',$data);
    }
}

你的模型方法fetch_subcategory应该是这样的:

注意:确保所有列名属于正确的表别名

function fetch_subcategory($id)
{
    $this->db->select('*');
    $this->db->from('category c'); 
    $this->db->join('category_language cl', 'cl.category_id = c.id');

    /* Use $id here like this
       $this->db->where('parent_id', $id);
    */

    $this->db->where('c.parent_id', '1');
    $this->db->where('cl.lang_id', '1'); 
    $this->db->order_by('c.category_description', 'DESC');
    $result = $this->db->get()->result();
    return $result;
}
于 2018-08-04T05:52:42.197 回答