CodeIgniter Forums
Extracting all the id's from a query and saving them as a comma seperated list - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Extracting all the id's from a query and saving them as a comma seperated list (/showthread.php?tid=7940)



Extracting all the id's from a query and saving them as a comma seperated list - El Forum - 04-28-2008

[eluser]jtotheb[/eluser]
I'm trying to run a query using AR and then save back to another table all the ID's from that query as a comma seperated list.

I'm trying to use array_pop($array) and then join (', ', $array) . ", and $last";

But i think the problem i'm having is getting the result correctly out of the query result.

If anyone could give me a wee hand with this, that would be fantastic!


Extracting all the id's from a query and saving them as a comma seperated list - El Forum - 04-28-2008

[eluser]TheFuzzy0ne[/eluser]
Stupid question, I know, but what's AR?


Extracting all the id's from a query and saving them as a comma seperated list - El Forum - 04-28-2008

[eluser]jtotheb[/eluser]
AR = Active record

Not stupid at all, i should have been clear with my original post.


Extracting all the id's from a query and saving them as a comma seperated list - El Forum - 04-28-2008

[eluser]TheFuzzy0ne[/eluser]
Of course. I should have known that. I've read about it a lot in the past, but not actually used it.

Can you post some code so we can see the query you're making, the results you're getting back, and how you're processing it?


Extracting all the id's from a query and saving them as a comma seperated list - El Forum - 04-29-2008

[eluser]jtotheb[/eluser]
The query is simply something like this.

$this->db->select('id');

$this->db->from('table_name');

$this->db->get();

And i want to get all the id's into a comma seperated list.

Cheers


Extracting all the id's from a query and saving them as a comma seperated list - El Forum - 04-29-2008

[eluser]xwero[/eluser]
Code:
$array = array();
foreach($query->result() as $row)
{
   $array[] = $row->id;
}
$commastr = implode(',',$array);
php5 code for php4 you have to use $query->result_array() and $row['id']


Extracting all the id's from a query and saving them as a comma seperated list - El Forum - 04-29-2008

[eluser]jtotheb[/eluser]
Php5 is where i'm at so that is great.

I really struggle with the myriad ways of getting data out from AR queries, i find myself getting quite quite lost a lot of the time. I think i need to write myself some form of cheat sheet.

Thanks a lot xwero, i really appreciate your help.