Extract links from mysql and make it clickable?

I have a database table that stores a URL. What I need is to grab these URLs from the table and make it clickable with the URL header as an anchor.

Here is what I tried:

while($row4 = mysql_fetch_assoc($result4)) { echo "<a href =\"$row4[Url1]\">".$row4['Title1']. "</a>"; } 

For example, my tilte1 , which youtube and Url1 is www.youtube.com .

But when I click on it, it will be localhost/mysite/www.youtube.com

How can i fix this?

+4
source share
7 answers

Add http:// before the link. Then he will go where you want.

+1
source

to try:

 echo "<a href =\"http://$row4[Url1]\">".$row4['Title1']. "</a>"; 
+3
source

you need http: // in front.

 echo '<a href ="http://'.$row4['Url1'].'">'.$row4['Title1']. '</a>'; 
0
source

Can you check if your Url1 field is a valid url? see if it has http: // protocol in the url. if not, you will need to add it to add it to your table or programmatically add the http: // protocol to your link.

In addition, you can use the following function taken from the codeigniter framework. It prepares your link for the URL. do prep_url ($ row4 [Url1]) instead of $ 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; } 
0
source

Try with this

 while($row4 = mysql_fetch_assoc($result4)) { echo "<a href ='http://".$row4['Url1']."'>".$row4['Title1']. "</a>"; } 
0
source

You must make an absolute reference, and remember to put the attribute values ​​in quotation marks. I suggest the following:

 echo '<a href="http://www.'.$row4[Url1].'">'.$row4['Title1']. '</a>'; //by doing this you also won't need any of \ slashes 
0
source

I entered the URLs enclosed in quotes, for example: " http://google.com " Then I use:

<line> $ row ['date']. "<a href =". $ row ['title']. ">". $ row ['title']. "</a>
"

the result is an interactive link in the form: http://google.com remove the space between <and a, (I had to add a place to send the code.

0
source

Source: https://habr.com/ru/post/1499569/


All Articles