Hi guys, i am writing this piece of code, it is building some javascript. I have got it working exactly the way I would like, apart from in the while loop, I would like to reomve the comma on the last loop. This needs to be removed for the javascript function to work

<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("testbase") or die(mysql_error());
$script = "";
$query=mysql_query("
	SELECT town
	FROM country
	WHERE country='England'");
$script .= 'stateNames["England"]=new Array(';
while($row=mysql_fetch_array($query))
	{
	$script .= '"' . $row['town'] . '",'; //the part that needs altering
	}
$script .= ")
";
echo $script;
?>

Is there maybe a php function to delete the last character of a variable or some way that i can get rid of that one comma.

Thanks for looking

Yes, use the substr function:

$script = substr($script, 0, -2);

If this trims too much, try changing the -2 to -1.

Thanks dude

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.