Jump to content

Who can help with making a simple MySQL query?


danest

Recommended Posts

I have a website baker site with maybe 150 News posts, that are all enabled for comments. I would like to see what is going on in the comments. It needs a little SQL, but I will take me forever to try to figure  that out myself.

What I need is to produce a table with, say the last 30 comments, with coloumn headings like:

Time, Username, Comment heading, Post heading.

I have the module News installed, and the table mod_news_comments contain the following fields:

comment_id, section_id, page_id, post_id, title, comment, commented_when, commented_by

The field user_id from the "users" table should joined with the "commented_by" from above, so that the query can produce the "username" and "display_name" from users.

Well, there are different issues, and i am not able to make this work myself. Are there anybody willing to help me with this?

The site is:

www.hidethedecline.eu

 

Link to comment
Share on other sites

I have a website baker site with maybe 150 News posts, that are all enabled for comments. I would like to see what is going on in the comments. It needs a little SQL, but I will take me forever to try to figure  that out myself.

What I need is to produce a table with, say the last 30 comments, with coloumn headings like:

Time, Username, Comment heading, Post heading.

 

What I have managed so far by copy and paste and fiddling a little is this code:

// Specify the Group(id) you want to read the news from or 0 for all groups
$group = 0;       

// Specify number of newsitems                       
$limit=30;

// Specify read more text
$readmore = "Read more";       

// Specify Older news text
$oldernews = "Older items";       

// Query for obtaining stuff
$query = "SELECT * FROM ".TABLE_PREFIX."mod_news_comments ORDER BY comment_ID DESC LIMIT 0, 30;";

// Query for all groups 
if ($group<1 ) {
  $query = "SELECT * FROM ".TABLE_PREFIX."mod_news_comments ORDER BY comment_ID DESC LIMIT 0, 30;";
}

// No changes needed here, maybe for output!

global $database;

$result = $database->query($query);
$total = $result->numRows();       // how many items are there?

if ($total>200) {                                // at least 2 needed for 2 news items
  $data = $result->fetchRow() ;    // get left item
  $data2 = $result->fetchRow() ;  // get right item
  echo '<table width=100% border=1>';
  echo '<tr><td width=50%><b>'.$data['title'].'</b></td><td></td></tr>';
  echo '<tr><td>'.$data['content_short'].'</td><td></td></tr>';
  echo '<tr><td><a href="'.WB_URL.PAGES_DIRECTORY.$data['link'].PAGE_EXTENSION.'">'.$readmore.'</a></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td width=50%><b>'.$data2['title'].'</b></td></tr>';
  echo '<tr><td></td><td>'.$data2['content_short'].'</td></tr>';
  echo '<tr><td></td><td><a href="'.WB_URL.PAGES_DIRECTORY.$data2['link'].PAGE_EXTENSION.'">'.$readmore.'</a></td></tr>';
  echo '</table>';
}

// Next show the rest
if ($total>0) {                                // are there any left?
echo '<br><br><table><tr><td colspan=2><b>'.$oldernews.'</b></td></tr>';
echo '<tr><td>'.$data['commented_when'].'</td><td>Comment title</td></tr>';
while ($data = $result->fetchRow()) { 
  echo '<tr><td>'.strftime( "%d-%m-%Y",$data['posted_when']).'</td>';
  echo '<td><a href="'.WB_URL.PAGES_DIRECTORY.$data['link'].PAGE_EXTENSION.'">'.$data['title'].'</a></td></tr>';
}
echo '</table>';
}

 

That produces this result: http://www.hidethedecline.eu/pages/links/test-sql.php

 

Besides the problems with making the JOIN queries, the above query gives wrong dates, and the links doesn't work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.