jdrouin Posted June 22, 2010 Share Posted June 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/205573-limiting-number-of-search-results/ Share on other sites More sharing options...
Alex Posted June 22, 2010 Share Posted June 22, 2010 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")); Quote Link to comment https://forums.phpfreaks.com/topic/205573-limiting-number-of-search-results/#findComment-1075700 Share on other sites More sharing options...
jdrouin Posted June 22, 2010 Author Share Posted June 22, 2010 Thank you for this suggestion, but when I added LIMIT 3 to the end of that line, nothing appeared in the search results at all. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/205573-limiting-number-of-search-results/#findComment-1075725 Share on other sites More sharing options...
jdrouin Posted June 22, 2010 Author Share Posted June 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/205573-limiting-number-of-search-results/#findComment-1075737 Share on other sites More sharing options...
Alex Posted June 22, 2010 Share Posted June 22, 2010 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")); Quote Link to comment https://forums.phpfreaks.com/topic/205573-limiting-number-of-search-results/#findComment-1075748 Share on other sites More sharing options...
jdrouin Posted June 22, 2010 Author Share Posted June 22, 2010 Hmmm. I removed the curly brackets from node, and the query still worked. But when I added LIMIT 3 to the end of the query, just like you showed, it throws the same error message. Quote Link to comment https://forums.phpfreaks.com/topic/205573-limiting-number-of-search-results/#findComment-1075757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.