Solved

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

Posted on 2009-05-14
10
612 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now