1

Im working with a custom PHP MVC application

I retrieved a list of posts using this method:

MODELs: videos_model.php

class Index_Model extends Model {
    public function fetchVideos(){      
        $stmt = $this->db->prepare("SELECT * FROM bv_videos");
        $stmt->setFetchMode(PDO::FETCH_OBJ);
        return $stmt->fetchAll();

    }
}

CONTROLLERs : video.php class Index extends Controller {

    public function index(){    

        $this->view->render('../../public/templates/header');       
        $this->view->Videos         = $this->model->fetchVideos();      
        $this->view->render('index/index');
        $this->view->render('../../public/templates/footer');
    }
}

Views :

<div class="entry-header video_frame_description">
    <h3><?php echo $value->title; ?></h3>

    <p class="pull-left post-info">           
        <span class="cat-links hidden-xs">
            <a href="<?php echo URL; ?>?cat=<?php echo $value->cat_id; ?>">
            Category: 
            ** HERE I WANT TO ADD CATEGORY NAME UNDER EACH POST **
            </a>
     </p>
</div>

In the view, I want to show the name of the video category under each post.

I created this function in the index class

public function get_video_category($cat_id){        

        $stmt = $this->db->prepare("SELECT * FROM  bv_categories WHERE cat_id=?");
        $stmt->execute(array($cat_id));
        $stmt->setFetchMode(PDO::FETCH_OBJ);    
        $data = $stmt->fetch();
        return $data->category;
}

And then in the Controller I added this line:

$this->view->video_category = $this->model->get_video_category();       

And here the problem. What parameter should I pass the get_video_category() function??

Here are the tables structure im using:

bv_videos

CREATE TABLE IF NOT EXISTS bv_videos ( video_id int(11) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL, url text NOT NULL,
cat_id int(11) NOT NULL,
date_added int(11) NOT NULL, status int(11) NOT NULL,
PRIMARY KEY (video_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;

bv_categories

CREATE TABLE IF NOT EXISTS bv_categories ( cat_id int(11) NOT NULL AUTO_INCREMENT, category varchar(100) NOT NULL, order_cat int(11) NOT NULL DEFAULT '100', PRIMARY KEY (cat_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

1 Answer 1

1

I would extend this query

"SELECT * FROM bv_videos"

to

SELECT v.*, c.category as category_name FROM bv_videos v, bv_categories c WHERE v.cat_id = b.cat_id and v.cat_id = " . $cat_id;

or using left join

and then in view would just call category same way as you use category id

 <a href="<?php echo URL; ?>?cat=<?php echo $value->cat_id; ?>">
Category name is: $value->category_name;

Your way:

$this->view->video_category = $this->model->get_video_category();  

you need to enter category id, which seems to be $value->cat_id; if you are calling from view

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot , It works perfectly using the left join :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.