[eluser]titanite[/eluser]
Hello,
I'm a novice at this, so please bear with me.
I'm doing this query for a movie review (stored in a table called movie_review), and in the review, there is normally a director whose name I store in another table called persons.
Now, there could be a second director, whose name I would like to show.
In non-MVC formats, I would do first do a query to retrieve the the movie review and if Director 2 exists, I would do another query.
Code:
print "$row[movietitle]";
print "by director $row[director1]";
if ($row[director2]) {
$query = "select first_name, last_name from persons where id = '$row[director2]'"; etc etc etc
print "and also by $first_name $last_name.";
}
In CI, this is my controller, which I name movie.php
Code:
<?php
class Movie extends Controller {
function Movie()
{
parent::Controller();
$this->load->helper('url');
$this->load->helper('form');
}
function index()
{
$data['title'] = "All Movies";
$data['heading'] = "All Movies";
$this->db->order_by("date", "desc");
$data['query'] = $this->db->get('movie_review');
$this->load->view('movie_listing_all', $data);
}
function movie_review()
{
$this->load->model('Movie_model');
$id = $this->uri->Segment(3);
$data['info'] = $this->Movie_model->get_review($id);
$this->load->view('movie_view', $data);
}
}
?>
My model is called movie_model.php
Code:
<?php
class Movie_model extends Model {
function Movie_model()
{
parent::Model();
}
function get_review($id)
{
$this->db->select('*');
$this->db->from('movie_review');
$this->db->where('movie_review.id', $id);
$this->db->join('authors', 'authors.id = movie_review.author');
$this->db->join('persons', 'persons.id = movie_review.director_1');
$q = $this->db->get();
$info = $q->row();
return $info;
}
}
?>
As you can see, I have done a join. However, this only works for director_1, I will not be able to do a second join for director_2.
Another problem is in the view file. How can I rename a variable so as to distinguish between the names of Director_1 and Director_2?
Code:
<h2>Title: <?php echo "$info->title"; ?></h2>
<h2>Marketed title: <?php echo "$info->title_marketed"; ?></h2>
<h3>Directed by: <?php echo "$info->first_name $info->last_name"; ?><?php if($info->director_2) { echo ", $info->first_name $info->last_name";} ?></h3>
<p>Review:</p>
<p><?php echo "$info->text;"?></p>
<p>Written by author <?php echo "$info->name"; ?></p>
<p>on date <?php echo "$info->date" ?></p>
Can anyone advise a stupido like me?