PHP Scripts

Remove blank spaces in a MySQL result, so can be pasted direct as a hyperlink

The idea is to display a list of cities in a region, and click to retrieve more information.
The problem is that the result contains empty spaces for cities that are 2 or 3 words long, like Stratford upon Avon.

This was displaying properly on the page, but the <a href> link only had the first word.

It was a very simple solution.

<?PHP
$db = mysql_connect("host", "login", "password");
mysql_select_db("table",$db);

// query results
$result = mysql_query("SELECT DISTINCT city FROM table ORDER BY city ASC,$db);
$num_rows = mysql_num_rows($result);

//isolate each value
and replace blank spaces with +
if ($myrow = mysql_fetch_array($result)) {
$zz=0;
while ($zz < $num_rows) {
$link=mysql_result($result,$zz,"city");
$myarray = array("$zz"=>"$link");
foreach($myarray as $key=>$value) {
$d2 = urlencode($value);

//paste as link
$go="<a href=map.php?p=$d2>$value</a>";
printf("<tr bgcolor=#acdad8><td>$go</td></tr>");
$zz++;
}
}
} else {
//echo "No record";
}

?>

 

Back

Source