...

View Full Version : Help with Pagination and holding the drop down form value for Next Page!



mobimad
01-12-2010, 02:33 PM
Hi, I've been banging my head against a wall for days now, and i just cant solve my little problem.

I have compiled this script below on 1 page called test.php: which pulls data from my DB. I have a drop down form that should only pull data from the DB by the value of the MONTH.

I fould a tutorial to add pagination so that i can limit the data on the page to 15 rows, and then click > next to view the next 15 and so-on, this seems to work fine.

But

1. On page load the entire DB is loaded, i want nothing until a month is selected from the drop down form.

2. When for e.g February is selected from drop down, it show the 1st 15 records/rows perfectly, but when you hit the > next page link it reverts back to showing the entire DB and not only the continued February records.

Please stop me from smashing my head against this wall any longer.
The biggest thankyou in-advance.
P.S I'm am a PHP Novice
Jenna



<?

echo "
<form method=post action=test.php>
<select name=month>
<option value='Select'>Select</option>
<option value='January'>January</option>
<option value='February'>February</option>
<option value='March'>March</option>
<option value='April'>April</option>
<option value='May'>May</option>
<option value='June'>June</option>
<option value='July'>July</option>
<option value='August'>August</option>
<option value='September'>September</option>
<option value='october'>October</option>
<option value='November'>November</option>
<option value='December'>December</option>
</select>
<input type=submit value=Select name=select>
";

?>


<?php

// How many entries you want to show per page
$perpage = 15;

// Find out what page of entries you are looking for
// The reason why you subtract 1 is so that you can calculate which returned entry (for later when we query the database) to start at
if (isset($_GET['page'])) {
$page = max(intval($_GET['page'] - 1), 0);
$page_original = intval($_GET['page']);
}
else {
$page = 0;
$page_original = 1;
}

$start_at = $page * $perpage;
$start_at_public = $start_at + 1;

mysql_connect('localhost', 'USER', 'PASS');
mysql_select_db('DATABASE') or die( 'Unable to select database');

// How many entries are there?
$query = "SELECT COUNT(*) FROM payments WHERE date LIKE '%$month%' ";
$result = mysql_query($query) or die('Error in query: ' . $query);
$total_entries = mysql_result($result, 0);

// You've asked for a page that is too high
if ($total_entries < $start_at_public) {
die('No Queries Found.');
}

// Grab the specified number of entries, starting at the appropriate record
$query2 = "select * FROM payments WHERE date LIKE '%$month%' ORDER by id DESC LIMIT $start_at, $perpage";
$result2 = mysql_query($query2);
$num=mysql_numrows($result2);

// Print those entries!
while ($entries = mysql_fetch_object($result2)) {
print '<div class="entry">' . "\n";
print '<h2>' . $entries->title . '</h2>' . "\n";
print '<p>' . $entries->content . '</p>' . "\n";
print '</div>' . "\n";
}

mysql_close();

$end_value_this_page = min($start_at + $perpage, $total_entries);

if (($end_value_this_page == $total_entries) && ($start_at_public == $total_entries)) {
print '<p>Entry ' . $end_value_this_page . ' of ' . $total_entries . '</p>' . "\n";
}

else {
print '<p>Entries ' . ($start_at_public) . '-' . $end_value_this_page . ' of ' . $total_entries . '</p>' . "\n";
}

// How many pages of content are there?
$total_pages = ceil($total_entries / $perpage);

if ($total_pages != -1) {

print '<div class="paginator">' . "\n";

$next_link = '';
$previous_link = '';
$page_links = '';

// Loop through each of the pages
for ($page_count = 1; $page_count <= $total_pages; ++$page_count) {

if ($page_original == $page_count) {

// We're on this page, so no link needed
$page_links .= $page_count;

// Get the "previous" link if there is more than one page and if this isn't the first page
if ($total_pages > 1 && $page_count != 1) {
$previous_link = '<a href="?page=' . ($page_count - 1) . '"><</a> | ';
}

// If there are more pages than this, prepare the "next" link
if ($page_count != $total_pages) {
$next_link = ' <a href="?page=' . ($page_count + 1) . '">></a>';
}
}

else {
$page_links .= '<a href="?page=' . $page_count . '">' . $page_count . '</a>';
}

if ($page_count != $total_pages) {
// Print a separator for all pages but the last page
$page_links .= ' | ';
}
}
print $previous_link . $page_links . $next_link . '</div>';

?>

<table ></p>
<tr>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">NAME</font></div></th>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">ALE</font></div></th>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">ALE</font><font face="Arial, Helvetica, sans-serif"> ID</font></div></th>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">REGION</font></div></th>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">NETWORK </font></div></th>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">UID </font></div></th>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">CID </font></div></th>
<th bgcolor="#9999FF"><div align="center"><font face="Arial, Helvetica, sans-serif">DATE </font></div></th>
</tr>

<div align="center"></div>

<font size="2">

<?
$i=0;
while ($i < $num) {
$aff=mysql_result($result2,$i,"name");
$sale=mysql_result($result2,$i,"ale");
$saleID=mysql_result($result2,$i,"aleID");
$region=mysql_result($result2,$i,"region");
$network=mysql_result($result2,$i,"network");
$uid=mysql_result($result2,$i,"uid");
$cid=mysql_result($result2,$i,"cid");
$date=mysql_result($result2,$i,"date");
?>
</font>

<tr>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$name"; ?></font></div></td>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$ale"; ?></font></div></td>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$aleID"; ?></font></div></td>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$region"; ?></font></div></td>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$network"; ?></font></div></td>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$uid"; ?></font></div></td>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$cid"; ?></font></div></td>
<td><div align="center"><font size="1" face="Arial, Helvetica, sans-serif"><? echo "$date"; ?></font></div></td>
</tr>

<div align="center"><font size="2">
<?
++$i;
}
echo "</table>";
}

?>

JAY6390
01-12-2010, 02:38 PM
Simply wrap all of your code in an if statement
if(isset($_GET['month'])) {..
All of your query code here
... }

mobimad
01-12-2010, 03:26 PM
Hi Jay,

Thanks for the info, but i've put your mentioned if statement just about everywhere within the code and it stops working completely. I'm unsure where to put it to be honest. :(

Thankyou
Jenna

JAY6390
01-12-2010, 03:27 PM
put it around this

// Print those entries!
while ($entries = mysql_fetch_object($result2)) {
print '<div class="entry">' . "\n";
print '<h2>' . $entries->title . '</h2>' . "\n";
print '<p>' . $entries->content . '</p>' . "\n";
print '</div>' . "\n";
}

mobimad
01-12-2010, 03:47 PM
Hi Jay,

Ok i have added it around that part of code, but it hasnt changed what happens...

When February is selected from the drop down form it does show the 15 records/rows but when i hit the next page 2 ( 1|2 > ) link it doesnt show the rest of the february records/rows, it shows the next 15 records/rows of the entire DB.

Thanks for your help so-far
Jenna

JAY6390
01-12-2010, 03:52 PM
oh OK. Change this line

$next_link = ' <a href="?page=' . ($page_count + 1) . '">></a>';
to

$next_link = ' <a href="?page=' . ($page_count + 1) .(isset($_GET['month']) ? '&amp;'.$_GET['month'] : '') '">></a>';
to begin with. Does that make the next button work?

mobimad
01-12-2010, 04:00 PM
Afraid not, now i get a

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

JAY6390
01-12-2010, 04:02 PM
Oops forgot the .
This should work

$next_link = ' <a href="?page='.($page_count + 1).(isset($_GET['month']) ? '&amp;'.$_GET['month'] : '').'">></a>';

mobimad
01-12-2010, 04:07 PM
Hi,
It fixed the syntax error, but still hasnt corected the next link for February.
Jenna

JAY6390
01-12-2010, 04:19 PM
ah, this is a problem too
<form method=post action=test.php>
your method is post and it should be GET
<form method="GET" action="test.php">

mobimad
01-12-2010, 04:37 PM
Ok i have changed it to GET

On the next link ( > ) i now get

/test.php?page=2&February

Which looks promising, but it doesnt use February in the

WHERE date LIKE '%$month%'

so it still shows all data from DB when the next link is clicked and not the rest of februarys data.

It looks like we (YOU) might be close.

Thanks-so-far-jay
Jenna

JAY6390
01-12-2010, 04:42 PM
ok I am far too tired lol. It should have been this for the link

$next_link = ' <a href="?page='.($page_count + 1).(isset($_GET['month']) ? '&amp;month='.$_GET['month'] : '').'">></a>';
also change your prev_link to

$previous_link = '<a href="?page='.($page_count - 1).(isset($_GET['month']) ? '&amp;month='.$_GET['month'] : '').'"><</a> | ';
See if that gets those two working :)

mobimad
01-12-2010, 05:04 PM
Excellent,

It looks like its working for the > link

Yipee :)

How would i change it so that the 1|2|3|4 links, get the month aswell.

Much Appreciated
Jenna

JAY6390
01-12-2010, 05:07 PM
Change this line

$page_links .= '<a href="?page=' . $page_count . '">' . $page_count . '</a>';to

$page_links .= '<a href="?page='.$page_count.(isset($_GET['month']) ? '&amp;month='.$_GET['month'] : '').'">'.$page_count.'</a>';

mobimad
01-12-2010, 05:27 PM
Thank you for help today Jay
much appreciated
All the best
Jenna

JAY6390
01-12-2010, 05:39 PM
No pproblem :)

sir.jones
01-12-2010, 06:10 PM
Hi Jay... Excellent Finishing... I lift up all my thumb to You :thumbsup:

But let me ask you, would you like to help me too Please? if you have consideration to looking forward of my invitations I greatly Appreciate it and Thank You 1st.
Please feel free to see my problem at: http://www.codingforums.com/showthread.php?t=185737



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum