Tech Talk Discuss PC Hardware, Software, Internet and Other Technology

PHP Search with Pagination Question?

Old 05-18-04, 04:57 PM
  #1  
DVD Talk Platinum Edition
Thread Starter
 
Join Date: Dec 2002
Location: NJ
Posts: 3,337
PHP Search with Pagination Question?

Since I got such great help here before with my last PHP/mySQL problem, I figured I would give it another go.

I have a simple search function that just goes through the mySQL table I want it to go through. I am using a simple %search% code to scan through the table.

Now the problem comes with the results page, right now I do not have too many entries in the database so this isn't an issue, but the plan is to have a few hundred within the next month or so. So I want to limit the search results to about 15-25 results per page and have been trying to code a pagination script going by some tutorials I have found online, unfortunately these tutorials are for simple displaying of data from the table, not from a search form. So far I am not getting any luck integrating my search code with the pagination code. I have gotten the pagination stuff to display, but not limit the hits to 15 results on a page. And when I go to the 2nd page it doesn't carry over the results, it gives me an error that I put in the script if there was no search term entered into the field.

Anyone know of a simple script I can just modify to work for me? Or if there are any tutorials online that are doing just want I need them to do? This is getting really frustrating, and I am going to need the pagination for another section (alphabetical listing of terms) so I need to figure out how to get this working before I move on.

Thanks...I hope
fnordboy is offline  
Old 05-18-04, 05:18 PM
  #2  
DVD Talk Hall of Fame
 
Join Date: Jan 2000
Location: US
Posts: 9,623
Have you already looked at this one:
http://www.devshed.com/c/a/PHP/Pagin...Data-with-PHP/

dave
Dave99 is offline  
Old 05-19-04, 12:31 AM
  #3  
DVD Talk Platinum Edition
Thread Starter
 
Join Date: Dec 2002
Location: NJ
Posts: 3,337
Originally posted by Dave99
Have you already looked at this one:
http://www.devshed.com/c/a/PHP/Pagin...Data-with-PHP/

dave
I can't remember if I went through that tutorial or not so I am going to give it a go tomorrow.

Thanks.
fnordboy is offline  
Old 05-19-04, 04:21 PM
  #4  
DVD Talk Legend
 
Join Date: Oct 1999
Location: Plano, TX
Posts: 23,226
First.. set a variable for results per page, even if you know what you want, you can change this later or have it user selectable.

Now.. after your SQL query, grab the total of results, I think the command is mysql_num_rows. Now, do something like this..

Code:
if(!$page) $page=1;
for($loop=(($page-1)*$resultsperpage);$loop<(($page*$resultsperpage)+1);$loop++)
Just put your output in that for loop.

Then, you want to make them select the previous or next page do this.

Code:
if($page>1) { (link to $page-1) }
if($total>($page*$resultsperpage)) { (link to $page+1) }
$total is of course the value from mysql_num_rows.

Okay, this is kind of rought and from memory only, but if you're any kind of programmer I think you should be able to work with it. Let me know if this works, or is even what you're looking to do.
PixyJunket is offline  
Old 05-19-04, 04:22 PM
  #5  
DVD Talk Legend
 
Join Date: Oct 1999
Location: Plano, TX
Posts: 23,226
I forgot to metion.. you want to pass $page in the URL. If it's not there it assume page 1.
PixyJunket is offline  
Old 05-19-04, 04:34 PM
  #6  
DVD Talk Platinum Edition
Thread Starter
 
Join Date: Dec 2002
Location: NJ
Posts: 3,337
Wow and not a mention of anime

I knew you would post some code (I saw your pre-edited post)
fnordboy is offline  
Old 05-20-04, 12:04 AM
  #7  
DVD Talk Platinum Edition
Thread Starter
 
Join Date: Dec 2002
Location: NJ
Posts: 3,337
Ok I am stuck. I keep getting a parse error on line 73 (which I bolded below):
Code:
	 $Per_Page=10;
     mysql_connect(localhost,$username,$password);
     @mysql_select_db($database) or die( "Unable to select database");
    $srch="%".$search."%"; 
    $SQL="SELECT COUNT(*) AS Total FROM defs WHERE TITLE LIKE '$srch' || ARTICLE  LIKE '$srch' || ALT LIKE '$srch' || ALSO LIKE '$srch'"; 

    $SQL_Result=mysql_db_query($database, $SQL);
    $SQL_Result_Array=mysql_fetch_array($SQL_Result);
    $Total=$SQL_Result_Array['Total'];
    // Create a new SELECT Query with the 
      // ORDER BY clause and without the COUNT(*)
    $SQL="SELECT * FROM defs WHERE TITLE LIKE '$srch' || ARTICLE  LIKE '$srch' || ALT LIKE '$srch' || ALSO LIKE '$srch' ORDER BY ID";
    // Append a LIMIT clause to the SQL statement
    if (empty($_GET['Result_Set']))
 
      {
       $Result_Set=0;
       $SQL.=" LIMIT $Result_Set, $Per_Page";
       }else
       {
 
      $Result_Set=$_GET['Result_Set'];
       $SQL.=" LIMIT $Result_Set, $Per_Page";
 
      }
    // Run The Query With a Limit to get result
    $SQL_Result=mysql_db_query($database, $SQL);
    $SQL_Rows=mysql_num_rows($SQL_Result);
    // Display Results using a for loop
    for 
    ($a=0; $a < $SQL_Rows; $a++)
        {
        $SQL_Array=mysql_fetch_array($SQL_Result);
 
       $title=$SQL_Array['TITLE'];
        $id=$SQL_Array['ID'];
      echo "
    "; echo "
  1. ***A HREF HERE***="'definition.php?id=$id'>$title
  2. "; echo "
"; } // Create Next / Prev Links and $Result_Set Value if ($Total>0) { if ($Result_Set<$Total && $Result_Set>0) { $Res1=$Result_Set-$Per_Page; echo "***A HREF HERE***="search_test.php?Result_Set=$Res1&Keyword=$srch"> <;<; Previous Page "; } // Calculate and Display Page # Links $Pages=$Total / $Per_Page; if ($Pages>1) { for ($b=0,$c=1; $b < $Pages; $b++,$c++) { $Res1=$Per_Page * $b; echo ***A HREF HERE***="search_test.php?Result_Set=$Res1&Keyword=$srch" > $c n"; } } if ($Result_Set>=0 && $Result_Set<$Total) { $Res1=$Result_Set+$Per_Page; if ($Res1<$Total) { echo " ***A HREF HERE***="search_test.php?Result_Set=$Res1&Keyword=$srch"> Next Page >>"; } } } // Close Database Connection mysql_close($Connection); ?>
I am sure I am missing somethign SUPER obvious but I just can't see it. Help.

Note: I had to put: ***A HREF HERE***=" where the hrefs were since it was showing it as a link

Last edited by fnordboy; 05-20-04 at 12:11 AM.
fnordboy is offline  
Old 05-20-04, 07:19 AM
  #8  
Video Game Talk Reviewer
 
Canis Firebrand's Avatar
 
Join Date: Dec 1999
Location: Formerly known as "Vryce"/Detroit, Michigan
Posts: 13,857
Let me show what I'm using on my webpage. It isn't a search page, but it does use pagination and calls to my database.

You can see the page here

I think the problem might be in the way you are writing the href line.

Try writing the line like this:

Code:
echo "***A HREF HERE***=definition.php?id=' . $id . '> ' . $title . ' ";
I think its getting hung up processing your variables, I believe. the ' . $variable . ' seperates out the call to the variable so that it can be used in the url.

Do you have the page up so that people can go to it and see what error it gives, or are you still testing it and haven't uploaded it to a web host yet?

Last edited by Canis Firebrand; 05-20-04 at 07:25 AM.
Canis Firebrand is offline  
Old 05-20-04, 09:19 AM
  #9  
DVD Talk Platinum Edition
Thread Starter
 
Join Date: Dec 2002
Location: NJ
Posts: 3,337
Originally posted by Vryce
I think its getting hung up processing your variables, I believe. the ' . $variable . ' seperates out the call to the variable so that it can be used in the url.

Do you have the page up so that people can go to it and see what error it gives, or are you still testing it and haven't uploaded it to a web host yet?
Well, I tried what you said but it then started choking on line 69 which is the line with the URL.

The error it gives the way I had it is:
Parse error: parse error in /directory/structure/search_test.php on line 73

Which is the line "and $Result_Set Value". Damnit this is frustrating.
fnordboy is offline  
Old 05-20-04, 09:23 AM
  #10  
Video Game Talk Reviewer
 
Canis Firebrand's Avatar
 
Join Date: Dec 1999
Location: Formerly known as "Vryce"/Detroit, Michigan
Posts: 13,857
The line that has and $result_set value. is that supposed to be a comment?

maybe in uploading, it moved it to a new line and the php interpretor is thinking its a statement and not sure what to do with it?

Just a thought.
The code tag here looks to have formatted the script differently then its probably written in the file.
Canis Firebrand is offline  
Old 05-20-04, 10:02 AM
  #11  
DVD Talk Gold Edition
 
Join Date: Dec 1999
Posts: 2,708
Here's an alternate way to write that a href tag (I removed the asterisks an just entered the way the tag will show on the screen)

PHP Code:
echo '<a href=\"definition.php?id=' $id '\">' $title '</a>'
I don't think you really need the backslash (the escape character) as I'm using single quotes for the echo statement, but the above should parse correctly for you.


Here's the way the code you entered will need to look to parse correctly

PHP Code:
echo "<A HREF='definition.php?id=$id'>$title</a>"
You just had an extra double quote before the single quote in front of definition.php

Last edited by Lampei; 05-20-04 at 10:07 AM.
Lampei is offline  
Old 05-20-04, 10:20 AM
  #12  
DVD Talk Platinum Edition
Thread Starter
 
Join Date: Dec 2002
Location: NJ
Posts: 3,337
Originally posted by Vryce
The line that has and $result_set value. is that supposed to be a comment?

maybe in uploading, it moved it to a new line and the php interpretor is thinking its a statement and not sure what to do with it?

Just a thought.
The code tag here looks to have formatted the script differently then its probably written in the file.
Ugh. That was the problem. Im an idiot
fnordboy is offline  
Old 05-20-04, 10:25 AM
  #13  
DVD Talk Platinum Edition
Thread Starter
 
Join Date: Dec 2002
Location: NJ
Posts: 3,337
Ok I got it working perfectly now. Thanks everyone for the help. I am a php n00b.
fnordboy is offline  

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Thread Tools
Search this Thread

Archive Advertising Cookie Policy Privacy Statement Terms of Service

Copyright 2018 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.