neex1233 Posted July 11, 2009 Share Posted July 11, 2009 Hi, I am making a news script with a comment system, but I would like it so every time I created a new article, I could post comments like example.com/news.php?artid=1 and I'm pretty sure you use the get method and the array function, but how do you do it? Here is my show news script: <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_Name", $con); $sql = " SELECT title, poster, text, time, date FROM `news` LIMIT 0, 30 "; $result = mysql_query($sql) or die (mysql_error()); $results = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $results[] = $row; } foreach($results as $row) { $text = stripslashes($row['text']); $title = stripslashes($row['title']); $text = wordwrap($text, 50, "<br/>\n"); $date = $row['date']; $time = $row['time']; echo "<font size='4'><b>$title</b></font><br>"; echo "Posted by {$row['poster']} "; echo "on $date at $time<br>"; echo "$text"; echo "<br> <br>"; } ?> I'd also like to make it so the user can click on the article name and it would shot just that article and it's comments. Could anybody help me? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/ Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 You'll first have to provide a link to the article. To do this First change your query so it also returns the article id. $sql = " SELECT id, title, poster, text, time, date FROM `news` LIMIT 0, 30 "; Now you'll want to provide a link for the user to click on to view the article. $time = $row['time']; $id = $row['id']; echo "<font size='4'><b> <a href='article.php?id=".$id."'>$title</a></b></font><br>"; Now in article.php the basic code will be <?php if(isset($_GET['id'])) && is_numeric($_GET['id'])) { $id = (int) $_GET['id']; $sql = 'SELECT title, poster, text, time, date FROM `news` WHERE id='.$id; $result = mysql_query($sql); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); ?> <h1><?php echo $row['title']; ?></h1> <p>Posted by: <?php echo $row['poster']; ?> On <?php echp $row['date'] . ' ' . $row['time']; ?> <p><?php echo $row['text'];</p?> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/#findComment-873598 Share on other sites More sharing options...
neex1233 Posted July 11, 2009 Author Share Posted July 11, 2009 Okay, but what's the little thing at the bottom for?: <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/#findComment-873600 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 its to close the if statements in the first code block. If you notice I closed the first code block before the <h1> tag. As I closed the first code block I didnt close the if statments. Thats why at the end of the page I had to open another PHP code block to close the if statements. Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/#findComment-873601 Share on other sites More sharing options...
neex1233 Posted July 11, 2009 Author Share Posted July 11, 2009 Okay. Also the final code I am using: <?php $allow = array ('5');include ("/home/username/public_html/folder/protect.php"); ?> <title>News</title> <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_Name", $con); if(isset($_GET['id'])) && is_numeric($_GET['id'])) { $id = (int) $_GET['id']; $sql = 'SELECT title, poster, text, time, date FROM `news` WHERE id='.$id; $result = mysql_query($sql); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); ?> <h1><?php echo $row['title']; ?></h1> <p>Posted by: <?php echo $row['poster']; ?> On <?php echo $row['date'] . ' ' . $row['time']; ?> <p><?php echo $row['text'];</p?> <?php } } ?> Gives me this error: Parse error: syntax error, unexpected T_BOOLEAN_AND in /home/username/public_html/folder/folder/folder/show_news.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/#findComment-873603 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 Oops. Remove the highlighted character on line 7 if(isset($_GET['id'])) && is_numeric($_GET['id'])) That should sort the error. Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/#findComment-873604 Share on other sites More sharing options...
neex1233 Posted July 11, 2009 Author Share Posted July 11, 2009 Wait, nevermind. I just fixed the code. Here is my final one: <?php $allow = array ('5');include ("/home/username/public_html/folder/protect.php"); ?> <title>News</title> <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_Name", $con); if(isset($_GET['id']) && is_numeric($_GET['id'])) { $id = (int) $_GET['id']; $sql = 'SELECT title, poster, text, time, date FROM `news` WHERE id='.$id; $result = mysql_query($sql); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); ?> <h1><?php echo $row['title']; ?></h1> <p>Posted by: <?php echo $row['poster']; ?> On <?php echo $row['date'] . ' ' . $row['time']; ?> <p><?php echo $row['text'];?></p> <?php } } ?> Thanks for your help wildteen!! Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/#findComment-873606 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 Yep that code is fine. Quote Link to comment https://forums.phpfreaks.com/topic/165618-solved-php-get-method/#findComment-873611 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.