array_slice called twice is returning NULL instead of result subset
am 17.09.2007 18:26:54 von Ouray VineyHi All: ';
I have been trying to hack a quick an dirty ebook search engine for
myself and my tech buddies. I do not want to use a db. I will
include the code to ensure that the "experts" can have their opinions;
please note, I am not an experienced PHP coder so please accept my
apology in advanced for how ugly the code is.
Problem: User hits my search page, he types in "hacking", this string
is passed to the search.php server side script and is passed to a
function that grabs an array of matching files (see ugly code below),
then the resulting array is printed using (array_slice to grab the
section of the array that I want; which is based on the pagination
code that I use to calculate how many pages to display). The problem
that I am having with array_slice is that it is return the correct
number of records for the first page; but for each subsequent page,
the array returned from calling array_slice (http://us2.php.net/manual/
en/function.array-slice.php) is NULL; which according to the
documentation means that nothing was found based on the variables
passed to array_slice(). I have spent a lot of time adding debug
statements and researching other people having similar problems but I
finally decided to post my problem here.
I will greatly appreciate any PHP experts taking the time to help me
get closer to the solution; which by the way is getting the
array_slice to return a specific number of elements from the array
based on the pagination code. I really just wanted it to return
example 50 records per page if 300 hits where found. So, for each
time the user pages through the results; he/she will be displayed the
next/previous 50 records from the result set.
exec('locate -i --regex /*.pdf$ --regex /*.chm$ --regex /*.rar$ --
regex /*.zip$ | grep -i '.$searchstr, $resultSet);
$_SESSION["resultset"] = $resultSet; // I want to be sure that this
variable is accessible when the result set is too large to display on
one page (ie pagination)
search.php (please excuse the ugly coding :()
session_start();
$VISITED=FALSE;
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
// Set the search string from the post of the index.html page
$searchstr = trim($_POST['keyword']);
if ((!empty($searchstr))) //($VISITED == FALSE)
{
$VISITED = TRUE;
exec('locate -i --regex /*.pdf$ --regex /*.chm$ --regex /*.rar$ --
regex /*.zip$ | grep -i '.$searchstr, $resultSet);
// Set the session variable to the resultset
$_SESSION["resultset"] = $resultSet;
// var_dump($_SESSION);
}
if ((empty($searchstr)))
{
$resultSet = $_SESSION;
// var_dump($resultSet);
}
class Timer {
var $s;
var $p = 0;
function start() {
$this->s = $this->getmicrotime();
}
function pause() {
$this->p = $this->getmicrotime();
}
function unpause() {
$this->s += ($this->getmicrotime() - $this->p);
$this->p = 0;
}
function fetch($decimalPlaces = 3) {
return round(($this->getmicrotime() - $this->s),
$decimalPlaces);
}
function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
// Create a new instance of the timer class
$t = new Timer();
global $t;
// Start the timer
$t->start();
// Print out html template
/*
* This should be an include to keep
* the html from the source code
*
*/
include("index.html");
echo "
;
//This checks to see if there is a page number. If not, it will set it
to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//This is the number of results displayed per page
$page_rows = 25;
//This tells us the page number of our last page
//We call getArraySize and pass it the resultSet
//from the locate command
$last = ceil(getArraySize($resultSet)/$page_rows);
//echo "Last: ".$last;
//this makes sure the page number isn't below one, or more than our
maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
// Number of records to show per page:
$display = 200;
// Determine how many pages there are.
if (isset($_GET['np']))
{ // Already been determined.
$num_pages = $_GET['np'];
}
else
{
$num_records = getArraySize($resultSet);
// Calculate the number of pages.
if ($num_records > $display)
{ // More than 1 page.
$num_pages = ceil ($num_records/$display);
}
else
{
$num_pages = 1;
}
}
// End of np IF.
// Determine where in the database to start returning results.
if (isset($_GET['s']))
{
$start = $_GET['s'];
}
else
{
$start = 0;
}
// Define the regex patters
$pattern = '/mnt/hdd2/Ebooks';
$pattern2 = '/mnt/hdd2/Ebooks/.*/';
$replacement = '';
// Grab the time from the timer
$totaltime = $t->fetch();
function getPages($argv)
{
// Get current location in array
$foobar = round(count($argv)/50);
//echo("Current Location: ".$foobar);
return ($foobar);
}
function getArraySize($argv)
{
// Get the size of the resultset
//$arraySize = count($resultSet);
//echo("Array size: ".count($argv));
return(count($argv));
}
// Function to get the current location in the array
function getLocation($argv, $argv2)
{
// If we get 100 as our argv then
// we will want to say; first iteration
// 50; second 100 after that we are finished.
counter;
if ($argv < 50)
{
return($argv);
}
else
{
for ( $i = 0; $i < $argv2; $i++)
{
$counter = $counter + 50;
return($counter);
}
}
}
//PAGE CONTENT
// Print out the results template
// not included
echo "
";
if (getArraySize($resultSet) == 0)
{
echo("Nothing for search string: ".$searchstr);
}
//$data = getData($resultSet,$start,$display);
if($resultSet == NULL)
{
echo("ERROR: The resultset data returned from getData was NULL
which means the array_split " .
"failed!");
}
// New hack for searching through the resulting array from base search
function array_isearch($str,$array){
foreach ($array as $k=>$v) {
if (strtolower($v) == strtolower($str)) { return $v; };
};
return false;
}
// foreach($resultSet as $outputline)
foreach($myarray = array_slice($resultSet,$start,$display, TRUE) as
$outputline)
// foreach($myarray = array_slice($resultSet,50,count($resultSet),
TRUE) as $outputline)
{
// We need to place the looping construct around this code here to
// ensure that it doesn't iterate through all of the array at once
//echo "INFO: we are in the data looping construct!";
// echo("
");
echo("".eregi_replace($searchstr,'\\0
b>',eregi_replace($pattern2,$replacement,$outputline))."
");
echo("
");
}
//}
// Make the links to other pages, if necessary.
if ($num_pages > 1) {
echo '
echo '
// Determine what page the script is on.
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo 'Previous ';
}
// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '' . $i . ' ';
} else {
echo $i . ' ';
}
}
// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo 'Next';
}
//echo '
echo '
} //end of links
?>