Jump to content

How to change the browser results ( text )


Tasos

Recommended Posts

Hello, i have a question about the results in the browser where you search.

I want that this, 

Search+source+code&page=36
Search+source+code&page=72
Search+source+code&page=108

 

to change it like this, when you click next or 2 3 4 5 etc 

Search+source+code&page=2
Search+source+code&page=3
Search+source+code&page=4

 

is that possible ? here below the complete script 

<?php
    
$button = $_GET ["submit"];
$search = $_GET ["search"]; 
  
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("mysql_host_name","mysql_username","mysql_password");
mysql_select_db("database_name");
    
$search_exploded = explode (" ", $search);
    
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
    
}
  
$constructs ="SELECT * FROM database_table_name WHERE $construct";
$run = mysql_query($constructs);
    
$foundnum = mysql_num_rows($run);
    
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{ 
  
echo "$foundnum results found !<p>";
  
$per_page = 36;
$page = $_GET["page"];
$max_pages = ceil($foundnum / $per_page);
if(!$page)
$page=0; 
$getquery = mysql_query("SELECT * FROM database_table_name WHERE $construct LIMIT $page, $per_page");
  
while($runrows = mysql_fetch_assoc($getquery))
{
$title = $runrows ["title"];
$desc = $runrows ["description"];
$url = $runrows ["url"];
   
echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";
    
}
  
//Pagination pages
echo "<center>";
  
$prev = $page - $per_page;
$next = $page + $per_page;
                       
$adjacents = 3;
$last = $max_pages - 1;
  
if($max_pages > 1)
{   
//previous button
if (!($page<=0)) 
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$prev'>Prev</a> ";    
          
//pages 
if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;   
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $page){
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'>$counter</a> ";
}  
$i = $i + $per_page;                 
}
}
elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($page/$per_page) < 1 + ($adjacents * 2))        
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $page){
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'>$counter</a> ";
} 
$i = $i + $per_page;                                       
}
                          
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($page / $per_page) && ($page / $per_page) > ($adjacents * 2))
{
echo " <a href='search.php?search=$search&submit=Search+source+code&page=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$per_page'>2</a> .... ";
 
$i = $page;                 
for ($counter = ($page/$per_page)+1; $counter < ($page / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $page){
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'>$counter</a> ";
}   
$i = $i + $per_page;                
}
                                  
}
//close to end; only hide early pages
else
{
echo " <a href='search.php?search=$search&submit=Search+source+code&page=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$per_page'>2</a> .... ";
 
$i = $page;                
for ($counter = ($page / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $page){
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$i'>$counter</a> ";   
} 
$i = $i + $per_page;              
}
}
}
          
//next button
if (!($page >=$foundnum-$per_page))
echo " <a href='search.php?search=$search&submit=Search+source+code&page=$next'>Next</a> ";    
}   
echo "</center>";
} 
} 
?>

 

Thank`s in advance.

Edited by Tasos
Link to comment
Share on other sites

the value in that code that is being called a "page" is actually the starting row number, starting at zero. it's the offset value being put into the LIMIT offset, row_count statement.

 

to change the code so that the page number is actually the page number, starting at 1, the first step would be to take the submitted $_GET['page'] value and calculate the starting row offset from it -

$offset = ($page - 1) * $per_page;

the above value would be used in the LIMIT statement instead of the $page value.

 

you also need to modify the code that produces the pagination links so that it just increments the page number, starting at 1, instead of adding $per_page to it.

Edited by mac_gyver
Link to comment
Share on other sites

you also need to modify the code that produces the pagination links so that it just increments the page number, starting at 1, instead of adding $per_page to it.

My excuse for this I do not understand how to do this, and where to begin.

Link to comment
Share on other sites

I understand what you mean and I have tried to solve it but nothing hapens, i am new to php and if somebody could help me than can i finaly find a domain and put the website online.... 

 

 

Thank you very much for helping me, but i cant solve it alone. ( I'm not so far with php ) just a beginner.

 

 

I'm not so good in english, sorry for that.

Link to comment
Share on other sites

I made this script in one week, with the help of internet on this moment is my website ready to launch, so i can use it like that but i would like it if the page numbers are like i want, and if someone can help me I would be very grateful.

Link to comment
Share on other sites

mac_gyver already has helped you, in that he has told you exactly what you need to do. He even included the critical line of code. You have everything you need, but it requires understanding the code.

If you can't do that, then you're not programming. Programming is solving a logical problem, and then translating said solution to a programming language. Understanding both the problem and the solution is paramount for being able to program.

 

Now, if you don't understand what to do because of your lacking English knowledge. Then, I'm afraid, there are only three options available for you. Either use a dictionary, find someone who speaks your language (as well as Englihs) to help you translate, or a PHP forum dedicated for your language.

 

In either case: We do not solve your problems for you, seeing as that is called doing the work for you, and not helping. You have to put some effort in yourself, but if you get stuck we can offer help and advice on how to proceed correctly.

Link to comment
Share on other sites

you have mentioned your site a couple of times. that code allows sql injection in the search value and will allow the display of the contents of any of your database tables. i don't think you want to put that code onto a live site without correcting the sql injection problem in it first.

 

if you truly wrote that code, rather than just copy/paste it and modifying it to match your database connection and table, changing the pagination link code like i suggested should take you about 5-10 minutes.

Link to comment
Share on other sites

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.