Solved

mysql while into array and output comma separated list

Posted on 2011-03-22
8
877 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 142

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 142

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 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 …

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