» Quick Links
You can make money on these forums
We Share profits with you
Google
Google Adsense
Google Adwords
YPN
Yahoo
MSN Search
Web Directories
Web Hosting
Web Hosting Offers
Hosting News
Suggestions
Link Building
Domain Names
PHP Forums
MySQL Forums
» More Links
OSP News
Reseller Hosting
Shared Hosting
Dedicated Servers
Google Adsense
Search Engine Marketing
Link Development
Affiliate Marketing


» Advertising
Multiple DC PR Check

Free SEO Tools


Go Back   Webmaster Forums > Website Developement / Programming > PHP Programming and Tips

PHP Programming and Tips Discuss about PHP programming and Share Tips. Ask questions about Scripting and Errors.

Reply
 
Thread Tools Display Modes
  #1  
Old 08-19-2006, 10:40 AM
instinctive instinctive is offline
Junior Member
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
instinctive is on a distinguished road
Question Variable, variables and variable

How can I echo an array of variables with commas + 'ands' where appropriate? For example, say there is an array of artist names such as:

bob, mary, john

how can I have php make use of commas and 'and' when displaying these? 'artists' array may only have one artist in which case I'd want it on it's own:

bob

if there are two variables then 'and' should be used:

bob and mary

but if there are three (or more) artists, commas and 'and' are needed:

bob, mary and john

Is there any way of doing this without having a switch of ifelse statement for each possible number of artists?
Thanks and bye -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Revenue Sharing Ads ( ?):
  #2  
Old 08-19-2006, 10:41 AM
azhar azhar is offline
OSP Starters
 
Join Date: Mar 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
azhar is on a distinguished road
Default

I think the simpliest way would be to check for the size, use ifs/case to check if its one, two or three or more. then for 3 or more. loop through the array and print it with commas until you get to the item in the array with an index that is two less then the size. then use an and.

something like:


PHP Code:
if(sizeof($artists) >=3){
for($i ==0; $i < sizeof($artists); $i++){
if($i < sizeof($artists) - 2){
echo $artists[$i] . ", ";
}
else if($i == sizeof($artists) - 2){
echo $artists[$i] . " and ";
}
else{
echo $artists[$i];
}
}

i didn't test this so there is probably some missing semi colans and such but it should give you the general idea. -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
  #3  
Old 08-19-2006, 10:43 AM
stephanie stephanie is offline
OSP Starters
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
stephanie is on a distinguished road
Default

This'll save you a loop:


PHP Code:
//initial array
$array1 = array('bob');
$array2 = array('bob','martha');
$array3 = array('bob','martha','jim');

$count = count($array);

//implode will ignore the glue in single-element arrays,
//so we can run arrays 1 and 2 through the basic 'and' routine
if( $count < 3 )
{
$string = implode(' and ',$array);
}
else
{
//break array into comma-delimited string
$string = implode(', ',$array);

//get position of comma before the last array element
$spot = strrpos($string, ', ' . $array[$count - 1]);

//replace the ', ' before the last element with ' and '
$string = substr_replace($string, ' and ', $spot, 2);

}

//$array1 = 'bob'
//$array2 = 'bob and martha'
-- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
forcing uppercase variable dizzie PHP Programming and Tips 1 08-22-2006 01:49 PM
clear session variable vandort ASP forums and Tips 1 08-11-2006 02:48 PM


All times are GMT. The time now is 08:44 AM.