Bluehost.com Web Hosting $6.95

Array to List

This is a discussion on Array to List within the PHP General forums, part of the PHP Programming Forums category; Hello, Coming from ColdFusion, this is difficult. CF has an ArrayToList() function. I can't find anything similar in PHP. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-11-2003
James Johnson
 
Posts: n/a
Default Array to List

Hello,

Coming from ColdFusion, this is difficult. CF has an ArrayToList() function.
I can't find anything similar in PHP.

I'm building a list from an array with the following code, but it puts a
trailing "," and I need to remove it.

$campusList = "";
foreach ($_POST['campus'] as $campus_ID){
$campusList .= $campus_ID;
$campusList .= ",";
}
print($campusList);

I've tried this:
if($campusList <> ""){
$chr = ",";
$campusList = strrchr($campusList,$chr);
}

But it removes everything from the list, except 1 ","

Thanks,
James

Reply With Quote
  #2 (permalink)  
Old 08-11-2003
Chris Boget
 
Posts: n/a
Default Re: [PHP] Array to List

> Coming from ColdFusion, this is difficult. CF has an ArrayToList() function.
> I can't find anything similar in PHP.


implode();

> I'm building a list from an array with the following code, but it puts a
> trailing "," and I need to remove it.


[snip]

$campusList = implode( ', ', $_POST['campus'] );

Simple as that.
Read more about arrays here:

http://www.php.net/manual/en/ref.array.php

Chris

Reply With Quote
  #3 (permalink)  
Old 08-11-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Array to List

* Thus wrote James Johnson (james@smb-studios.com):
> Hello,
>
> Coming from ColdFusion, this is difficult. CF has an ArrayToList() function.
> I can't find anything similar in PHP.
>
> I'm building a list from an array with the following code, but it puts a
> trailing "," and I need to remove it.
>
> $campusList = "";
> foreach ($_POST['campus'] as $campus_ID){
> $campusList .= $campus_ID;
> $campusList .= ",";
> }


Just need to truncate the last character:
$campusList = substr($campusList,0, -1);

> print($campusList);



Curt
--
"I used to think I was indecisive, but now I'm not so sure."
Reply With Quote
  #4 (permalink)  
Old 08-11-2003
James Johnson
 
Posts: n/a
Default RE: [PHP] Array to List

Actually, I'm using $campusList for a SQL statement:

SELECT name FROM campuses WHERE inst_id IN ('$campusList');

It wasn't working until I found out that $campusList needs to look like
'1','2','3'.

$campusList = implode( ', ', $_POST['campus'] );

Returns 4,2,3 (whatever was selected)

I've looked in the manual on implode, but don't see how to surround each
value with single quotes. Is there another function that will do this?

Thanks,
James

-----Original Message-----
From: Chris Boget [mailto:chris@wild.net]
Sent: Monday, August 11, 2003 11:50 AM
To: James Johnson; php-general@lists.php.net
Subject: Re: [php] Array to List


> Coming from ColdFusion, this is difficult. CF has an ArrayToList()
> function. I can't find anything similar in PHP.


implode();

> I'm building a list from an array with the following code, but it puts
> a trailing "," and I need to remove it.


[snip]

$campusList = implode( ', ', $_POST['campus'] );

Simple as that.
Read more about arrays here:

http://www.php.net/manual/en/ref.array.php

Chris


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:59 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0