Page 1 of 1

Displaying single items

Posted: Sat Nov 13, 2004 10:19 am
by Trinity
Ok, I have a problem with the news system querying the database. Here is the code to get the news items from the database and display them on the page:

[code]$sql =
   "SELECT id,name,message,userid,title,DATE_FORMAT(postdate, '%a %D %b @ %H:%i') AS dated " .
   "FROM news ORDER BY postdate DESC";
 }
 $results = $this->_dCon->doQueryResult($sql);
 foreach ($results as $result) {
   echo '<div id="news">';
   echo '<p class="header">'. $result['title'] .' - by <a href="#">'. $result['name'] .'</a></p>';
   echo $result['message'];
   echo '<p class="footer">'. $result['dated'] .' - <a href="#"> Comments (10)</a></p>';  
   echo '</div>';
 }[/code]

That code works fine. The problem I'm having is when I try to display itemes which are called by their id:

[code]$sql = "SELECT id,name,news_id,message,userid,DATE_FORMAT(postdate, '%a %D %b @ %H:%i') AS dated FROM news_comments WHERE news_id=$id ORDER BY postdate DESC";
 $results = $this->_dCon->doQueryResult($sql);
 foreach ($results as $result) {
   echo '<div id="comments">';
   echo '<p><a href="#">'. $result['name'] .'</a> says:</p>';
   echo $result['message'];
   echo '<p class="commentftr">'. $result['dated'] .'</p>';
   echo '</div>';
 }[/code]

Now this works fine when the id is 1: [url=\"http://codecrypt.org/index.php?action=show&id=1\"]http://codecrypt.org/index.php?action=show&id=1[/url]
However, when the id is anything other than 1, it doesn't work:
[url=\"http://codecrypt.org/index.php?action=show&id=2\"]http://codecrypt.org/index.php?action=show&id=2[/url]

There is obviously a problem with the array but I can't work it out. Here is the code for the "doQueryResult" function:

[code]function doQueryResult($sql) {
 $this-> _dItems = "";
 if ($sql != "") {
   $this-> _dResult = mysql_query($sql, $this-> _dConnection);
   $dItem = array();
   while ($dItem = mysql_fetch_array($this-> _dResult)) {
   $this-> _dItems[] = $dItem;
   }
   return $this-> _dItems;
   mysql_free_result($this-> _dResult);
 }
 else {
   return mysql_error();
 }
 }[/code]

As you can see it puts the sql into an array. Maybe I need to change the function or add a new one that grabs just one item and doesn't put it in an array, but I think there is another way to solve this. Thanks in advance /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />

Displaying single items

Posted: Sat Nov 13, 2004 11:21 am
by ner0
mmm

[quote]$dItem = array();
  while ($dItem = mysql_fetch_array($this-> _dResult)) {
    $this-> _dItems[] = $dItem;[/quote]

shouldn\'t $dltems be called as an array() instead, cuz mysql_fetch_array does that auto, just a thought, could be why you\'re only getting one result if its not defined as an array, therefore just a variable.

Displaying single items

Posted: Sat Nov 13, 2004 11:39 am
by ner0
[b]
[code]$sql = "SELECT id,name,news_id,message,userid,DATE_FORMAT(postdate, \'%a %D %b @ %H:%i\') AS dated FROM news_comments WHERE news_id=$id ORDER BY postdate DESC";
 $results = $this->_dCon->doQueryResult($sql);
 foreach ($results as $result) {
  echo \'<div id="comments">\';
  echo \'<p><a href="#">\'. $result[\'name\'] .\'</a> says:</p>\';
  echo $result[\'message\'];
  echo \'<p class="commentftr">\'. $result[\'dated\'] .\'</p>\';
  echo \'</div>\';
 }[/code]

since its returnin one result, i\'d try somethin more simple, maybe:

[code]$sql = mysql_query("SELECT id,name,news_id,message,userid,DATE_FORMAT(postdate, \'%a %D %b @ %H %i\') AS dated FROM news_comments WHERE news_id=\'$id\'");

while ($news=mysql_fetch_array($sql))
{
  echo \'<div id="comments">\';
  echo \'<p><a href="#">\' . $news[\'name\'] . \'</a> says:</p>\';
  echo $news[\'message\'];
  echo \'<p class="commentftr">\' . $news[\'dated\'] . \'</p>\';
  echo \'</div>\';
}[/code]

not much simpler, and I don\'t know if the class has any special function to do with the query, but with this it\'d probably be easier to see where its going wrong.

^^ i have no idea wtf happened to the quotes up there, i hope they\'re still decipherable[/b]

Displaying single items

Posted: Sat Nov 13, 2004 11:46 am
by Trinity
I dunno what you did to the CSS there 0.o
The problem is, i want to use the sql class so that if we change the database, or want to use the news system on another site we don't have to recode each file, just the database + config files. I agree that that method would be simpler, but I want to try and find a way to do it using the class.
*keeps tinkering*

Displaying single items

Posted: Sun Nov 14, 2004 1:17 pm
by Scott
[url=\"http://www.codecrypt.org/?action=show&id=12\"]Clickety[/url]

There is nothing in id 2...coding is fine.