Jump to content
PShannon

MySQL hyperlink data in HTML table

Recommended Posts

I am displaying data from a MySQL table in an HTML table.  I have one field that I would like to display as an hyperlink (hot link). I have been all over the web for three days looking for a solution to do this.

I have placed all of the problem elements on a webpage:

1. MySQL table specs

2. PHP display MySQL table data in HTML table

3. HTML table as seen in website

http://www.database39.com/testtableurl.php

Thank you for your help . . .

Share this post


Link to post
Share on other sites

Hey there, welcome to the forums!

Your code looks pretty solid, you just need to add the anchor (<a>) surrounding each database output. Just as you've done with the <td> elements.

 

Think of it like a normal HTML page, but rather than typing in the numbers, the PHP is doing that for you. Just because you're using PHP, doesn't stop you from needing to still format the HTML properly.

Share this post


Link to post
Share on other sites

Thank you for responding. I appreciate it.

Would you kindly show me where the anchor (<a>) is placed, Funce? Placement is everything.

    while($row = $result->fetch_assoc()) {
        $the_url = $row["the_url"];
    echo "<tr width:100%>
         <td text-align: left; width: 50%;>" . $row["some_text"] . "</td>
         <td text-align: center; width: 50%;>" . $row["the_url"] . "</td>
         </tr>";
    }
    echo "</table>";
    } else { echo "0 results"; }
        echo "The URL--> " . $the_url;

You guys have some pretty good rag sailors down there . . .

Share this post


Link to post
Share on other sites

Think about it like this. Your output is just straight HTML.

<tr width:100%="">
         <td text-align:="" left;="" width:="" 50%;="">Good Morning!</td>
         <td text-align:="" center;="" width:="" 50%;="">https://www.website.com/goodmorning.php</td>
</tr>

Do you know how to add a link in HTML?

Share this post


Link to post
Share on other sites

Yes. I've been developing in HTML for 20 years, built a dozen sites in HTML, PHP and JAVA, but I've never displayed an URL retrieved from a MYSQL table in an HTML table, and have been unable to find a working solution on the Net -- and I've been looking for a week..

In your example. you know what the URL is. That's ice cream.

<td text-align:="" center;="" width:="" 50%;="">https://www.website.com/goodmorning.php</td>

In my case, I have no idea what the URL is. It's coming from the database. It now displays in the table as text. My problem is how do I tell the HTML interpreter it is an hyperlink and not text.

<td text-align: center; width: 50%;>" . $row["the_url"] . "</td>

I do appreciate your assistance, Funce, and am here because W3 has the clearest and best presented documentation on the Net.

I'm a newbee here, but I've been doing this stuff in a variety of languages and environments since 1967, spent 25 years training professional developers and at age 81, may be the worlds oldest blogger. I'm pretty good, but this problem has me by the nads!

Share this post


Link to post
Share on other sites

What I was going to get to, is that while the manner at which you create the HTML is different, its still going to be HTML.

This means that you while you can create a fixed link like the following

<tr width:100%="">
         <td text-align:="" left;="" width:="" 50%;="">Good Morning!</td>
         <td text-align:="" center;="" width:="" 50%;=""><a href="https://www.website.com/goodmorning.php">https://www.website.com/goodmorning.php</a></td>
</tr>

Because you know how to make a link.

 

To make a link with something from a database is the same. You know that referencing your database fields resolves them to Text. You just need to put that text in the right spaces inside your HTML. Just because we make it with PHP, doesn't stop this.

Including dsonesuk's solution:

<?php
	while($row = $result->fetch_assoc()) {
        $the_url = $row["the_url"];
    echo "<tr width:100%>
         <td text-align: left; width: 50%;>" . $row["some_text"] . "</td>
         <td text-align: center; width: 50%;><a href='" . $row["the_url"] . "'>" . $row["the_url"] . "</a></td>
         </tr>";
    }
    echo "</table>";
    } else { echo "0 results"; }
        echo "The URL--> " . $the_url;

 

Share this post


Link to post
Share on other sites

That solution is as fine as wine!

You guys are the best and I am very grateful.

Do you guys have a tip jar or PayPal account?

 

 

Edited by PShannon

Share this post


Link to post
Share on other sites

Apart from my code adding inline css within style attribute. They are not table attributes.

Actually it would be better to add class name, then apply the styling you require to that class name, without the need to edit dynamic php code that created the table.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

It's working! I'm good and I am grateful for your help.

"A good plan today is better than a perfect plan tomorrow."
~ George Washington

Edited by PShannon

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...