Solved

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

Posted on 2009-05-14
10
613 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now