?
Solved

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

Posted on 2009-05-14
10
Medium Priority
?
625 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 …
Suggested Courses

762 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