How to pass a text from the database to display it as a link on a website

Asked

Viewed 45 times

0

Hello, I’m making a site in php and html, linked to a database in mysql.

And I have a table of news in the database, with the idartigos fields, title, text and link.

The link is running in text and I would like when someone sees the news on the site, becomes a link, so that the person clicked. Can someone help me?

 $query = mysqli_query($mysqli, "SELECT * FROM artigos WHERE activo = 1 ORDER BY idartigos DESC");
                                    while($row_artigos = mysqli_fetch_array($query))
                                    {                       
                                echo "<br><br>";
                                echo($row_artigos[1]);
                                echo "<br>";
                                echo($row_artigos[2]);
            # tirar # qdo tiver link        echo($row_artigos[3]);          

                                echo "<br><br>";

4 answers

1


You must use the tag <a> and assign the value of $row_artigos[3] to the attribute href.

Example:

echo '<a href="'.$link.'">'.$row_artigos[3].'</a>';

Edited:

$link = $row_artigos[3];
if (strpos($link,'http') === false)
  $link = 'http://'.$link;

echo '<a href="'.$link.'">'.$row_artigos[3].'</a>';
  • yes @carlosandrade however this code does not make people be redirected to the location that the link points to, imagining that I put www.google.pt I wanted the person clicking on the link to be redirected to that site.

  • Don’t? This example should print on your page something like: <a href="http://www.google.pt">http://www.google.pt</a>. Remembering that for the link to work, you should also write the protocol in the url, which would be exactly like this: http://www.google.pt and not just www.google.pt.

  • If you do not save the protocol in the field of your table (http:// or https://) you can add it when generating the link: echo '<a href="http://'.$row_artigos[3].'">'.$row_artigos[3].'</a>';

  • +1 is just that!!

  • I will make an edit in the reply to make a check for if the protocol is not inserted in the field, the same be inserted.

0

I believe you are seeking to implement a common link on your page...

Here is an example of implementation: Example of link implementation

Whatever you want to display as a link will always be inserted inside the tag '<a>', perform a usage search of this tag that can be applied more effectively.

0

You want the title to become a link? If that’s what you can do:

echo "<a href='".$link."'>".$titulo."</a>";

I couldn’t understand the question, it might be clearer?

  • I want the link field (which is text) to turn a link to be shown on the site.

0

For the displayed text to be the contents of column 1, and the link, the contents of column 3:

   echo("<a href='".$row_artigos[3]."'>".$row_artigos[1]."</a>");

For text and link to be in the same column:

echo("<a href='".$row_artigos[3]."'>".$row_artigos[3]."</a>");
  • The code is almost perfect, but imagining that the link was google.pt I wanted that when clicking could be redirected to google.pt and this is not working

  • google.pt must be within $row_artigos[3] and the text you want to display as a link within $row_artigos[1]. What problem is going on ?

  • cannot be '$row_articles[1]' has to be '$row_articles[3] because it is in field 3, which is called link

  • one is the text that will display, the other the link url, no ?!

  • the end result should be something like: <a href='http://google.pt'>Meu texto do link</a>

  • the fields in my database are: idartigos, title, text, and link. The link field is row_artigos[3] and when I add a news the link is in normal text, I wanted that when passing to the site, customers could see this text "link" as a link and that could be clicked and redirected to such link

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.