Solved

mysql while into array and output comma separated list

Posted on 2011-03-22
8
890 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 50 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
Independent Software Vendors: 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 450 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 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