View Single Post
  #4 (permalink)  
Old Jan 28th, 2008, 20:32
saltedm8's Avatar
saltedm8 saltedm8 is online now
Lead Administrator

SuperMember
Join Date: Nov 2005
Location: Always About
Age: 27
Posts: 1,508
Blog Entries: 1
Thanks: 1
Thanked 11 Times in 11 Posts
Re: is there a better way

a friend of mine has done this,

PHP: Select all

<?php

    
//get this month and year
    
$month date("n");
    
$year date("Y");
    
    
$jump = isset($_POST['jump']) ? $_POST['jump'] : (isset($_GET['jump']) ? $_GET['jump'] : $month);

    require(
'main.php');
    
    
$month_tots = array();
    for (
$i=1$i<=12$i++) $month_tots[$i] = 0;       // ensure total for every month

    
$res mysql_query ("SELECT MONTH(FROM_UNIXTIME(date)) as mth, COUNT(*) AS num 
                        FROM form
                        WHERE YEAR(FROM_UNIXTIME(date)) = '$year'
                        GROUP BY mth"
);
     while (list(
$mth$num) = mysql_fetch_row($res)) 
     {
         
$month_tots[$mth] = $num;         // put month totals in the array
     
}
     
     
/**
     * create the dropdown
     */
     
echo '<form method="POST">
     <select name="jump" id="jump"  onchange="location.href=this.options[this.selectedIndex].value">'
;
     foreach (
$month_tots as $m => $num)
     {
         
$sel $jump==$m 'selected':'';
         
$dt date ('F Y'mktime(0,0,0,$m1$year));
         echo 
"<option value='index.php?jump=$m' $sel>$dt ($num)</option>";
     }
     echo 
'</select></form>'?>
     
    <?php mysql_close($conn); ?>
but i need to separate the pagementation from the drop down box ( totally seperate so i can 'include' each where i want in my site, at the moment the pagementation is directly above the jump menu

PHP: Select all

<?php include 'config.php';

global 
$jump;

$conn mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
mysql_select_db$dbname$conn );

$perPage 5// number to display on per page

// Start by counting the number of posts

$res mysql_query ("SELECT count(*) AS num FROM form WHERE MONTH(FROM_UNIXTIME(date)) = '".$jump."' ") or die (mysql_error() . 'dying hurts');

$num mysql_fetch_assoc($res);
$num $num['num'];

mysql_free_result($res);

// now work out how many pages there are, check if page has been sent via GET

$numPages ceil($num $perPage);

// Check to make sure pagenum will be 1 or greater, less than numpages and an actual number
$pageNum  = (!empty($_GET['id']) &&
             
is_numeric($_GET['id']) &&
             
$_GET['id'] > 1)

             ? (
$_GET['id'] < $numPages ?  ($_GET['id']) : $numPages) : 1// Set to 1 if not set or something dodgy

// Okay, now we run the query and set the limits LIMIT xx, yy (x= start from , y= limit)


$sql "SELECT * FROM form WHERE MONTH(FROM_UNIXTIME(date)) = '".$jump."' ORDER BY date DESC LIMIT " . ($pageNum 1) * $perPage ", $perPage";
$res mysql_query($sql$conn) or die(mysql_error());

while (
$row mysql_fetch_object($res))
{
echo 
'<hr></hr>';
      print  
$row->title ;
      echo 
nl2br("\n");
      print 
strftime('%d %B %Y'$row->date);
      echo 
nl2br("\n");
      echo 
nl2br("\n");
      print 
$row->blog_mes;

}

// Some paginananananananation

echo '<div>';
for (
$i 1$i <= $numPages$i++)
{
    if (
$pageNum != $i)
    echo 
'<a href="index.php?id='$i'">'$i'</a> ';
    else
    echo 
'<b>'$i'</b>';
}
echo 
'</div>';

?>
cheers
Reply With Quote