Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
pagination without database...pls help
Old 03-01-2009, 09:51 PM pagination without database...pls help
Novice Talker

Posts: 14
Name: paparanch
Trades: 0
hello gurus!

i have a folder name comment in my root directory...and inside that folder are images or gif....now what i wanted, is to display those images with pagination...let say 4 images per page...

and here's what i got so far...the problem is that in my main page it display 3 images as what i declared in the $rpp variable...but when i press the link 1 (view_pix.php?page=1) it will now display 4 images instead of only 3, the same with the following pages...and i also noticed that the last image in every pages is still present on its next page...and its placed first in the row...

i really don't know how to fix this one...i really need ur help...
any help is very appreciated....tnx in advanced!

PHP Code:

<?php

        $page 
$_GET['page'];
        
$rpp 3;
        
$c 0;
        
$handle opendir('comments/');
        
$d opendir("comments/");
        
$count 0;
        
        while((
$f readdir($d)) !== false)
        if(
ereg('.gif$'$f))
        ++
$count;
        
closedir($d);
        
        print 
"$count";        
        
        
$numOfPages ceil$count $rpp );
        
        if(
$handle) {

            while(
false !== ($file readdir($handle))) {

                if(
preg_match("/\w+(.gif)/",$file)) {

                   
$c++;
                   if (
$c > (($page $rpp) + $rpp)) break;
                   if (
$c >= ($page $rpp)) {
                          echo 
"<div class='update_pics_frame'>";
                          echo 
"<div class='update_pics'>";
                              echo 
"<a href='comments/$file'><img src='comments/$file' width='200'border='0'></a>";
                          echo 
"</div>";
                          echo 
"</div>";
                          
                   }
                }
            }
        
            
closedir($handle);
        }


        
        for (
$i 1$i <= $numOfPages$i++) {
          
            echo 
"<a href='view_pix.php?page=$i'>";
                echo 
"&nbsp;".$i."&nbsp;";
            echo 
"</a>";
            
        }

?>

Last edited by paparanch; 03-01-2009 at 09:53 PM.. Reason: wrong spelling
paparanch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-02-2009, 01:31 PM Re: pagination without database...pls help
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Try this - I think its easier to include all the images into an array and work from there.

PHP Code:
<?php
  
  
// Define defaults (these can alternativly be standard vars instead of constants):
  
define('DIR_IMAGE_PATH''comments/');
  
define('PAGINATION_DISPLAY'3);
  
  
  
// Process:
  
$image_array = array();
  
  
// Load images into an array (case insensitive)
  
if ($dir = @dir(DIR_IMAGE_PATH))
  {
    while (
$file $dir->read())
    {
      if (
preg_match('/\.gif$/i'$file)) $image_array[] = DIR_IMAGE_PATH $file;
    }
  }
  
  if (
count($image_array) > 0)
  {
    
// There are images to display
    
$total_images count($image_array);
    
    
// Get max page count:
    
$total_pages ceil($total_images PAGINATION_DISPLAY);
    
    
// Get current page:
    
$page intval(@$_GET['page']);
    if (
$page OR $page $total_pages$page 1;
    
    
// Get starting range:
    
$range_start = ($page <= 1) ? : ($page 1) * PAGINATION_DISPLAY;
    
    
// Display images:
    
for ($i $range_start$i $total_images AND $i $range_start PAGINATION_DISPLAY$i++)
    {
      echo 
'<div class="update_pics_frame"><div class="update_pics">';
      echo 
'<a href="' $image_array[$i] . '"><img src="' $image_array[$i] . '" width="200" border="0" /></a>';
      echo 
'</div></div>' "\n";
    }
    
    
// Display page links:
    
for ($i 1$i <= $total_pages$i++)
    {
      echo 
'<a href="' $_SERVER['PHP_SELF'] . '?page=' $i '">&nbsp;' $i '&nbsp;</a>';
    }
  }
  else
  {
    echo 
'<strong>There are no images to display</strong>';
  }
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 03-02-2009 at 01:43 PM..
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-02-2009, 07:54 PM Re: pagination without database...pls help
Novice Talker

Posts: 14
Name: paparanch
Trades: 0
wow! tnx man! it works!

but now, i wanted to dispaly the latest added image at the top or the first image to display...how am i going to do this?
paparanch is offline
Reply With Quote
View Public Profile
 
Old 03-03-2009, 09:02 AM Re: pagination without database...pls help
Defies a Status

Posts: 1,606
Trades: 0
Sort the array by system date. I can't write but that is what needs to be done.
__________________
Colbyt

Please login or register to view this content. Registration is FREE
colbyt is offline
Reply With Quote
View Public Profile
 
Old 03-03-2009, 09:49 PM Re: pagination without database...pls help
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
PHP Code:
<?php
  
  
// Define defaults (these can alternativly be standard vars instead of constants):
  
define('DIR_IMAGE_PATH''comments/');
  
define('PAGINATION_DISPLAY'3);
  
  
// size = file size, atime = access time, mtime = modification time, ctime = inode change time
  
define('PAGINATION_DISPLAY_ORDER''ctime');
  
  
// 1 = acending order, -1 = decending order
  
define('PAGINATION_ACENDING_ORDER'1);
  
  
  
// Process:
  
$image_array = array();
  
  
// Load images into an array (case insensitive)
  
if ($dir = @dir(DIR_IMAGE_PATH))
  {
    while (
$file $dir->read())
    {
      if (
preg_match('/\.gif$/i'$file))
      {
        
$fstate = (array)@stat(DIR_IMAGE_PATH $file);
        
$fstate['file'] = DIR_IMAGE_PATH $file;
        
$image_array[] = $fstate;
      }
    }
  }
  
  
  
// Process image pagination
  
if (count($image_array) > 0)
  {
    
// Sort array
    
usort($image_array'callback_usort_file');
    
    
// There are images to display
    
$total_images count($image_array);
    
    
// Get max page count:
    
$total_pages ceil($total_images PAGINATION_DISPLAY);
    
    
// Get current page:
    
$page intval(@$_GET['page']);
    if (
$page OR $page $total_pages$page 1;
    
    
// Get starting range:
    
$range_start = ($page <= 1) ? : ($page 1) * PAGINATION_DISPLAY;
    
    
// Display images:
    
for ($i $range_start$i $total_images AND $i $range_start PAGINATION_DISPLAY$i++)
    {
      echo 
'<div class="update_pics_frame"><div class="update_pics">';
      echo 
'<a href="' $image_array[$i]['file'] . '"><img src="' $image_array[$i]['file'] . '" width="200" border="0" /></a>';
      echo 
'</div></div>' "\n";
    }
    
    
// Display page links:
    
for ($i 1$i <= $total_pages$i++)
    {
      echo 
'<a href="' $_SERVER['PHP_SELF'] . '?page=' $i '">&nbsp;' $i '&nbsp;</a>';
    }
  }
  else
  {
    echo 
'<strong>There are no images to display</strong>';
  }
  
  
  
// Define user sort function
  
function callback_usort_file($a$b)
  {
    
$idx strtolower(PAGINATION_DISPLAY_ORDER);
    
    if (
$a[$idx] == $b[$idx]) return 0;
    
    return ((
PAGINATION_ACENDING_ORDER 0) ? $a[$idx] > $b[$idx] : $a[$idx] < $b[$idx]) ? -1;
  }
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 03-03-2009 at 09:51 PM..
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to pagination without database...pls help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45482 seconds with 12 queries