Thursday, January 6, 2011

Directory Complete

I did it!  Yesterday in-between Bio42 lecture and Physics 23 discussion section I went to the Falconer library and finished that pagination code.  Actually, early that morning I had written the majority of it, having thought of the idea just as I was falling asleep.


The code is a bit static compared to Silvanon's work.  The number of results per page is set to 30, and users are not able to sort their results according to ID, breed, gender, name, etc.  It makes use of a dropdown box, in which there is a list of result pages generated by this:
$page = 0;
<select name=Start>";
while ($page < $numpages) {
  $pagevalue = $page * 30;
  $pagename = $page + 1;
  echo "<option value=$pagevalue>Page $pagename</option>";
  $page++;
}
echo "</select>";
This loop function calls for a 'counting out' of pages, starting from 0, and producing numbers 1, 2, 3, 4, etc, until the value of $numpages is reach.  $numpages is defined by the number of results returned for that query, divided by 30 and rounded up:
$resultcount = mysql_query($querycount) or die(mysql_error());
$numcount = mysql_num_rows($resultcount);
$numpages = ceil($numcount/30);
to give us the number of pages needed to show all results of that query, given that there are 30 results per page.  $querycount is a query without LIMIT values, and we will end up doing two query tags, one for the purposes of counting results and one for showing results per page.  The second query looks like this:
if ($nqueryname) { //we are selecting any arbitrary apperance of the new $n variables
    $query = "SELECT * FROM critter WHERE `id` IS NOT NULL AND $nqueryname AND $nqueryowner AND $nquerygender AND $nquerybreed AND $nquerycolorist AND $nquerytype LIMIT $Start, 30";
}
else {
    $query = "SELECT * FROM critter WHERE `id` IS NOT NULL AND $queryname AND $queryowner AND $querygender AND $querybreed AND $querycolorist AND $querytype LIMIT $Start, 30";
}
wherein you'll see the variable $Start being used to define which result number to start with, and 30 signifies that all results were broken down into sets of 30.  $Start would therefore take values of 0, 30, 60, etc. and is defined in a _GET method by our drop down menu (above) by "$pagevalue = $page * 30;".  That drop down menu has action leading to the same page as it originated, so that the result page updates itself in generating different result pages.  It simply does a different query each time, refreshing to update the $Start value in LIMIT.

The old search values, such as $querybreed and $queryname ("if ( $name == '' ) { $queryname = "`name` IS NOT NULL"; } else { $queryname = "`name` LIKE '%$name%'"; }" defined to fit neatly into $query and accounts for situations where that field was left blank), must be carried over to the new refreshed page, and so their values are hidden in the drop down menu:
if ($querycolorist) {
 echo '<input type=hidden name="nquerycolorist" value="'.$querycolorist.'">';
}
if ($nquerycolorist) {
 echo '<input type=hidden name="nquerycolorist" value="'.$nquerycolorist.'">';
}
And then,
$nquerycolorist=stripslashes($_GET['nquerycolorist']);
The first takes the original value of the search and places it into the new variable, $nquery---, to avoid confusion in the engine, and beyond that simply replaces itself.  The search query is defined with an if statement to say that if no $query--- variables exist in that moment, it should use $nquery--- variables, which would have replaced the first.

$nquery--- variables must also be carried onwards through hidden inputs in the drop down menu:
if ($nqueryurl) {
 echo '<input type=hidden name="nqueryurl value="'.$nqueryurl.'">';
}
Also, one more thing.  When defining the colorist search options I wanted to be able to include "Guests", but wanted the search results to show up their actual names.  So the $colorist variable had to be defined as such:
if ( $colorist == '' ) {
   $querycolorist = "`colorist` IS NOT NULL";
}
else {
   $querycolorist = "`colorist` LIKE '$colorist'";
}

if ( $colorist == 'Guest' ) {
   $querycolorist = "`colorist` LIKE
 'Angel_of_Music' OR `colorist` LIKE
 'Animorph' //and so on;}
which would require manual addition of Guest colorist names whenever they come up.  Silvanon solved this similar problem by creating another MySQL table and adding to that, but I didn't want to deal with multiple MySQL tables.  One is difficult enough to keep track of!  What I might do further, only to refine the search, is to add arrow buttons directing viewers to the next or previous page.  I'm guessing this makes use of the current value of $Start to generate the next or previous multiple of 30.

Check out the working search engine at the main Felishorn website: www.felishorns.com.  It should always be under the Search link on the left hand side menu.

No comments:

Post a Comment