mysql循环遍历获取_如何循环遍历MySQL行?

bd96500e110b49cbb3cd949968f18be7.png

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';

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值