array_slice called twice is returning NULL instead of result subset

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Hi 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 (
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 :()

// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 0;
} else {

// Set the search string from the post of the index.html page
$searchstr = trim($_POST['keyword']);

if ((!empty($searchstr))) //($VISITED == FALSE)
    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),

   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

// Print out html template
 * This should be an include to keep
 * the html from the source code

echo "<title>some site -- searching for $searchstr</title><style><!--
.bb{border-bottom:1px solid #36c}
.bt{border-top:1px solid #36c}
.n a
.n .i
.b a
.tas{padding:3px 3px 3px 5px}
.taf{padding:3px 3px 6px 5px}
.tam{padding:6px 3px 6px 5px}
.tal{padding:6px 3px 3px 5px}
.e{margin:.75em 0}
.g{margin:1em 0}

//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'];
    $num_records = getArraySize($resultSet);

    // Calculate the number of pages.
    if ($num_records > $display)
    { // More than 1 page.
        $num_pages = ceil ($num_records/$display);
        $num_pages = 1;

// End of np IF.

// Determine where in the database to start returning results.
if (isset($_GET['s']))
    $start = $_GET['s'];
    $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));

// 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.
    if ($argv < 50)
        for ( $i = 0; $i < $argv2; $i++)
            $counter = $counter + 50;


// Print out the results template
// not included

echo "<br>";

if (getArraySize($resultSet) == 0)
    echo("Nothing for search string: ".$searchstr);

//$data = getData($resultSet,$start,$display);
if($resultSet == NULL)
    echo("<b>ERROR:</b> The resultset data returned from getData was NULL
which means the array_split " .

//    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
//    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("<br>");
        echo("<a href=".eregi_replace($pattern,$replacement,

// Make the links to other pages, if necessary.
if ($num_pages > 1) {
    echo '<center>';
    echo '<br /><p>';
    // 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 '<a href="search.php?s=' . ($start - $display) . '&np=' .
$num_pages . '&sort=' . $sort .'">Previous</a> ';

    // Make all the numbered pages.
    for ($i = 1; $i <= $num_pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="search.php?s=' . (($display * ($i - 1))) . '&np=' .
$num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
        } else {
            echo $i . ' ';

    // If it's not the last page, make a Next button.
    if ($current_page != $num_pages) {
        echo '<a href="search.php?s=' . ($start + $display) . '&np=' .
$num_pages . '&sort=' . $sort .'">Next</a>';

    //echo '</p><p><a href="index_all.php">See all</a></p>';
    echo '</center>';
} //end of links


Site Timeline