Welcome Guest, Not a member yet? Register   Sign In
How can I Modify data from model to view?
#1

[eluser]cleansugar[/eluser]
controller:
Code:
function detail($name)
    {
        $data['result'] = $this->hello_model->getDetail($name);
        foreach ($data['result'] as $row)
           {
               if ($row->sex == 0){
                  $row->sex = 'female';
              }else{
                  $row->sex = 'male';
              };
}
    foreach ($data['result'] as $row)
        {
              if ($row->bigo != null){
                  $this->load->view('bigodetail_view',$data);
              }else{
                  $this->load->view('detail_view',$data);
              };
           }
model:
Code:
function getDetail($name)
    {
        $this->db->where('name', $name);
        $query = $this->db->get('table1');
        
        if ($query->num_rows() == 0)
        {
            //show_error('Database is empty!');
        }else{
            return $query->result();
        }
    }

If I want to make and add to this obeject another 'havecar' property and pass to view, what can I do?

Should I make another new object? Is there any way not to make another one and modifying old one? How can I make another new object and copy it from old one?

Please help...
#2

[eluser]kgill[/eluser]
Ok I'm missing something here, where's the first havecar property, you said you want another but I don't see the first in your code. Another thing, your 2nd foreach are you you meaning to load the view multiple times with the same data?
#3

[eluser]cleansugar[/eluser]
first property is sex. I want to add a havecar property to object $data['result'].
2nd foreach is just a bug. never mind, please.
#4

[eluser]kgill[/eluser]
Ok now that I understand what you're trying to do, just add the column to your table, your model is pulling all details for a name. So once you put it in the table, the select is going to return name, sex, havecar and whatever else you've got in there. Now we need to address your code, if you're going to be changing booleans to male, female - the way you're doing it isn't going to work. Your foreach is looping through the result and copying each row in the the $row variable, but when the loop moves to the next row the data in $row is overwritten and lost. Either you need to build a new object or you modify the data in $data['result'] directly.
#5

[eluser]mimran[/eluser]
Hi Cleansugar,
From reading your post I am sensing there are a couple of things unclear for you in that code, so i am going to explain each part and hopefully that should resolve the issue for you.

Firstly lets have a look at your model:

MODEL:
function getDetail($name)
{
$this->db->where('name', $name);
$query = $this->db->get('table1');

if ($query->num_rows() == 0)
{
//show_error('Database is empty!');
}else{
return $query->result();
}
}

Explanation:

The code above is essentially taking a parameter (Name) and then inquiring your database table against that name. So if you are familiar with SQL then simply this is what you are doing:

select *
from table1
where name = $name

And that's it. So now what i sense is unclear for you is that you don't have the structure of table in your head and can't picture what you are getting back from this code.

So lets say the table you are inquiring 'table1' it looks like this:
Table1:
Name Gender HaveCar PhoneNumber
John Male Yes 123455
Sara Female No 111111

What your query doing now is:
Lets say you passed ‘John’ as parameter
It is returning the whole row as an array which includes (john, male, yes, 123455)
So that was your model.
Now controller is sampling calling this function in your model and getting this full array.
I hope that clarifies any doubts you had.
Regards
mimran
#6

[eluser]cleansugar[/eluser]
Thank you for your reply.

I don't want to change DB. I want to change only passing data to view.

original DB:
name
sex

model:
return $query->result();

controller:
name, sex from model
add $data['db']->HaveCar
send name, sex, HaveCar to view

View:
echo name, sex, HaveCar

What can I do?
#7

[eluser]mimran[/eluser]
Hi Again,

One request please forget MVC pattern or code, just explain in simple language what you are trying to achieve and how would you do it stepwise.

Cause the issue here is your database is only storing name and sex, what is the mechanism of creating a havecar?

don't worry about model, controller or view for now please. Just explain how is it happening and then we can help probably.

regards
#8

[eluser]cleansugar[/eluser]
original DB:
name
sex

model:
return $query->result();

controller:
name, sex from model
add $data[‘db’]->HaveCar
foreach ($data['db'] as $row){
$row->haveCar = 0; <-no havecar property error will occur. That's what I want to know.
}
send name, sex, HaveCar to view

View:
echo name, sex, HaveCar

I want to make havecar is inputted just 0 in controller.
In other words, I want to add HaveCar=0 property to $data['db'] object in controller.
#9

[eluser]cleansugar[/eluser]
Anybody to response?

Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB