Reputation:
I have a database table that stores URL.What I need is grab those URL's from table and make it click-able with the URL's title as anchor.
This is what I have tried:
while($row4 = mysql_fetch_assoc($result4))
{
echo "<a href =\"$row4[Url1]\">".$row4['Title1']. "</a>";
}
It displays for example my tilte1
that is youtube
and Url1
is www.youtube.com
.
But when I click on it it is going to localhost/mysite/www.youtube.com
How can I fix this?
Upvotes: 3
Views: 1132
Reputation: 353
I enter urls enclosed in quotes, example: "http://google.com" Then I use:
.$row['date']."< a href=".$row['title'].">".$row['title']."< /a>
".
the result is a clickable link in the form of: http://google.com remove the space between < and a, ( i had to add a space for the code to post.
Upvotes: 0
Reputation: 67
You should make an absolute link from that, and don't forget to put attributes' values in quotes. I suggest this:
echo '<a href="http://www.'.$row4[Url1].'">'.$row4['Title1']. '</a>';
//by doing this you also won't need any of \ slashes
Upvotes: 0
Reputation: 287
Try with this
while($row4 = mysql_fetch_assoc($result4))
{
echo "<a href ='http://".$row4['Url1']."'>".$row4['Title1']. "</a>";
}
Upvotes: 0
Reputation: 7580
Can you check if your Url1 field is a proper url? see if it has http:// protocol in the url. if not you will need to add it to prepend it to your table or programmatically prepend http:// protocol to your link.
Additionally you can use below function taken form codeigniter framework. It prepares your link for url. do prep_url($row4[Url1]) instead of just $row4[Url1];
function prep_url($str = '')
{
if ($str == 'http://' OR $str == '')
{
return '';
}
$url = parse_url($str);
if ( ! $url OR ! isset($url['scheme']))
{
$str = 'http://'.$str;
}
return $str;
}
Upvotes: 0
Reputation: 1651
you need http:// in front.
echo '<a href ="http://'.$row4['Url1'].'">'.$row4['Title1']. '</a>';
Upvotes: 0
Reputation: 8578
Add http://
in front of the link. Then it will go to where you wanted.
Upvotes: 1
Reputation: 7475
try:
echo "<a href =\"http://$row4[Url1]\">".$row4['Title1']. "</a>";
Upvotes: 2