jewelsmac6 Posted March 7, 2011 Share Posted March 7, 2011 Hello all! I am somewhat new to PHP and SQL, but I think I have a basic understanding of how the language works. I have read through a million different websites and forums, and have tried a million different ways but am just not getting anywhere! This is what I am trying to do... I have a text link that I want to click through to an outside URL from my website. This outside URL was already entered into an SQL database, and I just cant seem to get the text link to bring up the outside URL from the database. This is what I have so far... 1.) My webpage that I am working on is www.mywebsite.com/articles.php 2.) I want the text "Read more..." on this webpage to click through to an outside URL, like "http://www.google.com", which was already entered into an SQL database under the title of "article_link" in a new window 3.) So I would like for "Read more..." to click through the outside URL that was entered into "article_link" in the database <?php echo <p><a href="\' . $article_link . '\" target="_blank">Read more...</a></p>; ?> Nothing I have tried has worked so far, nothing clicks through to the outside URL in the "article_link" entry from the database. Sorry if I am being redundant, I have been at this all day! Please help and thank you in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/229846-text-link-to-a-url-variable-from-a-database/ Share on other sites More sharing options...
HuggieBear Posted March 7, 2011 Share Posted March 7, 2011 You need to make sure that the value coming from the database is actually valid, we can't see if this is the case as you've provided no code and no example where it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/229846-text-link-to-a-url-variable-from-a-database/#findComment-1184095 Share on other sites More sharing options...
TOA Posted March 7, 2011 Share Posted March 7, 2011 I think your quotes are just wrong Try this: echo '<p><a href="'.$article_link.'">Read More...</a></p>'; or echo "<p><a href=\"$article_link\">Read More...</a></p>"; Either one should work...taking into account what HuggieBear said Quote Link to comment https://forums.phpfreaks.com/topic/229846-text-link-to-a-url-variable-from-a-database/#findComment-1184111 Share on other sites More sharing options...
jewelsmac6 Posted March 7, 2011 Author Share Posted March 7, 2011 Thank you both for your replies! Based on what you said, I went back and added the variables into the code that are entered in my database. Below is my entire code from top to bottom, inserted above the <head> tag. However, with this code, nothing is showing up on the page. Not even the "article_title" or "article_source" or "article_author" which are just typed in text into the database. What did I do wrong? Thank you!! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php // This file is www.developphp.com curriculum material // Written by Adam Khoury January 01, 2011 // http://www.youtube.com/view_play_list?p=442E340A42191003 // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php // Run a select query to get my letest 6 items // Connect to the MySQL database include "admin/storescripts/connect_to_mysql.php"; $dynamicList = ""; $sql = mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50"); $articleCount = mysql_num_rows($sql); // count the output amount if ($articleCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $article_title = $row["article_title"]; $article_source=$row["article_source"]; $article_author=$row["article_author"]; $article_link=$row["article_link"]; $output = '<table width="350" height="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="108" align="left" valign="top"> <p class="article_title">' . $article_title . '</p> <p class="pets_story_homepage">' . $article_source . ', ' . $article_author . '</p> <p><a href="<?php # echo "<a href=\"$article_link\">$article_title</a>"; # ?>">Read more...</a></p></table>'; mysql_close(); } } else { $dynamicList = "There aren't any articles yet, submit one today!"; } ?> <?php // Run a select query to get my letest 6 items // Connect to the MySQL database include "admin/storescripts/connect_to_mysql.php"; $dynamicList = ""; $sql = mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50"); $row = mysql_fetch_array($sql); $article_title = $row["article_title"]; $article_source=$row["article_source"]; $article_author=$row["article_author"]; $article_link=$row["article_link"]; $output = '<table width="350" height="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="108" align="left" valign="top"> <p class="article_title">' . $article_title . '</p> <p class="pets_story_homepage">' . $article_source . ', ' . $article_author . '</p> <p> echo "<p><a href=\"$article_link\">Read More...</a></p>"; </table>'; mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/229846-text-link-to-a-url-variable-from-a-database/#findComment-1184149 Share on other sites More sharing options...
jasonrichardsmith Posted March 7, 2011 Share Posted March 7, 2011 ? my sql close seems to be hanging out there. ?>">Read more...</a></p></table>'; mysql_close(); } } else { $dynamicList = "There aren't any articles yet, submit one today!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229846-text-link-to-a-url-variable-from-a-database/#findComment-1184151 Share on other sites More sharing options...
jewelsmac6 Posted March 7, 2011 Author Share Posted March 7, 2011 Ah ha! After a little more playing around with it and going back and forth between the other code that is successfully working from another table in my database, it is finally working perfectly! I can finally breathe now! Thank you for all your help!!!! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php // This file is www.developphp.com curriculum material // Written by Adam Khoury January 01, 2011 // http://www.youtube.com/view_play_list?p=442E340A42191003 // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php // Run a select query to get my letest 6 items // Connect to the MySQL database include "admin/storescripts/connect_to_mysql.php"; $dynamicList = ""; $sql = mysql_query("SELECT * FROM articles ORDER BY date_added DESC LIMIT 50"); $articleCount = mysql_num_rows($sql); // count the output amount if ($articleCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $article_title = $row["article_title"]; $article_source = $row["article_source"]; $article_author = $row["article_author"]; $article_link = $row["article_link"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $dynamicList .= '<table width="300" height="170" border="0" cellspacing="0" cellpadding="6"> <tr> <td width="288" valign="top" align="center"> <p>' . $article_title . ' </p> <p>' . $article_source . ' ' . $article_author . '</p> <p><a href="' . $article_link . '" target="_blank">Read More...!</a></p></td> </tr> </table>'; } } else { $dynamicList = "We have no pets submitted yet, won't you submit your pet's photo today?"; } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/229846-text-link-to-a-url-variable-from-a-database/#findComment-1184160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.