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
https://forums.phpfreaks.com/topic/205573-limiting-number-of-search-results/
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.

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"));

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.