Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/72863-solved-display-search-messy/
Share on other sites

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

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...

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

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.