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>";
}
?>
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>";
}
?>