Multiple functions for several joins in a query - how to do it? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Multiple functions for several joins in a query - how to do it? (/showthread.php?tid=29765) |
Multiple functions for several joins in a query - how to do it? - El Forum - 04-20-2010 [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]"; In CI, this is my controller, which I name movie.php Code: <?php My model is called movie_model.php Code: <?php 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> Can anyone advise a stupido like me? Multiple functions for several joins in a query - how to do it? - El Forum - 04-20-2010 [eluser]Mischievous[/eluser] Not sure about what your exactly trying to do but I always setup my DB requests like this: Code: function fetch_review($where, $join, $type = 'array') Code: $where = array('id' => $id); As for the naming, setup a format_result() function and run through the result and add your prefix's as needed d1_director, d2_director etc. etc. Multiple functions for several joins in a query - how to do it? - El Forum - 04-20-2010 [eluser]Federico BaƱa[/eluser] You can make a second join to the persons table by director_2 on the reviews table, in the case you dont have a second director, all the values that should match would return NULL. Also you'd need to set field aliases to avoid ambiguity. Code: <?php This way, a row on movie_review has a NULL director_2, the columns on the result set (director_2_name in the example) will be NULL as well. There on the view you use a conditional to know if there's or there's not a second director. Code: if($info[$i]->director_2_name !== NULL){ echo 'This review has a second director' } Hope this helps you... Multiple functions for several joins in a query - how to do it? - El Forum - 04-21-2010 [eluser]titanite[/eluser] Dear Mischevious: You are WAY too advanced for me... I'll study your method carefully and find out why you do it that way. Federico: Thank you so much for your help! It was exactly what I needed to learn! However, I have run into one error. In the cases where $info->director_2 is NULL, I have the "Message: Trying to get property of non-object" error message. When director_2 is not null, I have his name printed. Why is there such an error? Code: <h3>Directed by: <?php echo "$info->director_1_firstname $info->director_1_lastname"; ?><?php if($info->director_2_firstname !== NULL){ echo ", $info->director_2_firstname $info->director_2_lastname"; } ?></h3> Multiple functions for several joins in a query - how to do it? - El Forum - 04-22-2010 [eluser]titanite[/eluser] Hi guys, I don't get it. Why is it that when the database entry is NULL, I get this error message: A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: libraries/Loader.php(673) : eval()'d code Line Number: 9 I've checked the Loader.php file, it seems that the problem is with short tags? What's wrong? |