insejn Posted April 3, 2008 Share Posted April 3, 2008 Hi, This is my first post on any php-forum.. I have given up searching the net for my answer. It seems such a simple task but still I cant seem to find the solution. My problem is this: I'm creating a type of news section on my website where I want to display a preview of the last 3 articles in the database with a "read this.." link under each preview that takes the user to for example "read_news.php" and shows the chosen news from the database. The thing I cant seem to find the solution for is how to create the read this link on the preview-page that takes you to the "read_news.php?article_id=...." and displays the right $article_title + $article_body + $article_written_by + $article_date from the chosen $article_id.. I think I get the link to work on the preview.php page with this code: echo "<a href='read_news.php?article_id=$row[0]'>read this...</a>"; ..this links to the read_news.php and displays "read_news.php?article_id=14" in the adressfield but the page doesn't show anything. read_news.php looks at the moment like this: <? //database connection $dbhost = 'localhost'; $dbuser = '...'; $dbpass = '...'; $dbname = '...'; if(isset($_GET['article_id'])) { $query = "SELECT article_id, article_title, article_body ". "FROM articles_table ". "WHERE article_id = '{$_GET['article_id']}'"; $result = mysql_query($query) or die('Error : ' . mysql_error()); list($article_id, $article_title, $article_body) = mysql_fetch_array($result;) echo "$article_title<br>"; echo "$article_body<br>"; echo "$article_written_by<br>"; echo "$article_date<br>"; ?> ..and obviously this is'nt the right way of solving it. I'm totally lost here and there's probably a really simple solution to this but I cant seem to figure it out. Am I making any sense at all? /Björn (that's actually Bear in English:)) Quote Link to comment https://forums.phpfreaks.com/topic/99439-display-chosen-article-on-read_newsphp/ Share on other sites More sharing options...
stuffradio Posted April 3, 2008 Share Posted April 3, 2008 If I understand you correctly... you want to have the last 3 news articles displayed as a preview. Below that preview you want a "read this" link under them. You are going to want to use a while statement to select the articles. <?php $random_var = mysql_query("SELECT * FROM `articles_table` ORDER BY article_id DESC LIMIT 0,3"); while ($article_preview = mysql_fetch_array($random_var)) { // display the link here. You can limit the number of chars the article prints out, and than after that link to the article id etc. by using $article_preview['article_id']; } ?> Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/99439-display-chosen-article-on-read_newsphp/#findComment-508878 Share on other sites More sharing options...
insejn Posted April 5, 2008 Author Share Posted April 5, 2008 Thanks for the answer it cleared up some confusions I had about that code. But the main problem I have is to get the "chosen" news on the read_news.php page. The code looks like this on the preview page news.php [color=orange]// Get 3 news from the "articles_table" table and order them with the latest first![/color] $result = mysql_query("SELECT * FROM articles_table ORDER BY article_id DESC LIMIT 3") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { [color=orange]// Print out the contents of each row into a table[/color] echo "<table width='400' border='0' cellspacing='1' cellpadding='2'>"; echo "<tr><td bgcolor='#CCFF00'><font color='#333333'>".$row['article_date']."</font></td></tr>"; echo "<tr><td><font size='+1'>".$row['article_title']."</font><font class='console'> [ ".$row['article_console']." ]</font></td></tr>"; echo "<tr><td>".$row['article_body']."</td></tr>"; echo "<tr><td><i><font class='console'>Skriven av: ".$row['article_written_by']."</font></i></td></tr></table><br>"; echo "<a href='read_news.php?article_id=$row[0]'>read more..</a>"; } ?> ..How do I get the read_news.php to display the selected news? some sort of $_GET['article_id'] code? And how do I limit the number of chars from the $row['$article_body'] to show for exampe the first 30 chars? Quote Link to comment https://forums.phpfreaks.com/topic/99439-display-chosen-article-on-read_newsphp/#findComment-510211 Share on other sites More sharing options...
Barand Posted April 6, 2008 Share Posted April 6, 2008 read_news.php <?php //database connection $dbhost = 'localhost'; $dbuser = '...'; $dbpass = '...'; $dbname = '...'; mysql_connect($dbhost,$dbuser,$dbpass); // these were mysql_select_db($dbname); // missing if(isset($_GET['article_id'])) { $query = "SELECT article_id, article_title, article_body ". "FROM articles_table ". "WHERE article_id = '{$_GET['article_id']}'"; $result = mysql_query($query) or die('Error : ' . mysql_error()); list($article_id, $article_title, $article_body) = mysql_fetch_row($result); // use fetch_row with list echo "$article_title<br>"; echo "$article_body<br>"; echo "$article_written_by<br>"; // not selected in query echo "$article_date<br>"; // not selected in query } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99439-display-chosen-article-on-read_newsphp/#findComment-510278 Share on other sites More sharing options...
insejn Posted April 6, 2008 Author Share Posted April 6, 2008 Thank you very much! such an easy and noobish misstake that was I did ..but now I get an Error shown instead: "Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE article_id = '12'' at line 1". Is the line incorrect to use then? or might something else be the problem? Quote Link to comment https://forums.phpfreaks.com/topic/99439-display-chosen-article-on-read_newsphp/#findComment-510449 Share on other sites More sharing options...
insejn Posted April 6, 2008 Author Share Posted April 6, 2008 I got it fixed! it was that query line that was wrong.. I changed it to: $query = ("SELECT article_id, article_title, article_body, article_written_by, article_console, article_date FROM articles_table WHERE article_id = '{$_GET['article_id']}'"); Thank you guys for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/99439-display-chosen-article-on-read_newsphp/#findComment-510522 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.