hi second question for the board treat me gently I am working on my football site and am wondering how can I make my urls work better. http://www.mysite.com/index.php?team=Liverpool - ok http://www.mysite.com/index.php?team=Manchester%20United - bad %20 Is there any way to remove the %20 or replace with an _ underscore or hyphen?
You can use http://us.php.net/str_replace to replace characters, otherwise http://us3.php.net/urlencode and http://us3.php.net/urldecode are commands specially made for working with URL data.
if you are printing out a string for a link, you could do something like $url = str_replace("%20","_",$org_url); this would make all the %20's into underscores or you could go a little more advanced and do something like $url = str_replace(array("%20"," "),"_",$org_url); this would replace a real space, and the %20 at the same time, and if there was other characters you wanted to replace as well , just say a ', you could just add it into the array as well.
thanks for the replies, I tried using $team=str_replace(" ","_",$team); it works fine on the variable echoed on the output for the page display but not on the url %20 is still there. <?php mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT * FROM football WHERE team = '".$_GET['team']."'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $team=mysql_result($result,$i,"team"); $team=str_replace(" ","_",$team); $players=mysql_result($result,$i,"players"); $i++; } ?> Any ideas how I can get this working on the url ?
Woah. Red card for putting $_GET data directly into a query. Always filter and clean user input before putting it into your database, displaying it on their browser, or anything else. Do this instead: $get_team=mysql_real_escape_string(str_replace(" ","_",$_GET['team']);); $query = "SELECT * FROM football WHERE team = '".$get_team."'"; PHP: Also, you can simplify your loop by removing the count variable entirely and using mysql_fetch _assoc(), which is faster than mysql_result() anyway. <?php mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $get_team=mysql_real_escape_string(str_replace(" ","_",$_GET['team']);); $query = "SELECT * FROM football WHERE team = '".$get_team."'"; $result=mysql_query($query); while ($row=@mysql_fetch_assoc($result)) { $team=str_replace(" ","_",$row['team']); $players=$row['players']; } ?> PHP: I doubt the last str_replace() is necessary, but I have no idea what the rest of the code looks like.
the url depends on how you make your links if you use the str_replace function when you create the link you can make it point to Manchester_United, then you use str_replace again to replace _ with ' ' when you access the URL parameters there's no way you can change the page URL once it's loaded, you can only change what's clickable by your visitors in order to prevent %20 in the URL