Welcome Guest, Not a member yet? Register   Sign In
foreach array from mysql result
#1

[eluser]Unknown[/eluser]
Hey everyone!

I have an array records in my database, like this:

Code:
a:3:{i:0;s:6:"111111";i:1;s:6:"123456";i:2;s:6:"122345";}

how can I foreach this an view?

my controller:

Code:
$data['avcolors'] =  $this->Category_model->get_avcolors();
$this->load->vars($data);

my model:

Code:
function get_avcolors()
{
            
            $query = $this->db->query('SELECT avcolors FROM ds_categories');  
            return $query ->result_array();
      
}

and my view:

Code:
foreach($avcolors as $avcolor):
            
     echo $avcolor['color'];
          
endforeach;

result var_dump:

Code:
string(57) "a:3:{i:0;s:6:"111111";i:1;s:6:"123456";i:2;s:6:"122345";}"

thank you for your help!

Daniel
#2

[eluser]CroNiX[/eluser]
It looks like it's just a serialized array stored in the db. Just unserialize it in your loop.
Code:
foreach($avcolors as $avcolor)
{
    //$data is now the original array, however it doesn't look like an associative array so there is no 'color' index in it.
    $data = unserialize($avcolor);

    //based on your example data, it would look like:
    array(
      [0] => 111111,
      [1] => 123456,
      [2] => 122345
    )
}




Theme © iAndrew 2016 - Forum software by © MyBB