Have an account? Sign in
Login  Register  Facebook
PHP Pagination problem
I have a problem with the pagination class that I have downloaded from here.
When I run this code it gives me more pages than I need as it is grouping all the files in the database as one but only displaying the files per my sql query.
i.e if i have in totla 22 records in my database it gives me links to pages with no data on them. 1,2,3,4,5,6 etc
Where only page 1,2 and 3 will only have records on them.

My php code is as below

require_once('includes/database.php');
require_once('includes/functions.php');
require_once ('pagination.php');
page_protect();
// Page variables
	$pageTitle = "Admin";  
	$currentPage = "upload"; 
	
$client_ID = mysql_query("SELECT id
	FROM clients WHERE user_name='".$_SESSION['user_name']."'")or die(mysql_error());
$client_ID = mysql_fetch_array($client_ID);
$client_ID = $client_ID['id'];

		if(isset($_POST['upload'])) {
		$required = array('userfile');
		$missing = array();
		$validation = array();
		$userfile = array('image/gif','image/pjpeg');
		
		foreach($_POST as $key => $value) {
			$value = trim($value);
			if(empty($value) && in_array($key,$required)) {
				array_push($missing,$key);
			} 
		
		}
		
		if($_FILES['userfile']['error'] != UPLOAD_ERR_OK) {
			$validation['image_error'] = file_upload_error_message($_FILES['userfile']['error']);
				array_push($missing, 'image_error');
		} else {
			if($_FILES['userfile']['size'] > MAX_UPLOAD_FILE_SIZE) {
				$validation['image_size'] = "The file exceeds ".convert_size(MAX_UPLOAD_FILE_SIZE);
					array_push($missing, 'image_size');
			} elseif(!in_array($_FILES['userfile']['type'],$userfile)) {
					$validation['image_type'] = "This file types is not allowed";
					array_push($missing, 'image_type');
			}
		}
		
	if(!empty($missing)) {
		$before = " <span class="warn">";
		$after = "</span>";
		foreach($validation as $key => $value) {
			if(in_array($key,$missing)) {
				${"valid_".$key} = $before.$value.$after;
				}
			}
	} else {
		$uploadDir = 'uploads/';
		$fileName = $_FILES['userfile']['name'];
		$tmpName = $_FILES['userfile']['tmp_name'];
		$fileSize = $_FILES['userfile']['size'];
		$fileType = $_FILES['userfile']['type'];

		$filePath = $uploadDir . $fileName;

	$result = move_uploaded_file($tmpName, $filePath);
	
	if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
} 

$date = date('Y-m-d');
$time = date('H:i:s');

$sql = "INSERT INTO upload2 (name, client, size, type, path, date, time ) ".
"VALUES ('$fileName', '$client_ID', '$fileSize', '$fileType', '$filePath', '$date', '$time')";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());

if($result) {
		$confirmation = "<p class="conf">The $fileName file/image has been uploaded succesfully.</p>";
	} else {
		$confirmation = "<p class="warn">The $fileName file/image could not be uploaded.</p>";
		}

	}
	
}


<?php include_once('includes/header.php'); ?>
<!-- end heading -->
            
<?php include_once('includes/end-header.php'); ?><!-- end breadcrumbs -->
<div id="breadcrumbs"><!-- begin breadcrumbs -->   
<div class="container_16">

    <ul class="crumbs">
    	<li><a href="index.php">Home</a></li> 
        <li>Register</li>
    </ul>       
    
</div>
</div><!-- end breadcrumbs -->
<div id="center"><!-- begin center-->
<div class="container_16"><!-- begin wrap -->

      <div class="block">
<div class="block_head">
					<div class="bheadl"></div>
					<div class="bheadr"></div>
					
					<h2>Client Upload Area</h2>
                    <p class="session"><?php echo $_SESSION['user_name'];?></p>
                    <ul>
						<li><a href="#">Create New User</a></li>
                        <li><a href="#">Add page</a></li>
                        <li><a href="#">Logout</a></li>
					</ul>

   		</div>		<!-- .block_head ends -->

		<div class="block_content">
      <p>
<?php
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
	$page = ($page == 0 ? 1 : $page);
	$perpage = 20;//limit in each page
	$startpoint = ($page * $perpage) - $perpage;
	
$sql = "SELECT * FROM upload2 WHERE client='".$client_ID."' ORDER BY date DESC, time DESC LIMIT $startpoint,$perpage";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
$rows = mysql_fetch_assoc($result);
$total_rows = mysql_num_rows($result);
?>

       
       <h6>Welcome <font color="#42b7d4"><?php echo $_SESSION['user_name'];?></font></h6>
		<p> </p>
		<p>
		  <h7>please choose a file/image to upload.</h7> 
		  
		  <?php //show pages
	echo Pages("upload2",$perpage,"upload1.php?"); ?>
		  </p>
		<form action="" method="post" class="niceform" enctype="multipart/form-data">
		  
		  <fieldset>
		    <?php echo isset($confirmation) ? $confirmation : NULL; ?>
		    <?php
			echo isset($valid_image) ? $valid_image : NULL;
			echo isset($valid_image_error) ? $valid_image_error : NULL;
			echo isset($valid_image_size) ? $valid_image_size : NULL;
			echo isset($valid_image_type) ? $valid_image_type : NULL;
		?>
		    <p>  </p>
		    <dl>
		      <dt><label for="upload">Upload a File:</label></dt>
		      <dd><input name="userfile" type="file" name="upload" id="upload" /></dd>
	        </dl>
		    <dl>
		      <input name="upload" type="submit" id="uploadsubmit" value="Upload" />
	        </dl>
	      </fieldset>
		  
</form>
      <p>  </p>

<?php if($total_rows > 0) { ?>
          <table border="0" cellpadding="0" cellspacing="0" id="tbl_repeat">
        <tr>
          <th scope="col">File/Image Name</th>
          <th scope="col" style="width:10%">Date</th>
          <th scope="col" style="width:5%">Time</th>
          <th scope="col" style="width:10%">Size</th>
          <th scope="col" style="width:10%">Download</th>
          <th scope="col" style="width:5%">Delete</th>
        </tr>
        <?php do { ?>
        <tr>
          <td><?php echo $rows['name']; ?></td>
          <td><?php echo break_date($rows['date']); ?></td>
          <td><?php echo $rows['time']; ?></td>
          <td><?php echo convert_size($rows['size']); ?></td>
          <td><a href="downloads.php?id=<?php echo $rows['id']; ?>">Download</a></td>
          <td><a href="pages_remove_confirm.php?id=<?php echo $rows['id']; ?>">Delete</a></td>
        </tr>
        <?php } while($rows = mysql_fetch_assoc($result)); ?>
      </table> 
      
      <?php } else { echo "<p class="warn">Sorry there are no records available.</p>"; } ?>
      
      
		</div>		
				<!-- .block_content ends -->
				
				<div class="bendl"></div>
				<div class="bendr"></div>
                
      </div>	
      <!-- .block ends -->
      
      
      
      <br />
      
  </div><!-- end content --><!-- end portofolioSlider -->
  
  </div><!-- end wrap -->
  
</div><!-- end center-->

<?php require('includes/footer.php'); 



Does anyone know how to limit the records with the pagination with the client_id as the total num rows rather than all of the num rows??

Thank you
Started: September 21, 2011 Latest Activity: September 21, 2011 php mysql phpmysql
1 Answer
its easy just replace:
echo Pages(\"upload2\",$perpage,\"upload1.php?\");
by
echo Pages(\"upload2 WHERE client=\'\".$client_ID.\"\'\",$perpage,\"upload1.php?size=$perpage&\");
and about the limit use this code
<div class=\"pagination\">   
<?php

foreach (array(10,15,30,50) as $size) {

    if ($_GET[\'size\'] <> $size) {
        print \"<a href=\'?size=$size\";
        print (isset($_GET[\"page\"])) ? \'&page=\'.$_GET[\"page\"] : \'\';
        print \"\'>$size</a>\";
    }else{
        print \"<span class=\'current\'>$size</span>\";
    }
    
}
    
?>
<span class=\"disabled\">Per page</span></div>
and replace
$perpage = 20;//limit in each page
to
$perpage = (isset($_GET[\'size\'])) ? $_GET[\'size\'] : 20;//limit in each page

and you must use this ?size=10&page=1

Posted: MacOS
In: September 21, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer