[eluser]Schneider707[/eluser]
I created a model which fetches two pieces of info from a row and saves them to variables. How would I be able to use those variables in my controller? I see the example in the userguide of how to pass them to a view, but I don't think that would work for me.
Here is the model.
Code: function twitter_info_public()
{
$user = $this->uri->segment(1);
$this->db->where('username', $user);
$query = $this->db->get($this->table, 1);
if($query->num_rows() == 1)
{
foreach ($query->result_array() as $row)
{
return $twitterid = $row['twitter_id'];
return $twittername = $row['twitter_name'];
}
}
}
And here is the controller...well part of it
Code: function index()
{
$this->load->library('twitter');
$this->profile->twitter_info_public();
$config = array(
'userid' => '$twitterid',
'username' => '$twittername',
'type' => 'mine',
'num' => 5,
'rss_cache_path' => APPPATH.'cache/twitter-rss-cache',
'cache_time' => 300 // 5 minutes
);
// example for organikdesigns = http://twitter.com/statuses/user_timeline/16020929.rss"
// $config['type'] can be 'mine' (just your own tweets),
// or 'social' (includes people you follow)
$this->twitter->init($config);
See where $twitterid and $twittername is? Thats what im working on. I know its gotta be something like $this->load->vars or something...I just can't seem to find a question like mine.
Ty for the help =)
[eluser]TheFuzzy0ne[/eluser]
You have some serious errors in your model. Firstly, there are two return statements. A return statement makes the function return back to the caller, so the second one will never be reached. Also, unless it's in an if statement, it's pointless having one in an foreach statement.
As you're only expecting a single row, you don't need to use a foreach statement.
Here's a version of your code that may work:
Code: <?php
function twitter_info_public($username)
{
$this->db->where('username', $username); // Set the WHERE condition
$query = $this->db->get($this->table)->row; // Return the result row
}
function index()
{
if ( ! $this->uri->segment(3))
{
// Then there's no third segment, so do something here.
}
else
{
$this->load->model('profile'); // Load the model.
$ti = $this->profile->twitter_info_public(); // Get the data from the model method
$this->load->library('twitter'); // Load the library
$config = array(
'userid' => $ti->twitterid, // Variables won't work in single quotes.
'username' => $ti->twittername,
'type' => 'mine',
'num' => 5,
'rss_cache_path' => APPPATH.'cache/twitter-rss-cache',
'cache_time' => 300 // 5 minutes
);
// example for organikdesigns = http://twitter.com/statuses/user_timeline/16020929.rss"
// $config['type'] can be 'mine' (just your own tweets),
// or 'social' (includes people you follow)
$this->twitter->init($config
...
The above code is untested.
I've made a lot of guesses and assumptions here, so don't expect the code to work, but it should get you started.
[eluser]Schneider707[/eluser]
Ok so here is my new controller
Code: class Profiles extends Controller
{
function Profiles()
{
parent::Controller();
$this->load->model('Profile_Model', 'profile');
}
function index()
{
if ( !$user = $this->uri->segment(1))
{
$user = "home";
}
//START of twitter section
$ti[] = $this->profile->twitter_info_public($user); // Get the data from the model method
print_r($ti);
$this->load->library('twitter'); // Load the library
$config = array(
'userid' => $ti->twitterid,
'username' => $ti->twittername,
'type' => 'mine',
'num' => 5,
'rss_cache_path' => APPPATH.'cache/twitter-rss-cache',
'cache_time' => 300 // 5 minutes
);
// $config['type'] can be 'mine' (just your own tweets),
// or 'social' (includes people you follow)
$this->twitter->init($config);
$data['tweets'] = $this->twitter->get_latest();
//END of twitter section
if($this->template['profile'] = $this->profile->get($user))
{
$this->load->vars($this->template);
$this->load->view('profile.php', $data);
}
}
and my new model
Code: class Profile_Model extends Model
{
function Profile_Model()
{
parent::Model();
$this->table = "users";
}
function get($user)
{
$this->db->where('username', $user);
$query = $this->db->get($this->table, 1);
if($query)
{
if($query->num_rows() == 1)
{
return $query->row_array();
}
else
{
$data['error_msg'] = "The user <strong>".$this->uri->segment(1)."</strong>, does not exist! Please check your spelling.";
$this->load->view('error', $data);
}
}
}
function twitter_info_public($user)
{
$this->db->where('username', $user); // Set the WHERE condition
$query = $this->db->get($this->table, 1); // Return the result row
return $query->row_array();
}
I made changes to what Fuzzy0ne posted. I'm getting this error:
Code: A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/profiles.php
Line Number: 24
and when I print_r($ti) in the controller, i get this
Code: Array ( [0] => Array ( [id] => 1 [username] => tester [password] => 7110eda4d09e062aa5e4a390b0a572ac0d2c0220 [fname] => Mike [lname] => Schneider [email] => [email protected] [twitter_id] => 16020929 [twitter_name] => organikdesigns [company] => [address] => [number] => ) )
So what am I doing wrong? I'm able to fetch the data so its down to me sucking at php in general =P
[eluser]TheFuzzy0ne[/eluser]
You've put the row object into an array. Please remove the square brackets.
Code: $ti[] =
// should be
$ti =
[eluser]TheFuzzy0ne[/eluser]
Oh, and you've changed the model to return an array instead of an object.
Please wait. Working on the code now.
[eluser]TheFuzzy0ne[/eluser]
Controller:
Code: class Profiles extends Controller
{
function Profiles()
{
parent::Controller();
$this->load->model('Profile_Model', 'profile');
}
function index()
{
if ( !$user = $this->uri->segment(1))
{
$user = "home";
}
//START of twitter section
$ti = $this->profile->twitter_info_public($user); // Get the data from the model method - changed
print_r($ti);
$this->load->library('twitter'); // Load the library
$config = array(
'userid' => $ti['id'], // changed
'username' => $ti['username'], // changed
'type' => 'mine',
'num' => 5,
'rss_cache_path' => APPPATH.'cache/twitter-rss-cache',
'cache_time' => 300 // 5 minutes
);
// $config['type'] can be 'mine' (just your own tweets),
// or 'social' (includes people you follow)
$this->twitter->init($config);
$data['tweets'] = $this->twitter->get_latest();
//END of twitter section
if($this->template['profile'] = $this->profile->get($user))
{
$this->load->vars($this->template);
$this->load->view('profile.php', $data);
}
}
Model:
Code: class Profile_Model extends Model
{
function Profile_Model()
{
parent::Model();
$this->table = "users";
}
function get($user)
{
$this->db->where('username', $user);
$query = $this->db->get($this->table, 1);
if($query)
{
if($query->num_rows() == 1)
{
return $query->row_array();
}
else
{
$data['error_msg'] = "The user <strong>".$this->uri->segment(1)."</strong>, does not exist! Please check your spelling.";
$this->load->view('error', $data);
}
}
}
function twitter_info_public($user)
{
$this->db->where('username', $user); // Set the WHERE condition
$query = $this->db->get($this->table, 1); // Return the result row
return $query->row_array();
}
The above code is untested.
[eluser]Schneider707[/eluser]
Fantastic, works like a charm
Thanks much TheFuzzy0ne... =)
[eluser]Colin Williams[/eluser]
No reason the model should load a view.
[eluser]TheFuzzy0ne[/eluser]
Well spotted. I hadn't even noticed, as in my original redraft, there was no view loaded in the model method. I agree with Colin, though. Your model should only return data.
|