I am able to view a specific recored from a db and also i am able to view all of them. how can i make either or both of the following:
1- a drop down menue to select how many records in a page with a next button that views the next lot
2- a page that displays a record a page with a next and previos button. the first record will not have a previos button and the last record will not have a next button.
I am using this to achieve both targets,but it doesn't work all the way through:
PHP Code:
<form name="form1" method="post" action="pages.php">
show
<select name="select">
<option value="1" selected>1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
Per page
<input type="submit" name="Submit" value="Submit">
</form>
<?php
// Database Connection
include 'db.php';
// If current page number, use it
// if not, set one!
$number = $_POST['select'];
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = $number;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM school LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
echo $row['title']."<br />";
echo $row['id']."<br />";
echo $row['make']."<br />";
echo $row['name']."<br />";
echo $row['colour']."<br />";
echo $row['trainer info 1']."<br />";
echo $row['trainer info 2']."<br />";
echo $row['stock']."<br />";
echo $row['price']."<br />";
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM school"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>
and i get this warning:
Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\asgsoft\www\school\pages.php on line 39
Warning: Division by zero in c:\asgsoft\www\school\pages.php on line 56 |
but the actual function of which is to select how many to view on a page partially works because it works for the first page only. but on page 2 it shows only 1 trainer rather than whater i originally told it.
thank you very much