laff1975 Posted December 8, 2009 Share Posted December 8, 2009 Hi all, I hope someone can help me as I’m so close to completing a 2 month project that has involved learning PHP from scratch. I have my mysql database set up and can currently add articles. Each mysql record has its own unique id and using PHP I can currently echo the article title, date and article synopsis. I then have a read more link and I need it to display the whole article that is associated to that id. All I need to do is now create one link that links to one PHP page to displays the whole article for that id. I know you have to query the database and use the ? id=$id in the link to then display the article which is associated to that specific read more link. But I’m not sure how this completely works! I'm very new to PHP so please excuse if I have mentioned anything that seems stupid or obvious! Hope someone is able to help Thanks you Steve Link to comment https://forums.phpfreaks.com/topic/184389-creating-pages-with-the-get-function-php/ Share on other sites More sharing options...
cags Posted December 8, 2009 Share Posted December 8, 2009 You can create a list of articles using code that approximates to... $result = mysql_query("SELECT * FROM articles"); while($article = mysql_fetch_assoc($result)) { echo '<a href="/article.php?id=' . $article['id'] . '" />' . $article['title'] . '</a><br/>'; } Then in article.php you can use something like... if(isset($_GET['id'] && is_numeric($_GET['id'])) { $result = mysql_query("SELECT * FROM articles WHERE id={$_GET['id']} LIMIT 1"); $article = mysql_fetch_assoc($result); echo "<h2>" . $article['title'] . "</h2>"; echo "<p>" . $article['body_text'] . "</p>"; } If you wanted you could put the first block of code in and else block for the second block, so that if the article isn't found or id= isn't in the URL then a list of articles is displayed. Disclaimer: This was just a simple example the code isn't perfect nor does it entirely use best practices. Link to comment https://forums.phpfreaks.com/topic/184389-creating-pages-with-the-get-function-php/#findComment-973361 Share on other sites More sharing options...
laff1975 Posted December 8, 2009 Author Share Posted December 8, 2009 Hi, Stuck at work, so will give this ago at home tonight. Thank you for the advice, it is very much appriciated by this newbie. Thanks, Steve Link to comment https://forums.phpfreaks.com/topic/184389-creating-pages-with-the-get-function-php/#findComment-973367 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.