Jump to content

Limiting Number of Search Results


jdrouin

Recommended Posts

I'm trying to limit the number of search results that display on a custom homepage I'm creating with Drupal. I found some PHP on the web that I'm using to print a list of nodes of the 'news' content type. It works fine, but it displays all of the relevant nodes, whereas I only want it to display 2 or 3 (it's a list of teasers that goes in a small column).

 

What should I add to this code to limit the number of results to 3?

 

<?php
/**
* This php snippet displays content of a specified type, with teasers
*
* To change the type of content listed, change the $content_type.
*
* Works with drupal 4.6
*/
  $content_type = 'news';
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = '$content_type' AND n.status = 1 ORDER BY n.created DESC"));
  while ($node = db_fetch_object($result1)) {
    $output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
$output .= theme('pager', NULL, $listlength);
print $output;
?>

 

Thanks in advance for any help.

 

Jeff

Link to comment
Share on other sites

Add a limit clause to the query:

 

$result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = '$content_type' AND n.status = 1 ORDER BY n.created DESC LIMIT 3"));

Link to comment
Share on other sites

I just noticed the following error message resulting from adding LIMIT 3 to the end of the SQL query.

 

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 10' at line 1 query: SELECT n.nid, n.created FROM node n WHERE n.type = 'news' AND n.status = 1 ORDER BY n.created DESC LIMIT 3 LIMIT 0, 10 in /*path-to-drupal-root*/includes/common.inc(1695) : eval()'d code on line 11.

 

I'm new to PHP, so not sure what this means yet.

Link to comment
Share on other sites

You have curly brackets around the table name that you shouldn't, try:

 

$result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM node n WHERE n.type = '$content_type' AND n.status = 1 ORDER BY n.created DESC LIMIT 3"));

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.