Welcome Guest, Not a member yet? Register   Sign In
Simple query left join question - MVC
#1

[eluser]Otemu[/eluser]
Hi,

Another simple question that I be grateful if anyone can respond. I have a left join query for my news index page, which basically lists various news stories(image, title and intro text), clicking on either the image or title takes you to the corresponding page with the full story.

Not all the time however a image is return, as its not a mandatory requirement, so I plan to have a default picture displayed. Currently I have a switch statement on the actual view checking this as shown below:

Code:
<?php
    foreach($newsListAll as $row){
    // if no image available issue...............
        switch ($row['imageurl']){
            case "":
                echo "<li>No Image Available</li>";
                break;                        
            default:
                echo "<li><a href='".base_url()."index.php/home/news/".$row['><img src='".base_url().$row[' alt='".$row[' /><a></li>";
                break;
        }
        echo "<li>".anchor('home/news'."/".$row['newsid']."/".$row['page_url'],$row['page_title'])."</li>";
    }
?&gt;

Wasn't sure if this was correct MVC practice to be checking this in the view, or if this should be handled on the actual controller or even the model itself.

Cheers
#2

[eluser]mdvaldosta[/eluser]
I generally try to keep as much as possible out of the view, including something like that. I want the views to be as simple to redesign as possible. Having said that, I'm not a purist and sometimes thing like that get put into views because it's easier. I think with a pure MVC approach, you'd want to handle that in the controller and spit it out with on foreach that also displays the News stories. Or even putting the image display in it's own helper function would be acceptable, so you can just call it from the view.
#3

[eluser]Otemu[/eluser]
Thanks, excellent advice, I think i add to the controller.
#4

[eluser]techgnome[/eluser]
I handled my similar situation in the model as part of my select statement:
Code:
$this->db->select("CASE WHEN ifnull(historylist.picture, '') = '' THEN 'na_01.jpg' ELSE historylist.picture END albumcover");
I did it like this, because it's part of the data logic as far as I am concerned. Neither the controller nor the view should care what the picture is... I did my best to keep the controllers fairly stupid and the views even dumber.

Not saying it's the best way or only way... just pointing out an alternative.

-tg




Theme © iAndrew 2016 - Forum software by © MyBB