SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    I wrote a script that generate a list of words separate by a comma, and the number of words is different each time. Sometimes it there's 1 word, and sometimes there's more.

    When there's only one word, it'll display "word,"
    and when there's more than one words, it'll display "word1,word2,word3,".

    My question is how can i trim the trailing comma so that if there's no more words left, the comma wont show up?

    Thank you

    Sang N.

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use an if statement before you print out.
    Pseudocode:
    Code:
    if <at the end of array>
    print out the element only
    else
    print out the element and the comma
    Is that clear?
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Son,

    That didnt help much since i'm still pretty new ;o(

    here's the code i used:

    Code:
    	$brand[0] = $chk_canon;
    	$brand[1] = $chk_ricoh;
    	$brand[2] = $chk_minolta;
    	$brand[3] = $chk_sharp;
    	$brand[4] = $chk_xerox;
    	$brand[5] = $chk_toshiba;
    	$brand[6] = $chk_mita;
    	$brand[7] = $chk_lanier;
    	$brand[8] = $chk_riso;
    	$brand[9] = $chk_gestetner;
    	for($i=0; $i<10; $i++){
    	 if($brand[$i]){
    		  $whichbrand="$brand[$i], $whichbrand";
    		}
    	}
    notice the comma after $brand[$i]

    Sang N.

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem, here people are very helpful!

    Code:
    	$brand[0] = $chk_canon;
    	$brand[1] = $chk_ricoh;
    	$brand[2] = $chk_minolta;
    	$brand[3] = $chk_sharp;
    	$brand[4] = $chk_xerox;
    	$brand[5] = $chk_toshiba;
    	$brand[6] = $chk_mita;
    	$brand[7] = $chk_lanier;
    	$brand[8] = $chk_riso;
    	$brand[9] = $chk_gestetner;
    	for($i=0; $i<10; $i++){
    	 if($brand[$i])
             {
               if ($i != 9) // NOT at the end of array
               { $whichbrand .="$brand[$i],"; }
               else 
               { $whichbrand .="$brand[$i]";}
             }
    	}
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •