Framework : CI
Situation:
I have this very frustrating problem in codeigniter.
here is the code: (Controller)
class sampleController extends CI_Controller {
function index() {
data['message'] = 'something';
data['js'] = array('script1.js', 'script2.js');
data['css'] = array('style1.css', 'style2.css', 'style3.css');
$this->load->model('user_model');
$user = $this->input->post('username');
data['user_id'] = $this->user_model->getUserIdByUsername($user);
data['user_info'] = $this->user_model->getAllUserInfo($user);
$this->load->view('someview', $data);
}
}
okay there goes my controller, i passed several data like js, css, user_id and user_info
here are the methods in my model:
getUserIdByUsername($user) {
$this->db->select("id");
$this->db->where("username", $user);
$result = $this->get("users");
return $result->row();
}
getAllUserInfo($user) {
$this->db->select("first_name, last_name, age");
$this->db->where("username", $user);
$result = $this->get("users");
return $result->row();
}
The problem occurs in the view, when i tried to access all the variables that has been passed by the controller
- if i tried echoing
$message, it will render out correctly like this =something if i tried looping on each using foreach
$jsand$csslike thisforeach($js as $key){//echo script here}foreach($css as $key){// echo link rel stylesheet here}
it will render out correctly like <script src='script1.js'></script><script ...></script> iterate to all $js same goes with css
but if i tried echoing out $user_id . . it won't, bec. it is an object.
so to solve for the problem what i did is . .<?php foreach($user_id as $id){} ?> i can echo $id in this case
is there anyway so that i can echo it out simple without that messy foreach.
i understandd that when i comes to $user_info, it is fine to do this foreach($user_info as $key => $value){$info[$key]=$value}
but is there a better way that doing that in the view?
maybe instead of an object, array will be sent before the view can use it?
thanks muchs
Edit: fixed same method name;