PShannon Posted August 3, 2020 Share Posted August 3, 2020 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 . . . Link to comment Share on other sites More sharing options...
Funce Posted August 3, 2020 Share Posted August 3, 2020 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. Link to comment Share on other sites More sharing options...
PShannon Posted August 4, 2020 Author Share Posted August 4, 2020 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 . . . Link to comment Share on other sites More sharing options...
Funce Posted August 4, 2020 Share Posted August 4, 2020 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? Link to comment Share on other sites More sharing options...
PShannon Posted August 5, 2020 Author Share Posted August 5, 2020 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! Link to comment Share on other sites More sharing options...
dsonesuk Posted August 5, 2020 Share Posted August 5, 2020 '<td style="text-align: center; width: 50%;"><a href="' . $row["the_url"] . '">' . $row["the_url"] . '</a></td>'; Link to comment Share on other sites More sharing options...
Funce Posted August 5, 2020 Share Posted August 5, 2020 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; Link to comment Share on other sites More sharing options...
PShannon Posted August 6, 2020 Author Share Posted August 6, 2020 (edited) 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 August 6, 2020 by PShannon Link to comment Share on other sites More sharing options...
dsonesuk Posted August 6, 2020 Share Posted August 6, 2020 (edited) 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 August 6, 2020 by dsonesuk Link to comment Share on other sites More sharing options...
PShannon Posted August 7, 2020 Author Share Posted August 7, 2020 (edited) 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 August 7, 2020 by PShannon Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now