I am trying to achieve a web PHP program that will display data from a records in a MySQL database in a HTML form. I have a couple of navigation buttons that I wish to cycle to the next and previous records in the database.
My problem is, I can't seem to cycle back and forth through the records. mysql_fetch_assoc just gets one row or is only good when it comes to a while loop.
Any help would be appreciated!
This is what I have so far...
$page = intval($_GET['page']);
$limitStart = $page - 1;
if($limitStart < 0) {
$limitStart = 0;
}
$query = "SELECT caleadID, region, siteaddr1, siteaddr2, siteaddr3, siteaddr4, sitepcode, ";
$query .= "addgennotes, description, value, award, awardaddr1, awardaddr2, awardaddr3, awardaddr4, awardpcode, ";
$query .= "phone_number, fax_number, fldAwardedWebsite, fldAwardedEmail, contact_name, date FROM tblTradesman_Awarded WHERE 1";
$query .= ' LIMIT ' . $limitStart . ',2';
$result = mysqli_query($connection, $query);
confirm_query($result);
$result_array = array();
while($row = mysqli_fetch_assoc($result)) {
$result_array[] = $row;
}
if(intval($page) === 0) {
$previousRecord = NULL;
$currentRecord = $result_array[0];
$nextRecord = $result_array[1];
} else {
$previousRecord = $result_array[0];
$currentRecord = $result_array[1];
$nextRecord = $result_array[2];
}
if($previousRecord) {
}
if($nextRecord) {
}
?>
$caleadID = $currentRecord['caleadID'];
$region = $currentRecord['region'];
$siteaddr1 = $currentRecord['siteaddr1'];
$siteaddr2 = $currentRecord['siteaddr2'];
$siteaddr3 = $currentRecord['siteaddr3'];
$siteaddr4 = $currentRecord['siteaddr4'];
$sitepcode = $currentRecord['sitepcode'];
$addgennotes = $currentRecord['addgennotes'];
$description = $currentRecord['description'];
$value = $currentRecord['value'];
$award = $currentRecord['award'];
$awardaddr1 = $currentRecord['awardaddr1'];
$awardaddr2 = $currentRecord['awardaddr2'];
$awardaddr3 = $currentRecord['awardaddr3'];
$awardaddr4 = $currentRecord['awardaddr4'];
$awardpcode = $currentRecord['awardpcode'];
$phone_number = $currentRecord['phone_number'];
$fax_number = $currentRecord['fax_number'];
$fldAwardedWebsite = $currentRecord['fldAwardedWebsite'];
$fldAwardedEmail = $currentRecord['fldAwardedEmail'];
$contact_name = $currentRecord['contact_name'];
$date = $currentRecord['date'];
?>
Tradesman Lead ID:
Job Value GBP:
Tradesman Publish Date:
Site Details
-
Site Address:
-
Postcode:
-
Region:
-
General Notes:
- <?PHP echo $addgennotes ?>
-
Description:
- <?PHP echo $description ?>
Contractor Details
-
Awarded To:
-
Address:
-
Postcode:
-
Telephone:
-
Fax:
-
Website:
-
Email:
-
Contact Name:
$contact_made = true;
if ($contact_made) {
echo "
echo "Contact has been made";
echo "
}
?>
解决方案
The code is untested, but it should point you into the right direction.
The GET parameter page controls which section of the result set to show as you set the LIMIT of the query using the value of page.
You select the current record from DB as well as the previous and the next. This way, you know if you can generate a link to the next/previous record in your application.
$page = intval($_GET['page']);
$limitStart = $page - 1;
if($limitStart < 0) {
$limitStart = 0;
}
$query = "SELECT caleadID, region, siteaddr1, siteaddr2, siteaddr3, siteaddr4, sitepcode, ";
$query .= "addgennotes, description, value, award, awardaddr1, awardaddr2, awardaddr3, awardaddr4, awardpcode, ";
$query .= "phone_number, fax_number, fldAwardedWebsite, fldAwardedEmail, contact_name, date FROM tblTradesman_Awarded WHERE 1";
$query .= ' LIMIT ' . $limitStart . ',3';
$result = mysqli_query($connection, $query);
confirm_query($result);
$result_array = array();
while($row = mysqli_fetch_assoc($result)) {
$result_array[] = $row;
}
if(intval($page) === 0) {
$previousRecord = NULL;
$currentRecord = $result_array[0];
$nextRecord = $result_array[1];
} else {
$previousRecord = $result_array[0];
$currentRecord = $result_array[1];
$nextRecord = $result_array[2];
}
if($previousRecord) {
$prevPage = $page - 1;
if($prevPage < 0) {
$pagePage = 0;
}
echo 'previous';
}
if($nextRecord) {
echo 'next';
}