Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remove the last comma and get the result out of while loop

Posted on 2009-05-14
10
Medium Priority
?
626 Views
Last Modified: 2013-12-12
I have the code below, which I am using for multiple selections. What I want to do is remove the last comma from the result and then have the results available through out the script. The variable in question is $val1 and $val2
Thanks
<form method=post action=''>
<select name='color[]' size=4 multiple>
<option value='blue'>Blue</option>
<option value='green'>Green</option>
<option value='red'>Red</option>
<option value='yellow'>Yelllow</option>
<option value='' selected>Select a Color </option>
<option value='white'>White</option>
</select> 
<input type=submit></form>
 
///// collecting form data ///// 
 
@$color= $_POST['color'];
if( is_array($color)){
while (list ($key, $val) = each ($color)) {
$val1 = $val.",";
echo "$val1";
//then I want to have another $val2 that will not contain the last comma from $val1
}
}
//also I want to see $val1 available here too

Open in new window

0
Comment
Question by:syedasimmeesaq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 7

Expert Comment

by:Jonah11
ID: 24389620
why dont you use implode instead?

http://us3.php.net/manual/en/function.implode.php
0
 
LVL 2

Expert Comment

by:crash36936
ID: 24389657
I think these 2 functions may be useful to you.  The first one replaces your while loop.  The second one will do what you want if you pass the comma as a parameter (i.e. `$val = rtrim($val, ',');`)

http://us3.php.net/manual/en/function.implode.php

http://us3.php.net/rtrim
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24389659
Try

$val2 = substr( $val1, 0, -1);

which will chop off the last character. To chop off the last (say) six characters you  would use

$val2 = substr( $val1, 0, -6);

See http://www.php.net/substr
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:dhobale
ID: 24389717
Use the following code:

@$color= $_POST['color'];
$val1 = "";
$val2="";
if( is_array($color)){
while (list ($key, $val) = each ($color)) {
$val1 = $val.",";
echo "$val1";
//then I want to have another $val2 that will not contain the last comma from $val1
$val2 = substr($val1, 0, (strlen($val1)-1));
}
}
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24389773
I tried those suggestions.. Problem is that it chops off all the commas, not just last one. I think it is because I am still in while loop. Also how do I get the values out of while loop and show it on the other parts of script. Cause out of while loop it only shows the last value.
Thanks guys
0
 
LVL 5

Expert Comment

by:dhobale
ID: 24389787
Use the following code:

@$color= $_POST['color'];
$val1 = "";
$val2="";
if( is_array($color)){
while (list ($key, $val) = each ($color)) {
$val1 = $val.",";
echo "$val1";
//then I want to have another $val2 that will not contain the last comma from $val1
$val2 = substr($val1, 0, (strlen($val1)-1));
}
echo $val1;
echo $val2;
}
0
 
LVL 5

Expert Comment

by:dhobale
ID: 24389794
following formula will remove last comma..and everything else will remain from val1

$val2 = substr($val1, 0, (strlen($val1)-1));
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24389845
dhobale u already gave this code and I tried it. Please try it yourself and you will see it doesnt work.
Thanks
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 total points
ID: 24389851
You do not seem to accumulating data whilst in the loop. Try this


@$color= $_POST['color'];
$val1 = "";
if( is_array($color)){
   while (list ($key, $val) = each ($color)) {
      $val1 .= $val.",";
   }
}

$val2 = substr( $val1, 0, -1);

echo "Val 1 is $val1<br>";
echo "Val 2 is $val2<br>";
0
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
ID: 31581691
Perfect! Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

596 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