代码之家  ›  专栏  ›  技术社区  ›  Ahmad Tahhan

CodeIgniter联接数据库表

  •  0
  • Ahmad Tahhan  · 技术社区  · 7 年前

    我有两个数据库表,一个用于日志,另一个用于类别,每个日志都按类别ID分配给一个类别。

    我想在“文章”页上显示类别名称,在每一篇文章上我想显示分配给它的类别,所以我做了一个联接数据库表,类别名称通常显示在“主要文章”页上,但是当我单击一篇文章时,类别现在显示,我得到一个 Undefined index: name

    型号代码:

    <?php
      class Yacht_model extends CI_Model{
        public function __construct(){
          $this->load->database();
        }
        public function get_yachts($slug = FALSE){
          if ($slug === FALSE) {
            $this->db->join('categories', 'categories.cat_id = yachts.category');
            $query = $this->db->get('yachts');
            return $query->result_array();
          }
          $query = $this->db->get_where('yachts', array('slug' => $slug));
          return $query->row_array();
        }
        public function get_yachts_by_category($cat_id){
          $this->db->join('categories', 'categories.cat_id = yachts.category');
          $query = $this->db->get_where('yachts', array('category' => $cat_id));
          return $query->result_array();
        }
      }
     ?>
    

    控制器代码:

    <?php
      class Yachts extends CI_Controller{
        public function index(){
          $data['title'] = 'Yachts';
          $data['yachts'] = $this->yacht_model->get_yachts();
    
          $this->load->view('templates/header');
          $this->load->view('yachts/index', $data);
          $this->load->view('templates/footer');
        }
        public function view($slug = NULL){
          $data['yacht'] = $this->yacht_model->get_yachts($slug);
          if(empty($data['yacht'])){
            show_404();
          }
          $this->load->view('templates/header');
          $this->load->view('yachts/view', $data);
          $this->load->view('templates/footer');
        }
      }
     ?>
    

    发布的HTML代码:

    <section>
        <div class="rows inner_banner inner_banner_2">
            <div class="container">
                <h2><span><?php echo $yacht['name']; ?></span></h2>
                <ul>
                    <li><a href="#inner-page-title">Anasayfa</a>
                    </li>
                    <li><i class="fa fa-angle-right" aria-hidden="true"></i> </li>
                    <li><a href="#inner-page-title" class="bread-acti">Gulet Kiralama</a>
                    </li>
                </ul>
            </div>
        </div>
      </section>
    

    这个 $yacht['name']; 是从类别表中,它应该显示类别名称。

    4 回复  |  直到 7 年前
        1
  •  0
  •   user7126103    7 年前

    1)您需要使用post_id从数据库中获取该类别名称。

    2)您可以在会话中存储该类别和post-name映射数组,并且可以从会话数据中显示relavent post的类别名称。

        2
  •  0
  •   Dani    7 年前

    如果您有post和category表,则可以使用join或making函数访问它,该函数通过传递category id返回类别名称。 让我给你们俩看看

    第一个使用连接 在后模式中创建此函数

       function getPosts(){
                       $this->db->select('post.*,post_category.*')
                            ->from('post')
                            ->join('post_category','post_category.id=post.post_category','left');
                    $query = $this->db->get();
                    return $query->result_array();
         }
    

    类别模型写入函数中的第二个方法

    public function getCategoryById($id = Null){
            $query = $this->db->get_where('post_category',array('id'=>$id));;
            return $query->row()->category_title;
        }
    

    如果使用第一种方法,那么视图文件中的代码将类似

    <?php if(isset($posts) && count($posts) > 0){ 
            foreach($posts as $k => $val){
      ?>
    <tr>
        <td><?php echo $k++?></td>
        <td><?php echo $val['post_title']?></td>
        <td><?php echo $val['post_content']?></td>
        <td><?php echo $val['category_title']?></td>
        <td><?php echo $val['slug']?></td>
    </tr>
    <?php } } ?>
    

    如果要使用第二种方法,视图中的代码将类似

     <?php if(isset($posts) && count($posts) > 0){ 
            foreach($posts as $k => $val){
      ?>
    <tr>
        <td><?php echo $k++?></td>
        <td><?php echo $val['post_title']?></td>
        <td><?php echo $val['post_content']?></td>
        <td><?php echo $this->category_model->getCategoryById($val['post_category'])?></td>
        <td><?php echo $val['slug']?></td>
    </tr>
    <?php } } ?>
    

    希望对你有帮助

        3
  •  0
  •   Rudra    7 年前

    您可以尝试这个简单的查询。我希望这能如你所愿。

    $getData=$this->db->query("SELECT table1.*, table2.* FROM table1 JOIN table2 ON table1.id=table2.id and table1.id='1'");
    $result=$getData->result_array();
    
    echo '<pre>';
    print_r($result);
    
        4
  •  0
  •   Ahmad Tahhan    7 年前

    我找到了解决方案,我在if语句中创建了join表,默认情况下设置为false,这就是为什么在post页面中join不起作用,但是当我将它放在if语句之外时,它可以正常工作。

    之前:

    public function get_yachts($slug = FALSE){
          if ($slug === FALSE) {
            $this->db->join('categories', 'categories.cat_id = yachts.category');
            $query = $this->db->get('yachts');
            return $query->result_array();
          }
          $query = $this->db->get_where('yachts', array('slug' => $slug));
          return $query->row_array();
        }
    

    后:

    public function get_yachts($slug = FALSE){
          if ($slug === FALSE) {
            $this->db->join('categories', 'categories.cat_id = yachts.category');
            $query = $this->db->get('yachts');
            return $query->result_array();
          }
    $this->db->join('categories', 'categories.cat_id = yachts.category');
          $query = $this->db->get_where('yachts', array('slug' => $slug));
          return $query->row_array();
        }
    
    推荐文章