views:

51

answers:

1

Hi

I have a query which returns a series of cells of data from a $this->db->query($sql) in the controller. What I want to do is foreach row of data, concatenate two of the cells and create a new cell then add it back to the array? For example, if i had an array:

A 1
B 2
C 3

Then we can after processing the array

A 1 A1
B 2 B2
C 3 C3

I was going to do it all in sql but I need a php function to do a urlencode. Initially I tried

for($i=0; $i < $listProducts->num_rows(); $i++){
    $listProducts[$i][‘prodUrl’] = '/index.php/product/'.
        urlencode($listProducts[$i]['ProductName']).
        '/'.$listProducts[$i]['PK_Product'];
}

But then realised that the return from $this->db->query($sql) isn't an associative array.

help?

+1  A: 

If you want the result be an array, do this:

$query = $this->db->query($sql);
foreach ($query->result_array() as $row)
{ 
    $row[‘prodUrl’] = ‘/index.php/product/’. urlencode($row[‘ProductName’]). ‘/’. $row[‘PK_Product’]; 
}

Ref: http://codeigniter.com/user%5Fguide/database/results.html

David Elizondo
but how do i then put back the newly cell created back into the originating array? Thats my question. I am creating this in the controller and want to just pass the array with the additional column of data to the view.
Joe
you can assign $query->result_array() to a new variable. Then loop it like your original code.
David Elizondo