EllisLab text mark
Advanced Search
     
get value from views and send it directly to model
Posted: 15 November 2012 04:54 PM   [ Ignore ]
Avatar
Joined: 2011-06-12
21 posts

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

User has idnameemail
Post has 
iduser_idtitlecontent 

Model modelpage.php

function get_posts()
{
  $this
->db->select('*')
  ->
from('post');
  
$query=$this->db->get();
  return 
$query->result_array();

Controller page.php

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

View pageview.php

<?php
  
foreach($postdata as $pd)
{
  
echo $pd['title'];
  echo 
'<br>'
  
echo $pd['content'];
}
?> 
 
Posted: 15 November 2012 10:19 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-11-01
81 posts

How did you do your join? Like this:

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

Can you post the query that didn’t work?

 
Posted: 15 November 2012 11:15 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-08-24
214 posts

Try to execute traditional query.

$sql "SELECT * FROM member
           RIGHT JOIN sales ON member.id = sales.id"
 Signature 

Our greatest glory is not in never falling, but in rising every time we fall.

cheese
Email: .(JavaScript must be enabled to view this email address)
Mobile Number: +639216015372

 
Posted: 16 November 2012 12:00 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-08-12
80 posts

@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

print_r($data['postdata']

if it really fetches the data you want.

 
Posted: 16 November 2012 04:18 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts
Beginers - 16 November 2012 12:00 AM

@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

print_r($data['postdata']
if it really fetches the data you want.

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.

 
Posted: 16 November 2012 09:13 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2011-06-12
21 posts
jprateragg - 15 November 2012 10:19 PM

How did you do your join? Like this:

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

Can you post the query that didn’t work?

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

rochellecanale - 15 November 2012 11:15 PM

Try to execute traditional query.

$sql "SELECT * FROM member
           RIGHT JOIN sales ON member.id = sales.id"

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

Beginers - 16 November 2012 12:00 AM

@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

print_r($data['postdata']
if it really fetches the data you want.

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

Aken - 16 November 2012 04:18 AM
Beginers - 16 November 2012 12:00 AM

@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

print_r($data['postdata']
if it really fetches the data you want.

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.

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