?
Solved

mysql while into array and output comma separated list

Posted on 2011-03-22
8
Medium Priority
?
904 Views
Last Modified: 2012-05-11
I'm trying to do a mysql while and output the resulting list of IDs with commas

i.e. 123,232,432,453 (without a trailing comma)
I can easily add a comma but this leaves a trailing comma at the end and I think there must be a really easy way to use array() implode/explode but haven't managed to suss out the right method or combination.

    while( $row = $modx->db->getRow( $res ) ) {
	$fullidlist .= $row['contentid'];
    }
//need to get the $fullidlist to output as a comma separated list

Open in new window

0
Comment
Question by:freshwaterwest
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35189076
trim($comma_seperated_string ",");

Open in new window


This will remove a leading and trailing comma.
0
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35189086
trim($comma_seperated_string, ",");

Open in new window


Updated, made a typo.
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 200 total points
ID: 35189100
Using arrays, try this (UNTESTED)

    $arr = array();

    while( $row = $modx->db->getRow( $res ) ) {
	$arr [] = $row['contentid'];
    }

    $fullidlist = implode(",", $arr);

//need to get the $fullidlist to output as a comma separated list

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35189136
you could of course also solve this with mysql directly:

select group_concat(contentid) from yourtable where ...

0
 

Author Comment

by:freshwaterwest
ID: 35189265
many thanks all - angellll if I use the mysql group_concat in select how would I get this into a comma sep list?  
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1800 total points
ID: 35189284
you just fetch the value, you get only 1 row.

$sql = "select group_concat(contentid) from yourtable where ... ";
$res = mysql_query($sql) or die (mysql_error());
$list = mysql_result($res, 0,0 );
print $list;

Open in new window

0
 

Author Comment

by:freshwaterwest
ID: 35190205
brilliant thanks angellll

I used the group_concat and then:

    $row = $modx->db->getRow( $res, 0,0 );
	$fullistarray = implode(',',$row);

Open in new window


works perfectly
0
 

Author Closing Comment

by:freshwaterwest
ID: 35190238
thanks again - sorted!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question