Reputation: 847
I have following controller,
class mycontroller extends CI_Controller
{
public function getvalue() {
$q= $this->db->select(array('value'))
->from('settting')
->get();
$data['data']= $q->result();
$this->load->view('myview',$data);
}
}
In my view:
print_r($data);
Gives output like this
Array ( [0] => stdClass Object ( [value] => wwwwwwwwwwww ) [1] => stdClass Object ( [value] => 5454546 ) [2] => stdClass Object ( [value] => 6868 ) [3] => stdClass Object ( [value] => 9898 ) [4] => stdClass Object ( [value] => 7878 ) [5] => stdClass Object ( [value] => 212 ) [6] => stdClass Object ( [value] => 87878 ) [7] => stdClass Object ( [value] => 8989 ) [8] => stdClass Object ( [value] => 212 ) )
I am trying to store each single value in variable
when i use ,
foreach($data as $row)
{
echo $row->value;
}
its give me all value but i need to save each value in variable.
how can i do this, like this,
$var1='wwwwwwwwwwww'; // [0 ] array value
$var2='5454546'; // [1] array value
Upvotes: 2
Views: 36277
Reputation: 531
When you print a query variable,
print_r($query);
you would get following result
Array ( [0] => stdClass Object (
[Id] => 14
[username] => john
[password] => e10adc3949ba59abbe56e057f20f883e
[email] => [email protected]
[key] => 7f55570435fd15393f27eaac659b078a
[is_active] => 1
[gender] => Man
[countryid] => 1
[stateid] => 4
[cityid] => 4
[age] => 31
) )
So if you want to get the value of a single element from this array, you can write the following code -
echo $query[0]->username;
then you get the following value
john
I hope this solves your problems. I'm sure that you might have already found the answer. but i got the same problem, so i thought it might help somebody.
Upvotes: 4
Reputation: 46
Look array is containing "stdClass Object" object. So you need to convert object into array. So convert using,
$data= $q->result();
$data['data']=$data->result_array(); or $data->row_array();
Upvotes: 0
Reputation: 19882
You strategy is totally wrong. You are calling a db call from a controller which breaks MVC rule. Create a model for this.
Class Value Extends CI_Model{
function __construct(){
parent::__construct();
}
function getValue(){
return $this->db
->select(array('value'))
->from('settting')
->get();
->result();
}
And now call it from controller
class mycontroller extends CI_Controller
{
$this->load->model('value');
public function getvalue() {
$data['value']= $this->value->getValue();
$this->load->view('myview',$data);
}
}
And now in the view
foreach($value as $row)
{
echo $row;
}
Upvotes: 1
Reputation: 632
You loop through the result with some tempoarary variable to construct variable. But i don't know whats the use of storing the array values in seperate variable :-)
$i = 0;
foreach($data as $row)
{
$var_.$i = $row->value;
$i++;
}
Upvotes: 0
Reputation: 5809
better save it to another array
$my_values = array();
foreach($data as $row)
{
$my_values[] = $row->value;
}
now you have :
echo $my_values[0]; // wwwwwwwwwwww
echo $my_values[1]; // 5454546
Upvotes: 4
Reputation: 1805
You can try like this
class mycontroller extends CI_Controller
{
public function getvalue() {
$q = $this->db->select(array('value'))
->from('settting')
->get()
->result_array();
$i = 0;
foreach($q as $val)
{
${'var' . ++$i} = $val['value']; // Here we put each value in new variable, with variable name var1, var2, ...
}
echo $var1;
echo $var2;
$this->load->view('myview',$data);
}
}
Upvotes: 1