8 Replies - 7511 Views - Last Post: 09 September 2010 - 08:26 PM Rate Topic: -----

#1 CodeVillain   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Big Request: Search Engine + Pagination help

Posted 05 September 2010 - 03:44 PM

Alright, so I've taken tutorials and learned (more or less) how to create both pagination and a basic search engine for my site. But I cannot for the life of me figure out how to change the search engine to display the pagination. I'm currently just using the pagination to display my posts, at least I will be once I start making posts.

I've tried tinkering with it but it's been primarily me staring at the screen realizing I have no idea how the hell to do it.

Here's the search engine code:

<?php 
 include("body-parts/connect.php");

 if (strlen ($search) <=2) 
 echo "Search term too short.";
 else
 {
 echo "You searched for $search <hr>";
 
$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)
		{
$x++;
if ($x==1)
	$construct .= "keywords LIKE '%$search_each%' ";
else
	$construct .= "OR keywords LIKE '%$search_each%' ";
		}
	$construct = "SELECT * FROM posts WHERE $construct";
	$run = mysql_query($construct);
	
	$foundnum = mysql_num_rows($run);
	
	if ($foundnum==0)
		echo "No results found.";
		else
	{
echo $foundnum . "results found!<p>";

while ($runrows = mysql_fetch_assoc($run))
{
$code = $runrows['code'];

echo "$code";
}
}
}
?>


and here's the pagination code

<?php
$conn = mysql_connect('*****','*****','*****') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('*****',$conn) or trigger_error("SQL", E_USER_ERROR);

$sql = "SELECT COUNT(*) FROM posts";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);

if (isset($_GET['page']) && is_numeric($_GET['page'])) {
   $page = (int) $_GET['page'];
} else {
   $page = 1;
}

if ($page > $totalpages) {
   $page = $totalpages;
} 
if ($page < 1) {
   $page = 1;
}

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

$sql = "SELECT * FROM posts LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

while ($list = mysql_fetch_assoc($result)) {
   echo $list['code'] . "<br />";
}

echo "<div style='height:30px; margin-top:10px; margin-left:10px;'>";
$range = 3;

if ($page > 1) {
   echo " <a href='{$_SERVER['PHP_SELF']}?page=1'><span class='pagi'>first</span></a> ";
   $prevpage = $page - 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'><span class='pagi'>&laquo; prev</span></a> ";
}

for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
   if (($x > 0) && ($x <= $totalpages)) {
      if ($x == $page) {
         echo " <span class='spagi'>$x</span> ";
      } else {
         echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'><span class='pagi'>$x</span></a> ";
      }
   }
} 
                     
if ($page != $totalpages) {
   $nextpage = $page + 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'><span class='pagi'>next &raquo;</span></a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$totalpages'><span class='pagi'>last</span></a> ";
}
	echo "</div>";
?>


However the task of trying to combine these two is over my head. If anyone's willing to aid in this I'd be very appreciative. This is something I plan to use very often in the future so I definitely want to learn how to do it.

Is This A Good Question/Topic? 0
  • +

Replies To: Big Request: Search Engine + Pagination help

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5554
  • View blog
  • Posts: 14,560
  • Joined: 18-April 07

Re: Big Request: Search Engine + Pagination help

Posted 05 September 2010 - 04:33 PM

In your pagination code sample there you make a query to get a count of your posts and then use the LIMIT clause to pull in groups of records. The difference now is that you are going to query based on the search criteria, like you do in the search engine code, and build an array of results (instead of just printing them). Then, instead of using a LIMIT clause to query the database, you are going to pull a certain number of records off of the array. See the difference here?

Instead of querying directly from the database, you are instead using an array of search results as your data source. Lets say you typed in a search term, queried the database that returned 100 records. Using your while loop, you move through each result row and add it to an array. The next step is getting the page number specified by the user (through the URL most likely) and lets say they specified page 2. If you have 10 results per page, page 2 is going to be results 11 - 20. You will loop through the array and show only elements 10 - 19 (because arrays start at zero) from the array and then stop.

The array is your source of the rows, not a database query. :)

This post has been edited by Martyr2: 05 September 2010 - 04:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Big Request: Search Engine + Pagination help

Posted 05 September 2010 - 05:15 PM

Not sure why I keep having to do this...it should be pretty obvious by now, but here's a tutorial.
Was This Post Helpful? 0
  • +
  • -

#4 CodeVillain   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: Big Request: Search Engine + Pagination help

Posted 05 September 2010 - 05:41 PM

View PostJackOfAllTrades, on 05 September 2010 - 03:15 PM, said:

Not sure why I keep having to do this...it should be pretty obvious by now, but here's a tutorial.


Now correct me if I'm wrong but that tutorial doesn't show me how to create a search engine, therefore it does not show me how to do what exactly it is I'm trying to do. I'm still getting used to PHP and MySQL, I only started web development around May of 2009, before that I had never worked with code outside of bulletin boards. In short, I'm not very good and get easily confused and sometimes require help with direct explanations to my problem.

So linking me a tutorial expressing a hint of annoyance with me for having not figured it out isn't helping me.
Was This Post Helpful? 0
  • +
  • -

#5 CodeVillain   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: Big Request: Search Engine + Pagination help

Posted 05 September 2010 - 05:46 PM

View PostMartyr2, on 05 September 2010 - 02:33 PM, said:

In your pagination code sample there you make a query to get a count of your posts and then use the LIMIT clause to pull in groups of records. The difference now is that you are going to query based on the search criteria, like you do in the search engine code, and build an array of results (instead of just printing them). Then, instead of using a LIMIT clause to query the database, you are going to pull a certain number of records off of the array. See the difference here?

Instead of querying directly from the database, you are instead using an array of search results as your data source. Lets say you typed in a search term, queried the database that returned 100 records. Using your while loop, you move through each result row and add it to an array. The next step is getting the page number specified by the user (through the URL most likely) and lets say they specified page 2. If you have 10 results per page, page 2 is going to be results 11 - 20. You will loop through the array and show only elements 10 - 19 (because arrays start at zero) from the array and then stop.

The array is your source of the rows, not a database query. :)


Thank you very much this should be enough for me to figure it out on my own.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Big Request: Search Engine + Pagination help

Posted 06 September 2010 - 06:29 AM

You can't expect to find a tutorial that exactly matches your needs. Taking the lead from code and tutorials that does NEARLY what you want it to do, and modifying it to do what YOU need it to do, is an integral part of the learning process and a VERY important part of being a software developer. Sorry if that offends you.
Was This Post Helpful? 0
  • +
  • -

#7 CodeVillain   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: Big Request: Search Engine + Pagination help

Posted 06 September 2010 - 05:25 PM

View PostJackOfAllTrades, on 06 September 2010 - 04:29 AM, said:

You can't expect to find a tutorial that exactly matches your needs. Taking the lead from code and tutorials that does NEARLY what you want it to do, and modifying it to do what YOU need it to do, is an integral part of the learning process and a VERY important part of being a software developer. Sorry if that offends you.


It doesn't offend me, I was having difficulty figuring it out on my own so I asked. If you expected people to always be able to figure out solutions based on tutorials then this website would not exist, or at least not be nearly as successful as it is. I tried to do it myself, I could not figure it out, I asked for help. You linking me back to a tutorial I already read telling me I need to just figure it out myself while expressing annoyance was not helpful or appreciated.

I come here for help when I need it and to try to offer help in the rare situations I can. I don't come here to be belittled for asking a question when I can't figure something out on my own. It's not like I'm trying to start some sort of tension with you, you've been very helpful to me on several different occasions and I'm sure there'll be many situations in the future where I could use your help again. I just didn't appreciate your response in this particular situation.

This post has been edited by CodeVillain: 06 September 2010 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 RPGonzo   User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Big Request: Search Engine + Pagination help

Posted 08 September 2010 - 09:04 AM

I'm not usually one to just give people code but this would still need a lot of tweaking to work for you.

I tend to do my pagination with a function and pass the query with reference so that the function modifies it as needed.

Some may or may not agree with my method but hey it works for me.

The attached script is actually a test I did sometime back to get my function to do what i wanted it to do along side a search script. Maybe it will help you as well.I did put a few comments in there but most of it should be able to be reverse engineered.

Again this was a testing script for me ... so alot of this could probably be compacted, cleaned up or even deleted.

Attached File(s)


This post has been edited by RPGonzo: 08 September 2010 - 09:05 AM

Was This Post Helpful? 1
  • +
  • -

#9 CodeVillain   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: Big Request: Search Engine + Pagination help

Posted 09 September 2010 - 08:26 PM

View PostRPGonzo, on 08 September 2010 - 07:04 AM, said:

I'm not usually one to just give people code but this would still need a lot of tweaking to work for you.

I tend to do my pagination with a function and pass the query with reference so that the function modifies it as needed.

Some may or may not agree with my method but hey it works for me.

The attached script is actually a test I did sometime back to get my function to do what i wanted it to do along side a search script. Maybe it will help you as well.I did put a few comments in there but most of it should be able to be reverse engineered.

Again this was a testing script for me ... so alot of this could probably be compacted, cleaned up or even deleted.


Thank you, I actually ended up changing my pagination script to use arrays instead of directly taking it and displaying it from my database which allowed me to figure out how integrate it into my search engine relatively easily. However it can certainly be improved on and I definitely need to get better at using and building pagination in general so I appreciate you giving me this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1