Jump to content

A hyper link problem


ali_2kool2002

Recommended Posts

Hi guys... bit confused on this line for a hyperlink.. iv managed to get the form to go to the rite page name except the last part ".php" wont come at the end of the address line, and also as the $cat_name is an attribute coming from the database, only the first word is being outputted ie...

http://localhost/finalyear/Baby

when it should be

http://localhost/finalyear/Baby Food Productss

 

i use the echo value in the code to check if the right value is being retrieved from the databse and it outputs

Baby Food Productss

 

this is fine but when applied int the line just the first word baby comes up as mentioned above

 

  "<li class=\"style9\"><a href=http://localhost/finalyear/". $cat_name .".php\">". $cat_name ."</a></li>\n".
     "</ul>\n";

 

 

 

ORIGINAL CODE

 

while($cats = mysql_fetch_array($get_cats_res)) {
//$cat_name = strtolower($cats['cat_name']);
$cat_name = $cats['cat_name'];
$cat_desc = stripslashes($cats['cat_desc']);
echo $cat_name;
echo "<ul>\n".
     "<li class=\"style9\"><a href=http://localhost/finalyear/". $cat_name .".php\">". $cat_name ."</a></li>\n".
     "</ul>\n";

Link to comment
https://forums.phpfreaks.com/topic/41502-a-hyper-link-problem/
Share on other sites

the reason the link is outputting

 

http://localhost/finalyear/Baby

 

instead of

 

http://localhost/finalyear/Baby Food Products.php

 

is because the spaces in the file name.  when there are spaces in links the %20 is used to represent spaces.  so the link should actually be displayed like this.

 

http://localhost/finalyear/Baby%20Food%20Products.php

 

you'll have to either get rid of the spaces or set up a loop that replaces the spaces with %20 or something like that.  i'm not sure how to handle that.  i never use spaces in file names.

Link to comment
https://forums.phpfreaks.com/topic/41502-a-hyper-link-problem/#findComment-201117
Share on other sites

personaly when I need to use spaces I replace them with _

 

$url="blah...Baby Names"

 

$url= str_replace("_"," ",$url)

 

 

 

that works well, too, but i think it's much more secure and accurate to use rawurlencode(). because you can then use rawurldecode()

 

however, if you were to use str/preg_replace(), the url or file MAY already have '_' in it. therefore, if you need to retrieve this data in the future, it will not be accurate if you preg_replace to remove the '_' in order to get the data back to it's original state.

Link to comment
https://forums.phpfreaks.com/topic/41502-a-hyper-link-problem/#findComment-201268
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.