Pagination from a directory...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bindslind
    New Member
    • Oct 2006
    • 5

    #1

    Pagination from a directory...

    Hello. Pretty new to PHP(and any sort of programming), I've been trying to modify this script that was originally written to display mysql data so it will paginate images from a directory. The code below is simplified to just show the filenames so I could see the structure more clearly. I can get the initial page "index4.php " to show up with the first 8 filenames, but when I attempt to proceed by clicking one of the page links I get this error:

    Warning: array_slice() [function.array-slice]: The first argument should be an array in C:\Documents and Settings\Owner\ My Documents\Websi tes\samcalef\te st\images4.php on line 44

    Although the page links do proceed accordingly, I don't get get any content. [HTML]Line 44 is in quotes[/HTML]

    Here's the code and thanks in advance for any help:


    <?
    // Test to see if $_GET gets passed:
    echo '<pre>'; print_r($_GET); echo '</pre>';

    // Number of records to show per page:
    $display = 8;

    // Determine how many pages there are:
    if (isset($_GET['np'])) { // Already been determined.

    $num_pages = $_GET['np'];

    } else { // Need to determine.

    // Open the directory:
    $handle = opendir('upload s/');
    while (false !== ($file = readdir($handle ))) {
    $files[] = $file;
    }

    $num_records = count($files); // Get number of files in directory.


    closedir($handl e);// Close the directory.


    // 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 else.

    // Recieve file #.
    if (isset($_GET['s'])) {
    $start = $_GET['s'];

    } else {
    $start = 0;
    }

    $list = array_slice($fi les, $start, $display);

    print_r($list);

    if ($num_pages > 1) {

    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="images4.p hp?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
    }

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

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

    echo '</p>';

    }


    ?>
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Read the Postiong Guidelines before you post any code in your thread!!
    Especially the part about enclosing code within code, php or htl tags!! Who will try to underatnd this mess?

    Ronald :cool:

    Comment

    • bindslind
      New Member
      • Oct 2006
      • 5

      #3
      Sorry. I suck at times. Here's the revised version:


      Hello. Pretty new to PHP(and any sort of programming), I've been trying to modify this script that was originally written to display mysql data so it will paginate images from a directory. The code below is simplified to just show the filenames so I could see the structure more clearly. I can get the initial page "index4.php " to show up with the first 8 filenames, but when I attempt to proceed by clicking one of the page links I get this error:

      Warning: array_slice() [function.array-slice]: The first argument should be an array in C:\Documents and Settings\Owner\ My Documents\Websi tes\samcalef\te st\images4.php on line 44

      Although the page links do proceed accordingly, I don't get get any content.

      Here's the code and thanks in advance for any help:


      [PHP]<?
      // Test to see if $_GET gets passed:
      echo '<pre>'; print_r($_GET); echo '</pre>';

      // Number of records to show per page:
      $display = 8;

      // Determine how many pages there are:
      if (isset($_GET['np'])) { // Already been determined.

      $num_pages = $_GET['np'];

      } else { // Need to determine.

      // Open the directory:
      $handle = opendir('upload s/');
      while (false !== ($file = readdir($handle ))) {
      $files[] = $file;
      }

      $num_records = count($files); // Get number of files in directory.


      closedir($handl e);// Close the directory.


      // 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 else.

      // Recieve file #.
      if (isset($_GET['s'])) {
      $start = $_GET['s'];

      } else {
      $start = 0;
      }

      $list = array_slice($fi les, $start, $display);

      print_r($list);

      if ($num_pages > 1) {

      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="images4.p hp?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
      }

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

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

      echo '</p>';

      }


      ?>[/PHP]

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        When calling your script again, you are re-loading the page. So $files (and other variables) are not set! Save these variables in the $_SESSION array, then you can pick them up again after the reload.

        Ronald :cool:

        Comment

        • bindslind
          New Member
          • Oct 2006
          • 5

          #5
          Originally posted by ronverdonk
          When calling your script again, you are re-loading the page. So $files (and other variables) are not set! Save these variables in the $_SESSION array, then you can pick them up again after the reload.

          Ronald :cool:

          Thanks a lot. I actually figured it out by having the script reread the directory before the line 44.

          [PHP]// Open the directory:
          $handle = opendir('upload s/');

          while (false !== ($file = readdir($handle ))) {
          $files[] = $file;
          }

          $list = array_slice($fi les, $start, $display);


          print_r($list);[/PHP]

          I guess I could use $_SESSION to simplify the script, but I'm not exactly sure how to do that. Now I'm actaully in the process of trying to get the images to show instead of just the filenames. Here's the new code that's not working:

          [PHP]<?
          require_once ('functions.php '); // Connect to the db.

          // Number of records to show per page:
          $display = 8;

          // Determine how many pages there are:
          if (isset($_GET['np'])) { // Already been determined.

          $num_pages = $_GET['np'];

          } else { // Need to determine.

          // Open the directory:
          $handle = opendir('upload s/');
          while (false !== ($file = readdir($handle ))) {
          $files[] = $file;
          }

          $num_records = count($files); // Get number of files in directory.


          closedir($handl e);// Close the directory.


          // 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 else.

          // Recieve file #.
          if (isset($_GET['s'])) {
          $start = $_GET['s'];

          } else {
          $start = 0;
          }

          $dir = 'uploads'; // Define the directory to view.

          // Open the directory:
          $dp = opendir($dir);

          while (false !== ($file = readdir($dp))) {
          $files[] = $file;
          }

          $images = array_slice($fi les, $start, $display);

          // Somehow split up the array into parts.

          if ( (is_file ("$dir/$image")) && (substr($image, 10 != '.')) ) {

          // Print the information.
          echo "<img src=\"$dir/$image\"/>";

          } // End of the IF.


          if ($num_pages > 1) {

          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="images5.p hp?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
          }

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

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

          echo '</p>';

          }


          ?>[/PHP]

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            You have this part of code [php]// Print the information.
            echo "<img src=\"$dir/$image\"/>"; [/php]
            but where do you assign the file to the variable $image? Not in the script you show.

            About the session array. You can start your script with the session_start() ; command. Then you have an array $_SESSION available where you can store any variables that you want to keep during the session. Assigning a variable to the array is e.g.
            $_SESSION['mydata'] = $MyVar;
            next time you can assign it back or just leave it in the $_SESSION array.

            Ronald :cool:

            Comment

            • bindslind
              New Member
              • Oct 2006
              • 5

              #7
              Originally posted by ronverdonk
              You have this part of code [php]// Print the information.
              echo "<img src=\"$dir/$image\"/>"; [/php]
              but where do you assign the file to the variable $image? Not in the script you show.

              About the session array. You can start your script with the session_start() ; command. Then you have an array $_SESSION available where you can store any variables that you want to keep during the session. Assigning a variable to the array is e.g.
              $_SESSION['mydata'] = $MyVar;
              next time you can assign it back or just leave it in the $_SESSION array.

              Ronald :cool:
              I got the images showing now and I'm just tweaking it. Thanks for your help.

              Comment

              Working...