Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove last ',' from end of varaible?

Status
Not open for further replies.

keith23

Technical User
Joined
May 26, 2005
Messages
97
Location
NL
Hi all .I wonder how to remove the last ',' from end of playlistfromdbtotextfile.php?songid=,2,3,4,5,10,11,15,19,18,20,
The script below where it is bold creates this link for me and my php function does not work with ',' at start or end of the passed url . Could any one help me remove the last ',' but keep it it else where since between passeed variable.Thanks

Code:
<script language="javascript">
<!-- hide
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}

function newWindow(url) { 
	var x,y;
	x = screen.width-35;
	y = screen.height-30;
	var win = window.open(url,'glossaryWindow','toolbar=no,directories=no,width=500,height=500'+
	'screenX=0,screenY=0,top=0,left=0,location=no,status=no,scrollbars=no,resize=yes,menubar=no');
}  

// Start of Playme Function
function playme()
{
Id = document.forms.mp3Play.Id;
tempUrl ='';
url = '';

if (Id.length > 0){  
 for (i=0; i<Id.length; ++ i)
 {
	 if (Id[i].checked)
	{
		[B]tempUrl =tempUrl  + Id[i].value +","[/B]
		if (i == 30)
		{
			alert("Each time you can only select 30 songs to play")
			return false;
		}
	}
 }
}
else
	{
		alert("it is less than 0#2")
		tempUrl = tempUrl + "&Id=" + Id.value
	}
	//alert(tempUrl);
	url = "./playlistfromdbtotextfile.php?songid=" + tempUrl;
	newWindow(url);
	return false;
}		
// End of Playme Function	
// -->	
</script>
 
Process it like this.[tt]
tempUrl = tempUrl.replace(/,$/,"");
[/tt]
 
Another method would be to change this:

Code:
url = "./playlistfromdbtotextfile.php?songid=" + tempUrl;

to this:

Code:
url = "./playlistfromdbtotextfile.php?songid=" + tempUrl.substr(0, tempUrl.length - 1);

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
many thanks for u guys i got it fixed!
 
The above suggestions should work but how about not putting in the comma in the first place?
Try this:
Code:
if (Id.length > 0){  
 for (i=0; i<Id.length; ++ i)
 {
     if (Id[i].checked)
    {
        [COLOR=blue]if (tempUrl != '')[/color]
          tempUrl =tempUrl  + Id[i].value +","
        if (i == 30)
        {
            alert("Each time you can only select 30 songs to play")
            return false;
        }
    }
 }
}

This way it only puts the comma in after the first entry and BEFORE any additonal entry so you never get one at the beginning or end of the string.


Stamp out, eliminate and abolish redundancy!
 
Your heart's in the right place:

tempUrl =tempUrl + Id.value +","

should be:

tempUrl =tempUrl + ","+ Id.value

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Oops! 'hit SUBMIT too quickly. This was in response to theniteowl:

---------------------------------------------

Your heart's in the right place:

tempUrl =tempUrl + Id.value +","

should be:

tempUrl =tempUrl + ","+ Id.value

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Whoops, I actually meant to do this.

Code:
if (Id.length > 0){  
 for (i=0; i<Id.length; ++ i)
 {
     if (Id[i].checked)
    {
        [COLOR=blue]if (tempUrl != ''
          tempUrl = tempUrl + ',';)[/color]
        tempUrl =tempUrl  + Id[i].value[COLOR=blue];[/color][COLOR=red][s] +","[/s][/color]
        if (i == 30)
        {
            alert("Each time you can only select 30 songs to play")
            return false;
        }
    }
 }
}

I got distracted and did not add in the second line for the if. This way the comma is conditionally added but the value always is (within the logic block anyway).


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top