CodeIgniter Forums
get value from views and send it directly to model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: get value from views and send it directly to model (/thread-55901.html)



get value from views and send it directly to model - El Forum - 11-15-2012

[eluser]rebornishard[/eluser]
I just wanted to get User.name on pageview.php
i tried to join table but it didn't work
if i change get_post to get $id i can call User.name
how to show User.name with get_post() not with get_post($id)
thank you Smile


Database
Code:
User has , id, name, email
Post has , id, user_id, title, content

Model modelpage.php
Code:
function get_posts()
{
  $this->db->select('*')
  ->from('post');
  $query=$this->db->get();
  return $query->result_array();
}

Controller page.php
Code:
public function posts()
{
  $data['postdata']=$this->modelpage->get_posts();
  $this->load->view('pageview.php',$data);
}

View pageview.php
Code:
<?php
  foreach($postdata as $pd)
{
  echo $pd['title'];
  echo '<br>'
  echo $pd['content'];
}
?&gt;



get value from views and send it directly to model - El Forum - 11-15-2012

[eluser]jprateragg[/eluser]
How did you do your join? Like this:

Code:
$this->db->join('user', 'post.user_id = user.id');

Can you post the query that didn't work?


get value from views and send it directly to model - El Forum - 11-15-2012

[eluser]rochellecanale[/eluser]
Try to execute traditional query.
Code:
$sql = "SELECT * FROM member
           RIGHT JOIN sales ON member.id = sales.id";



get value from views and send it directly to model - El Forum - 11-15-2012

[eluser]Beginers[/eluser]
@rochellecanale -> Traditional query is not a good practice on doing the query. Active record was created so that it will become easier for us to do the query and more on that to have a lesser code.

@rebornishard -> I think your database has no data. Try to use
Code:
print_r($data['postdata'])
if it really fetches the data you want.


get value from views and send it directly to model - El Forum - 11-16-2012

[eluser]Aken[/eluser]
[quote author="Beginers" date="1353038429"]@rochellecanale -> Traditional query is not a good practice on doing the query. Active record was created so that it will become easier for us to do the query and more on that to have a lesser code.

@rebornishard -> I think your database has no data. Try to use
Code:
print_r($data['postdata'])
if it really fetches the data you want.[/quote]
Traditional query is perfectly fine. It's up to the developer. Active record is there simply for convenience; it isn't an expressed "best practice".

You shouldn't be calling models in your views. Fetch all the data you need in your controller, put it together in a way that your view expects, and then pass it to your view.


get value from views and send it directly to model - El Forum - 11-16-2012

[eluser]rebornishard[/eluser]
[quote author="jprateragg" date="1353032383"]How did you do your join? Like this:

Code:
$this->db->join('user', 'post.user_id = user.id');

Can you post the query that didn't work?[/quote]

that one worked if i view all post list
post 1 made by user A
post 2 made by user B
post 3 made by user A

but if i view in user side
user A made post 1
user A made post 3
user B made post 2

the result i wanted
user A made post 1, post 3
user B made post 2

and
user A total post 2
user B total post 1

thank you jprateragg

[quote author="rochellecanale" date="1353035756"]Try to execute traditional query.
Code:
$sql = "SELECT * FROM member
           RIGHT JOIN sales ON member.id = sales.id";
[/quote]
i already try that sir and then put that query in array but it still give me
user A made post 1
user A made post 3
user B made post 2

thank you rochellecanale

[quote author="Beginers" date="1353038429"]@rochellecanale -> Traditional query is not a good practice on doing the query. Active record was created so that it will become easier for us to do the query and more on that to have a lesser code.

@rebornishard -> I think your database has no data. Try to use
Code:
print_r($data['postdata'])
if it really fetches the data you want.[/quote]

i always print_r or var_dump to test it before
this is not about data but about result i wanted to get

thank you Beginers

[quote author="Aken" date="1353053900"][quote author="Beginers" date="1353038429"]@rochellecanale -> Traditional query is not a good practice on doing the query. Active record was created so that it will become easier for us to do the query and more on that to have a lesser code.

@rebornishard -> I think your database has no data. Try to use
Code:
print_r($data['postdata'])
if it really fetches the data you want.[/quote]
Traditional query is perfectly fine. It's up to the developer. Active record is there simply for convenience; it isn't an expressed "best practice".

You shouldn't be calling models in your views. Fetch all the data you need in your controller, put it together in a way that your view expects, and then pass it to your view.[/quote]

yes sir, i'm not using that, i just wanted to get this worked :

user A made post 1, post 3
user B made post 2

and
user A total post 2
user B total post 1

thank you Aken