在codeigniter中按类别ID获取结果

时间:2018-08-04 05:30:26

标签: php codeigniter codeigniter-3 codeigniter-query-builder

我需要在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替代

1 个答案:

答案 0 :(得分: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;
}