Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store value from array result to variable in codeigniter

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
like image 973
Kango Avatar asked Apr 14 '26 14:04

Kango


2 Answers

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
like image 173
Dino Babu Avatar answered Apr 16 '26 04:04

Dino Babu


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.

like image 20
Pankaj Singh Avatar answered Apr 16 '26 02:04

Pankaj Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!