Welcome Guest, Not a member yet? Register   Sign In
new to MVC
#1

[eluser]jochemvn[/eluser]
Hello,

I'm new to the MVC concept and I have a question. I have a controller a model and a view. Now in this controller I load my model and load some data into a variable. However it's possible that my model doesn't return any rows.

Where should I check this? I think I have 2 options:

1) The controller
Check if the variable I pass the data to is empty or not and load a different view for when it's empty.

2) The view
Check if the variable that it's given is empty and create two different sections of HTML in the view (one for when the var is empty and one for when there's data to show)

Which is the best, or is there an other option?
#2

[eluser]sofwan[/eluser]
I think you should choose the number 1 option, The Controller.
Because, function of a controller is to write a logical and your view just display the result.
#3

[eluser]jochemvn[/eluser]
thanks for you answer....

however this would mean, that for every view I write, I should make a 2nd view, for when there's no data to show... am I correct?
#4

[eluser]sofwan[/eluser]
You don't need to make 2nd views, just using 1 view, 1 variable but different value. One value is for filled data, another for empty data.
I have an example :

Model :contoh.php

<?php
class Contoh extends model
{
function tampil()
{
$query="select * from countries where value='Endonesia'";
$hasil=$this->db->query("$query");
return $hasil;
}
}

?>

Controller :contoh_contr.php
<?php
class Contoh_contr extends Controller
{
function contoh_contr()
{
parent::controller();
$this->load->model("contoh");
}

function index()
{
$hasil=$this->contoh->tampil();
if ($hasil->num_rows()>0)
{
$hasilnya=$hasil->row();
$data["negara"]=$hasilnya->value;
}
else
{
$data["negara"]="No country";
}
$this->load->view("tampil_hasil",$data);
}
}

?>

View : tampil_hasil.php

<?php
echo "Nama Negara : $negara";

?>

But for another example, getting an array of a table, it results 2 different kind values, one is array (Data is exist) another is a single variable (No data inside). You can add if .. else in your view to display the value.
#5

[eluser]jochemvn[/eluser]
Thanks again for your quick response...

You last remark is exactly what I was thinking...

[quote author="sofwan" date="1278076345"]
But for another example, getting an array of a table, it results 2 different kind values, one is array (Data is exist) another is a single variable (No data inside). You can add if .. else in your view to display the value.[/quote]

However I wasn't sure if that was correct in the MVC structure, but aparanteley it is, right?

Check if the variable is an array. If it isn't then display some message (no data available) otherwise loop throught the array and display the data
#6

[eluser]sofwan[/eluser]
Although a Controller is for logical and a View is for display, but a View can write for logical too but we keep to not so many put it there to keep MVC advantages and principles and that's one of CI for "You want a framework that does not require you to adhere to restrictive coding rules."

Actually if for one reason we put in a View so many logicals (controller) and Query (model) too, a program can still run well.But recommended only for certain reason.

For result an array and single variable, here is the example :

View : tampil_hasil.php

<?php

if ($negara=="No Country")
{ echo “Remark : $negara”; }
else
{
foreach ($data as $row)
{
// Our array put here
}
}
?>




Theme © iAndrew 2016 - Forum software by © MyBB