pogester Posted June 8, 2007 Share Posted June 8, 2007 Hi there, I am fairly new to PHP programming and need some help. I have created a news script using a mysql database that allows me to input news through the browser and then the news is displayed on my home page, however I am wanting to simply display the news header (and maybe a short news description) and have it so the visitor clicks on the headline or on a link to be taken to the news article. I know this has something to do with the ID and linking through that but so far am having no luck getting it to work. Any help would be much appreciated. Cheers Dazz Link to comment https://forums.phpfreaks.com/topic/54820-news-script/ Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 Some code would be helpful; you should make a news.php page with something like this in it: <?php if(is_numeric($_GET["id"])) { $id = $_GET["id"]; } if(isset($id)) { $query = mysql_query("SELECT * FROM news WHERE id = '{$id}'") or die(mysql_error()); // ... } ?> And to link to the articles: <a href="news.php?id=1">header</a> Link to comment https://forums.phpfreaks.com/topic/54820-news-script/#findComment-271135 Share on other sites More sharing options...
pogester Posted June 8, 2007 Author Share Posted June 8, 2007 Hi thanks, this is the code I have for my news.php page <?php include ('mysql_connect.php'); $query = "SELECT id,title,author,post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_data"; $result = @mysql_query($query); if ($result) { while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $url = 'comments.php?id='.$row['id']; echo '<p> <a href="viewArticle.php?id='.$row['id'].'">'; echo $row['title']; echo '</a></p>'; } } else { echo 'There are no news posts to display'; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/54820-news-script/#findComment-271140 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 viewArticle.php <?php include("mysql_connect.php"); if(is_numeric($_GET["id"])) { $id = $_GET["id"]; } if(isset($id)) { $query = mysql_query("SELECT * FROM news_data WHERE id = '{$id}'") or die(mysql_error()); // Output how you like here } ?> Link to comment https://forums.phpfreaks.com/topic/54820-news-script/#findComment-271145 Share on other sites More sharing options...
pogester Posted June 8, 2007 Author Share Posted June 8, 2007 Hi thanks for your help, I have created the viewArticle.php file as per your advice, full code below, but for some reason when I click the news link all that is displayed is the line Posted by: I am sure I am probably making an obvious mistake but cant for the life of me work out what it is. <?php include ('mysql_connect.php'); if(is_numeric($_GET["id"])) { $id = $_GET["id"]; } if(isset($id)) { $query = mysql_query("SELECT * FROM news_data WHERE id = '{id}'")or die(mysql_error()); echo '<h1><b>'.$row['title'].'</b></h1><br /> '.$row['sd'].'<br /> Posted by : <b>'.$row['author'].'</b><br /> '.$row['post'].'<br /> </p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/54820-news-script/#findComment-271152 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 <?php include ('mysql_connect.php'); if(is_numeric($_GET["id"])) { $id = $_GET["id"]; } if(isset($id)) { $query = mysql_query("SELECT * FROM news_data WHERE id = '{id}'")or die(mysql_error()); $row = mysql_fetch_assoc($query); echo '<h1>'.$row['title'].'</h1> '.$row['sd'].' Posted by : '.$row['author'].' '.$row['post'].' </p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/54820-news-script/#findComment-271154 Share on other sites More sharing options...
pogester Posted June 8, 2007 Author Share Posted June 8, 2007 OK well I dont know what I have done wrong here, copied the code you suggested but am still only getting the viewArticle.php displaying the line Posted by, yet it works fine if I display the full news all together so I know that the database is working?? Am totally lost now!! Link to comment https://forums.phpfreaks.com/topic/54820-news-script/#findComment-271158 Share on other sites More sharing options...
chigley Posted June 9, 2007 Share Posted June 9, 2007 <?php include("mysql_connect.php"); if(is_numeric($_GET["id"])) { $id = $_GET["id"]; } if(isset($id)) { $query = mysql_query("SELECT * FROM news_data WHERE id = '{id}'")or die(mysql_error()); $row = mysql_fetch_assoc($query) or die(mysql_error()); echo "<h1>{$row["title"]}</h1>\n\n{$row["sd"]}\n\nPosted by : {$row["author"]}\n\n{$row["post"]}\n\n</p>"; } else { echo "ID not found"; } ?> Link to comment https://forums.phpfreaks.com/topic/54820-news-script/#findComment-271292 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.