• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
returning an array from a db result set

#1
[eluser]stef25[/eluser]
having some basic php array problems. below is a function from my model that gets some results from a db. id like it to return an array called $data. this works, but the print_r only prints out 1 row of the 2 rows in that table. when it iterates through the foreach i think it just overwrites the previous values.

how do i create the $data array that holds all the row's data instead of just one row?

Code:
function getMainFeature(){
          $data = array();
          $this-> db->select("id, prod_name, prod_descr_short");
          $query = $this->db->get('products');
          foreach ($query->result_array() as $row) {
            $data['id'] = $row['id'];
            $data['prod_name'] = $row['prod_name'];
            $data['prod_descr_short'] = $row['prod_descr_short'];
          }
          
          print_r($data); exit;
          $query->free_result();
          return $data;
        }

#2
[eluser]Mike Ryan[/eluser]
You are overwriting the $data array each time you go through the loop, so it will only contain the last entry.
This would work:
Code:
foreach ($query->result_array() as $row)
{
  $data[] = array(id => $row['id'],
                  prod_name => $row['prod_name'],
                  prod_descr_short = $row['prod_descr_short']
                 );
}

I'm not sure why you are looping through the array to copy it. Why not just return that value directly?

Code:
$sql = 'SELECT id, prod_name, prod_descr_short FROM products';
$query = $this->db->query($sql);
//error checking goes here!
return $query->result_array();

#3
[eluser]stef25[/eluser]
hey, thanks for your reply. your first snippet outputs some errors:

Message: Use of undefined constant id - assumed 'id'

any idea how to fix this? getting closer, thanks!

EDIT:

think it should actually be

Code:
foreach ($query->result_array() as $row) {
            $data[] = array(
              'id' => $row['id'],
              'prod_name' => $row['prod_name'],
              'prod_descr_short' => $row['prod_descr_short']
             );
          }

#4
[eluser]TheFuzzy0ne[/eluser]
What's wrong with doing this(?):
Code:
function getMainFeature()
{
    $this-> db->select("id, prod_name, prod_descr_short");
    $result = $this->db->get('products');
    $arr = $result->result_array();
    $result->free_result();
    return $arr;
}

I must be missing something...

#5
[eluser]stef25[/eluser]
the only thing you missed was that you're better than us. your solution works fine and ill be using this :-)

thanks TheFuzzy0ne

#6
[eluser]TheFuzzy0ne[/eluser]
I wouldn't say I'm better than anyone, but rather I've been here before and made the same mistakes. Smile

#7
[eluser]Mike Ryan[/eluser]
[quote author="stef25" date="1239468311"]
Message: Use of undefined constant id - assumed 'id'
[/quote]

Sorry, I was in perl mode :-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.