Solved

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

Posted on 2009-05-14
10
614 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

774 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