Welcome Guest, Not a member yet? Register   Sign In
Best way to create an array with query results!
#1

[eluser]daweb[/eluser]
Usually.. excuse me about english :-(

I have a dynamic query with some field results (maybe cod, title) or (maybe cod, title, order) or etc... It depends on the query.

I want to build an array with the result of query

$myarray = [title] = 'MyTitle' [cod] = 'MyCod' ext...

Now, in my model code:

Code:
$query = $this->db->get('table'); // with some where clauses
if($query->num_rows() > 0 ) :
  $query_info = array(
        'list_field' => $query->list_fields(),
          'result'     => $query->result()
            );
  return $query_info;
end;

And in my library code:

Code:
$result = $this->CI->layout_Model->get_my_query();

$daweb_array = array();

# costruzione dinamica dell'array
foreach($result['list_field'] as $key => $field) :
$daweb_array['name_of_array_for_template_library'][$field] = $result['result'][0]->$field;
endforeach;

Code:
$result['result'][0]->$field

Is it the best way to do that?
Please help me CI people!

Thank you guys

PS. It works, I wanna know if it is elegant and the best way to do that
#2

[eluser]daweb[/eluser]
Ehi,

I think this is the better solution, I hope:

Code:
foreach($result['result'] as $res => $row) :
# costruzione dinamica dell'array
  foreach($result['list_field'] as $key => $field) :
    $daweb_array['promo'][$res][$field] = $row->$field;
  endforeach;
endforeach;




Theme © iAndrew 2016 - Forum software by © MyBB