0

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

  1. if i tried echoing $message, it will render out correctly like this = something
  2. if i tried looping on each using foreach$js and $css like this

    foreach($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;

4 Answers 4

3

$result->row(); returns an object with all the selected fields. Just because you only SELECTed the id field doesn't change the return value. Try this instead:

$data['user_id'] = $this->user_model->getUserIdByUsername($user)->id;

OR:

getUserIdByUsername($user) {
    $this->db->select("id");
    $this->db->where("username", $user);
    $result = $this->get("users");
    return $result->row()->id;
}

Check the CI docs for more information about generating query results if you want arrays instead of objects:

result_array()

This function returns the query result as a pure array, or an empty array when no result is produced.

row_array()

Identical to the above row() function, except it returns an array.

Sign up to request clarification or add additional context in comments.

2 Comments

@JoeySalacHipolito: I'm not 100% sure what your other problem was after reading it several times, maybe you can clarify it by editing to show your actual code, and focus on what you want to do rather than what's already working fine? It's hard for me to understand your problem, but I added some more information.
thanks for the edit.. yeap.. i think this is solve now.. thank you very much!
2

use {$user_id[0]['id']} , it should echo user id in view . btw in your question both methods name are same, i think 2nd one should be getAllUserInfo($user)

Comments

1

Like you said, it's an object. So you access the fields like one.

echo $user_id->id;

Comments

1

Using the row_array function in your model would return it as an array instead of an object if thats what you're interested in. You can read up on generating query results here: http://ellislab.com/codeigniter/user_guide/database/results.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.