kkekec Posted October 11, 2007 Share Posted October 11, 2007 Hi all. I'm beginner at php and I really don't know how to solve this problem. Well I'm running tutorial site and when I tested SEARCH option something went wrong. On the first page everything is ok (limit is 10 per page) and after page 1 its not ok anymore. Some of tutorials that was found on page 1 are shown on page 2 too.. and they r displayed 10+ and not only 10 like on page 1. Well dunno how to explain better but look at the code please: <?php $start = 0; $end = 10; $page = 1; if(isset($_GET['page'])){ $page = $_GET['page']; $start = ($_GET['page']-1)*10; $end = $_GET['page']*10; } $total = $db->fetch($db->query("SELECT COUNT(*) FROM tutorials WHERE t_title LIKE '%".$terms."' OR t_title LIKE '%".$terms."%' OR t_title LIKE '".$terms."%' OR t_description LIKE '%".$terms."' OR t_description LIKE '%".$terms."%' OR t_description LIKE '".$terms."%' OR t_title = '".$terms."' OR t_author = '".$terms."'"), SQL_ROW); $latest_tutorials = "SELECT t_title, t_url, t_description, t_author, t_dateline FROM tutorials WHERE t_title LIKE '%".$terms."' OR t_title LIKE '%".$terms."%' OR t_title LIKE '".$terms."%' OR t_description LIKE '%".$terms."' OR t_description LIKE '%".$terms."%' OR t_description LIKE '".$terms."%' OR t_title = '".$terms."' OR t_author = '".$terms."' ORDER BY t_dateline DESC LIMIT ".$start.",".$end; $tutorials = $db->query($latest_tutorials); if($db->numrows($tutorials) > 0){ echo 'Page: '.$sys->pagination($total[0],$url='/search.php?q='.$_GET['q'].'&page=%s',$page); echo '<br /><br />'; while($row = $db->fetch()){ echo '<strong><a href="/'.$row['t_url'].'/step-1.html">'.$row['t_title'].'</a></strong> Author: <strong>'.$row['t_author'].'</strong> <br />'; if(strlen($row['t_description']) > 100){ $row['t_description'] = substr($row['t_description'], 0, 97).'...'; } echo $row['t_description'].' <strong>('.date('d/m/y', $row['t_dateline']).')</strong><br /><br />'; } echo 'Page: '.$sys->pagination($total[0],$url='/search.php?q='.$_GET['q'].'&page=%s',$page); } else { echo str_replace('<SEARCH_QUERY>', $_GET['q'], $sys->R('Text.NoSearchResults')); } ?> I cant figure it out... Thanks for helping me! Quote Link to comment https://forums.phpfreaks.com/topic/72863-solved-display-search-messy/ Share on other sites More sharing options...
littledragon Posted October 11, 2007 Share Posted October 11, 2007 OK, so if the page is 1... 0 x 10 is 0 1 x 10 is 10 so start is 0 and end is 10 page 2: 1 x 10 = 10 2 x 10 = 20 page 1 shows result 10, and so does page 2... page 2 will show result 20, and page 3 will also show it. try <?php $start = ($_GET['page']-1)*10+1; ?> making the first result 11 or 21 intsead of the last result from previous page Quote Link to comment https://forums.phpfreaks.com/topic/72863-solved-display-search-messy/#findComment-367458 Share on other sites More sharing options...
kkekec Posted October 11, 2007 Author Share Posted October 11, 2007 Ty for fast reply. But its not working. 1. page = 10 results 2. page = 20 results 3. page = 27 results 4. page = 17 results 5. page = 7 results And all results in total are 81 and should be only 48... so some of results are showing twice or more times... Quote Link to comment https://forums.phpfreaks.com/topic/72863-solved-display-search-messy/#findComment-367487 Share on other sites More sharing options...
littledragon Posted October 11, 2007 Share Posted October 11, 2007 I'm sorry, of course, I think I see the error now... <?php $latest_tutorials = "SELECT t_title, t_url, t_description, t_author, t_dateline FROM tutorials WHERE t_title LIKE '%".$terms."' OR t_title LIKE '%".$terms."%' OR t_title LIKE '".$terms."%' OR t_description LIKE '%".$terms."' OR t_description LIKE '%".$terms."%' OR t_description LIKE '".$terms."%' OR t_title = '".$terms."' OR t_author = '".$terms."' ORDER BY t_dateline DESC LIMIT ".$start.",".$end; ?> the LIMIT syntax DESC LIMIT ".$start.",".$end; is not the start and end number, it's the start number followed by the number of rows to return. try: <? $start = 0; $end = 10; $page = 1; if(isset($_GET['page'])){ $page = $_GET['page']; $start = ($_GET['page']-1)*10+1; } ?> Which basically just omits the redfinition of $end to be anything other than 10. I'm not sure about my +1 fix now since I'm not sure if mysqpl counts from row 0 or row 1... if you're missing a row on the first page (i.e. there'll be 10 rows, but you get different results when you omit my +1 fix) then remove the +1 and things should be OK Quote Link to comment https://forums.phpfreaks.com/topic/72863-solved-display-search-messy/#findComment-367494 Share on other sites More sharing options...
kkekec Posted October 12, 2007 Author Share Posted October 12, 2007 Oh man... dunno how to say thanks. You are really smart guy One HUGE thank you! If you need some graphic to do, maybe.. let me know.. I'll be glad to help you. Quote Link to comment https://forums.phpfreaks.com/topic/72863-solved-display-search-messy/#findComment-367505 Share on other sites More sharing options...
littledragon Posted October 12, 2007 Share Posted October 12, 2007 :) no probs, nice to help Don't forget to hit the 'topic solved' button Quote Link to comment https://forums.phpfreaks.com/topic/72863-solved-display-search-messy/#findComment-367509 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.