Jump to content

News System Problems


Skatecrazy1

Recommended Posts

well i'm pretty well successfully creating a news system for myself, but i'm making it so it's simple enough that i can offer it as an open source script on my site once it's done.

well,
i'm having problems viewing comments, for instance here's the class file from my show news

[code]
<?php
/*
show_news.php
this file will show the news, with comments.
*/
//require db file created by installation

require('db.php');

class ShowNews {
      var $amt;
     
      function show_news($amt)
      {
        $self = $_SERVER['PHP_SELF'];
              require('db.php');
              //create dynamic sql based on func argument
              $sql = "SELECT * FROM `posts` ORDER BY `id` DESC LIMIT $amt";
              //query it up
              $rs = @mysql_query($sql, $conn) or die(mysql_error());
              while($row = mysql_fetch_array($rs)){
                //fetch no. of comments posted about this article
                $comm = "SELECT * FROM `comments` WHERE `postid`=\"".$row['id']."\"";
                $c_rs = @mysql_query($comm, $conn) or die(mysql_error());
                  //fetch number of comments here
                  $c_num = mysql_num_rows($c_rs);
                  $news .= "<p>";
                  $news .= "<table cellspacing=\"2\" cellpadding=\"4\" border=\"1\">";
                  $news .= "<tr><td>";
                  $news .= "<h3>".$row['title']."</h3>";
                  $news .= "</td></tr>";
                  $news .= "<tr><td>".$row['body']."</td></tr>";
                  $news .= "<tr><td><i>By ".$row['author'].", on ".$row['timestamp']."</i></td></tr>";
                  $news .= "<tr><td><div align=\"right\"><i><a href=\"$self?comm=".$row['id']."\">".$c_num." Comments</div></td></tr>";
                  $news .= "</table>";
                  $news .= "</p>";
              }
             
              echo $news;
             
      }
     

}
//set GET value based on link we made previously
$id = $_GET['comm'];
//write query that will grab the post based on the link we clicked
  $grab_post = "SELECT * FROM `posts` WHERE `id`=\"$id\" LIMIT 1";
  $result = @mysql_query($grab_post, $conn) or die(mysql_error());
  //write query to grab comments for this specific post
  $grab_comm = "SELECT * FROM `comments` WHERE `postid`=\"$id\" ORDER BY `id` DESC";
  $gb_rs = @mysql_query($grab_comm, $conn) or die(mysql_error());
  $news .= "<p>";
  $news = "<table cellspacing=\"2\" cellpadding=\"4\" border=\"1\">";
  $news .= "<tr><td>";
  $news .= "<h3>".$row['title']."</h3>";
  $news .= "</td></tr>";
  $news .= "<tr><td>".$row['body']."</td></tr>";
  $news .= "<tr><td><i>By ".$row['author'].", on ".$row['timestamp']."</i></td></tr>";
  $news .= "</table>";
  $news .= "</p>";
  //start building comment part of table
  $news .= "<p>";
  $news .= "<table cellspacing=\"2\" cellpadding=\"4\" border=\"1\" background=\"teal\">";
  while($row = mysql_fetch_array($gb_rs))
  {
    $link = $row['email'];
    $news .= "<tr><td>";
    if($link != null){
      $news .= "<a href=\"mailto:$link\">".$row['name']."</a>";
      } else {
      $news .= $row['name'];
      }
    $news .= "</td></tr>";
    $news .= "<tr><td>".$row['body']."</td></tr>";
  }
 
  $news .= "</table>";
 
  echo $news;



?>
[/code]

and the file it's being run on:
[code]
<?php
include('show_news.php');
$news = new ShowNews;
$news->show_news(2);
?>[/code]

i'm having a hard time viewing the comments separately without having to use a separate file, based on the GET values in the links

any help appreciated.
Link to comment
https://forums.phpfreaks.com/topic/26799-news-system-problems/
Share on other sites

Well there are lots of issues with this code:-

Firstly, the following query is a waste since the recordset obtained $result is not being used anywhere:

[code]$grab_post = "SELECT * FROM `posts` WHERE `id`=\"$id\" LIMIT 1";
$result = @mysql_query($grab_post, $conn) or die(mysql_error());[/code]

Secondly, there are no values in [b]$row['title'][/b] , [b]$row['body'][/b]

Thirdly, the variable [b]$news[/b] is used as a string at some places and as an object of [b]ShowNews[/b] class later. If you were to put the following two lines

[code]$news = new ShowNews;
$news->show_news(2);[/code]

In the second last line of show_news.php before [b]echo $news[/b] then the separate comments will be lost. Was that the case why separate comments were not getting displayed?
Link to comment
https://forums.phpfreaks.com/topic/26799-news-system-problems/#findComment-122658
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.