beerguts Posted September 28, 2009 Share Posted September 28, 2009 Hi All, I am setting up news articles on the front page of my website so that only the date and subject of the article is displayed as a hyperlink. When the user clicks on the hyperlink they are directed to a page which displays the whole article. I have created the code for the front page no problem: $result = mysql_query("SELECT news_title, news_body, news_id, DATE_FORMAT(news_date, '%D %b %Y') AS formattedRacedate FROM news ORDER BY news_id DESC LIMIT 10 ")or die(mysql_error()); while($article=mysql_fetch_array($result)) { echo "<tr>"; echo "<td width=100px valign='top'>".$article['formattedRacedate']."</td>"; echo '<td><a href="news.php?article=' . $article['news_id'] . '">'. $article['news_title'] . '</a></td>'; echo "</tr>"; } echo "</table>"; this is where my limited PHP knowledge starts to dry up! What code do I need for "news.php" to ensure the correct article is displayed? The URL is correctly displaying the news_id at the end. Thanks in advance for your help Quote Link to comment https://forums.phpfreaks.com/topic/175756-solved-news-article-using-the-subject-as-a-hyperlink/ Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 First you'd get the article id from the URL using the $_GET super-global, then after securing the variable (with mysql_real_escape_string()) setting up a query to return the information from that article. Like so: $article_id = mysql_real_escape_string($_GET['article']); $result = mysql_query("SELECT news_title, news_body, news_id, DATE_FORMAT(news_date, '%D %b %Y') AS formattedRacedate FROM news WHERE news_id='$article_id'")or die(mysql_error()); $row = mysql_fetch_assoc($result); // Not a loop because it's only 1 record echo $row['news_body']; // The whole article You can then format it anyway you want. Preferably you should also preform a check first to make sure that an article by that id really exists, like so: $article_id = mysql_real_escape_string($_GET['article']); $result = mysql_query("SELECT news_title, news_body, news_id, DATE_FORMAT(news_date, '%D %b %Y') AS formattedRacedate FROM news WHERE news_id='$article_id'")or die(mysql_error()); if(!mysql_num_rows($result)) { //Article doesn't exist } else { $row = mysql_fetch_assoc($result); // Not a loop because it's only 1 record echo $row['news_body']; // The whole article } Quote Link to comment https://forums.phpfreaks.com/topic/175756-solved-news-article-using-the-subject-as-a-hyperlink/#findComment-926193 Share on other sites More sharing options...
beerguts Posted September 28, 2009 Author Share Posted September 28, 2009 Awesome! Thanks for your help and the easy to understand explanation - I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/175756-solved-news-article-using-the-subject-as-a-hyperlink/#findComment-926227 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.