Solved

mysql while into array and output comma separated list

Posted on 2011-03-22
8
858 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
Comment Utility
trim($comma_seperated_string ",");

Open in new window


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

Expert Comment

by:Rik-Legger
Comment Utility
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
Comment Utility
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
 
LVL 142

Expert Comment

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

select group_concat(contentid) from yourtable where ...

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:freshwaterwest
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thanks again - sorted!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 24
Multilanguage Database Design in MySQL 5 33
php variable basic question 12 27
sql sentence 2 8
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now