?
Solved

mysql while into array and output comma separated list

Posted on 2011-03-22
8
Medium Priority
?
900 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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