jpopuk Posted August 8, 2009 Share Posted August 8, 2009 Hi, I have 2 problems - I have setup a simple search. The first thing is I don't know the best way to add in a line that says 'No items matched your search.' - I am not sure if it would be an if statement for an error. Also, the next thing is with my code when results produce more than the desired number (15) when I click next it loads nothing and then when I click back it doesn't load nothing again. Here is my code, if anyone knows what the best ways are to resolve my problems are I would be most grateful. Many thanks. <div> <?php $counter = 0; ?> <?php foreach (get_results($page) as $result) { ?> <?php switch ($result['table_name']) { case 'forum_posts': $url = '/forum/' . get_url_forum($result['forum_id'], $result['forum_name'], $result['topic_id'], $result['title'], $result['result_id']); break; case 'news_posts': $url = get_url('/news/', $result['category_id'], $result['result_id'], $result['title']); break; case 'pages': $url = get_url('/', $result['category_id'], $result['result_id'], $result['title'], $result['default_page']); break; case 'products': $url = get_url('/products/', $result['category_id'], $result['result_id'], $result['title']); break; } ?> <?php echo $counter != 0 ? '<hr />' : ''; ?> <a href="<?php echo $url; ?>"><?php echo $result['title']; ?></a><br /> <?php echo strlen($result['content']) > 500 ? substr(strip_tags(check_output($result['content'], true, true, true)), 0, 500) . '...' : strip_tags(check_output($result['content'], true, true, true)); ?><br /> <span class="url"><?php echo $url; ?></span> <?php $counter++; ?> <?php } ?> </div> <?php echo multipage(get_num_results(), 15, $page, '/search/index.htm?search_string=' . $search_string . '§ion=' . $section . '&user_id=' . $user_id . '&page='); ?> Below is the main code <?php define('ROOT_PATH', './../'); require ROOT_PATH . 'includes/common.inc.php'; $tpl = new template(ROOT_PATH . $config['template_path']); $body = new template(ROOT_PATH . $config['template_path'] . 'search/'); $tpl->set('title', 'Search - ' . $config['site_name']); $tpl->set('navigation', '<a href="/">Home</a> » Search'); /** * get_num_results() - get the number of results * * @return integer, number of results */ function get_num_results() { $search_string = isset($_POST['search_string']) ? check_input($_POST['search_string']) : ''; $section = isset($_POST['section']) ? check_input($_POST['section']) : 'All'; $user_id = isset($_GET['user_id']) && is_numeric($_GET['user_id']) ? (int) $_GET['user_id'] : 0; if ($user_id == 0) { $query = ""; if ($section == 'All' || $section == 'Forum') { $query .= "SELECT p.post_id AS result_id FROM " . $GLOBALS['config']['db']['prefix'] . "forum_posts p LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "forum_topics t ON p.topic_id = t.topic_id WHERE MATCH (p.message) AGAINST ('$search_string') OR t.subject LIKE '%$search_string%'"; } if ($section == 'All') { $query .= " UNION ALL "; } if ($section == 'All' || $section == 'News') { $query .= "SELECT post_id AS result_id FROM " . $GLOBALS['config']['db']['prefix'] . "news_posts WHERE MATCH (title, content) AGAINST ('$search_string')"; } if ($section == 'All') { $query .= " UNION ALL SELECT page_id AS result_id FROM " . $GLOBALS['config']['db']['prefix'] . "pages WHERE MATCH (title, content) AGAINST ('$search_string') UNION ALL "; } if ($section == 'All' || $section == 'Products') { $query .= "SELECT product_id AS result_id FROM " . $GLOBALS['config']['db']['prefix'] . "products WHERE MATCH (product_name, content) AGAINST ('$search_string')"; } } else { $query = "SELECT p.post_id AS result_id FROM " . $GLOBALS['config']['db']['prefix'] . "forum_posts p LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "forum_topics t ON p.topic_id = t.topic_id WHERE (MATCH (p.message) AGAINST ('$search_string') OR t.subject LIKE '%$search_string%') AND p.user_id = $user_id"; } $result = $GLOBALS['db']->query($query); return $GLOBALS['db']->num_rows($result); } /** * get_results() - get the results in a category * * @param $page integer, current page * @return array, results */ function get_results($page) { $search_string = isset($_POST['search_string']) ? check_input($_POST['search_string']) : ''; $section = isset($_POST['section']) ? check_input($_POST['section']) : 'All'; $user_id = isset($_GET['user_id']) && is_numeric($_GET['user_id']) ? (int) $_GET['user_id'] : 0; if ($user_id == 0) { $query = ""; if ($section == 'All' || $section == 'Forum') { $query .= "(SELECT p.post_id AS result_id, f.category_id, t.subject AS title, p.message AS content, '' AS default_page, t.forum_id, f.forum_name, t.topic_id, 'forum_posts' AS table_name, MATCH (p.message) AGAINST ('$search_string') AS score FROM " . $GLOBALS['config']['db']['prefix'] . "forum_posts p LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "forum_topics t ON p.topic_id = t.topic_id LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "forum_forums f ON t.forum_id = f.forum_id WHERE MATCH (p.message) AGAINST ('$search_string') OR t.subject LIKE '%$search_string%')"; } if ($section == 'All') { $query .= " UNION ALL "; } if ($section == 'All' || $section == 'News') { $query .= "(SELECT post_id AS result_id, category_id, title, content, '' AS default_page, '' AS forum_id, '' AS forum_name, '' AS topic_id, 'news_posts' AS table_name, MATCH (title, content) AGAINST ('$search_string') AS score FROM " . $GLOBALS['config']['db']['prefix'] . "news_posts WHERE MATCH (title, content) AGAINST ('$search_string'))"; } if ($section == 'All') { $query .= " UNION ALL (SELECT page_id AS result_id, category_id, title, content, default_page, '' AS forum_id, '' AS forum_name, '' AS topic_id, 'pages' AS table_name, MATCH (title, content) AGAINST ('$search_string') AS score FROM " . $GLOBALS['config']['db']['prefix'] . "pages WHERE MATCH (title, content) AGAINST ('$search_string')) UNION ALL "; } if ($section == 'All' || $section == 'Products') { $query .= "(SELECT product_id AS result_id, category_id, product_name AS title, content, '' AS default_page, '' AS forum_id, '' AS forum_name, '' AS topic_id, 'products' AS table_name, MATCH (product_name, content) AGAINST ('$search_string') AS score FROM " . $GLOBALS['config']['db']['prefix'] . "products WHERE MATCH (product_name, content) AGAINST ('$search_string'))"; } $query .= " ORDER BY score DESC LIMIT " . ($page - 1) * 15 . ", 15"; } else { $query = "SELECT p.post_id AS result_id, f.category_id, t.subject AS title, p.message AS content, t.forum_id, f.forum_name, t.topic_id, 'forum_posts' AS table_name, MATCH (p.message) AGAINST ('$search_string') AS score FROM " . $GLOBALS['config']['db']['prefix'] . "forum_posts p LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "forum_topics t ON p.topic_id = t.topic_id LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "forum_forums f ON t.forum_id = f.forum_id WHERE (MATCH (p.message) AGAINST ('$search_string') OR t.subject LIKE '%$search_string%') AND p.user_id = $user_id ORDER BY p.date_created DESC LIMIT " . ($page - 1) * 15 . ", 15"; } $result = $GLOBALS['db']->query($query); return fetch_array($result); } $body->set('search_string', isset($_REQUEST['search_string']) ? check_input($_REQUEST['search_string']) : ''); $body->set('section', isset($_REQUEST['section']) ? check_input($_REQUEST['section']) : 'All'); $body->set('user_id', isset($_GET['user_id']) && is_numeric($_GET['user_id']) ? (int) $_GET['user_id'] : 0); $body->set('page', isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1); $tpl->set('body', $body->fetch('index.tpl.php')); echo $tpl->fetch('layout.tpl.php'); ?> Link to comment https://forums.phpfreaks.com/topic/169408-search-implement-error/ Share on other sites More sharing options...
zq29 Posted August 8, 2009 Share Posted August 8, 2009 We'll have to assume a number of things here, as you have not posted enough of your code to be sure about anything. Assuming your get_results() function returns false if it doesn't have anything to display, you could do this: $r = get_results($page); if($r) { //Loop and display } else { echo "No results"; } As for your pagination, without seeing what get_results() and multipage() do, it's kinda hard to say... EDIT: OP posted the rest of the code while I was writing this. Link to comment https://forums.phpfreaks.com/topic/169408-search-implement-error/#findComment-893819 Share on other sites More sharing options...
jpopuk Posted August 8, 2009 Author Share Posted August 8, 2009 I have added all the code now in the top message, let me know if that is of more help. Cheers! Link to comment https://forums.phpfreaks.com/topic/169408-search-implement-error/#findComment-893826 Share on other sites More sharing options...
jpopuk Posted August 9, 2009 Author Share Posted August 9, 2009 Ok, ignore the second part. The 'No results' didn't work. - I tried to have a little play around with it but no luck, I want to do these things by myself but I guess to learn you have to learn from other resources. Cheers. Link to comment https://forums.phpfreaks.com/topic/169408-search-implement-error/#findComment-894024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.