PHP MySQL(i)分页/分页帮助

时间:2016-04-12 16:00:42

标签: php mysql

我正在处理分页/分页代码,但需要一些帮助。

我已经达到了列出所有页码的点,可以点击。 image of paging

现在我试图让它只在当前页面之前和之后输出5页。

  

[首页] [上一页] [6] [7] [8] [9] [10] [ 11 ] [12] [13] [14] [15] [16] [下一页] [上一页]

$dbhost="localhost";
$dbname ="dbase";
$dbuser="root";
$dbpw ="password";
$dbport=;
$dbsocket="";

$dbconnect = mysqli_connect($dbhost,$dbuser,$dbpw,$dbname, $dbport, $dbsocket);

// Check connection
if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error() . "<br /><br />";
 }

$dbmatrix_Join_stmnt="
SELECT 
`dbmatrix`.`RecID`,
`dbmatrix`.`AgencyID`,
`dbmatrix`.`DepartmentHead` AS `DeptHead`,
`dbmatrix`.`PrivateBoolean` AS `MtrxPrvt`, 
`dbmatrix`.`DateEntered` AS `dbmDateEntr`,
`dbmatrix`.`DateRevised` AS `dbmDateRvsd`,
`agency`.`AgencyName`,
`agency`.`Website` AS `AgencySite`,
`agency`.`EmploymentWebAddress` AS `AgencyEmplSite`,
`agency`.`DateEntered` AS `AgencyDateEntr`,
`agency`.`DateRevised` AS `AgencyDateRvsd`,
`dbmatrix`.`GroupID`,
`groups`.`GroupName`,
`groups`.`DateEntered` AS `GrpDateEntr`,
`groups`.`DateRevised` AS `GrpDateRvsd`,
`dbmatrix`.`ContactID`,
`allcontacts`.`FirstName`,
`allcontacts`.`LastName`,
`allcontacts`.`Title`,
`allcontacts`.`Division`,
`allcontacts`.`Address1`,
`allcontacts`.`Address2`,
`allcontacts`.`POBox`,
`allcontacts`.`City`,
`allcontacts`.`State`,
`allcontacts`.`ZipCode`,
`allcontacts`.`Phone`,
`allcontacts`.`Fax`,
`allcontacts`.`Email`,
`allcontacts`.`Website`,
`allcontacts`.`EmploymentWebAddress` AS `ContactEmpSite`,
`allcontacts`.`GeneralInfo` AS `ContGenInfo`,
`allcontacts`.`DateEntered` AS `ContactDateEntr`,
`allcontacts`.`DateRevised` AS `ContactDateRvsd`

FROM `dbase`.`dbmatrix`

INNER JOIN dbase.agency
ON dbmatrix.AgencyID=agency.RecID 

INNER JOIN dbase.groups
ON dbmatrix.GroupID=groups.RecID

INNER JOIN dbase.allcontacts
ON dbmatrix.ContactID=allcontacts.RecID
";

$dbmatrix_Join_qry="$dbmatrix_Join_stmnt
where `dbmatrix`.`PrivateBoolean`='0'
order by `agency`.`AgencyName` ASC
; ";
$queryNTD = mysqli_query($dbconnect,$dbmatrix_Join_qry);


/* Get total number of records */
$sql_qry_All_Directory="$dbmatrix_Join_stmnt where `dbmatrix`.`PrivateBoolean`='0' order by `dbmatrix`.`RecID` ASC ; ";
$query_All_Directory = mysqli_query($dbconnect,$sql_qry_All_Directory);
$count_All_Directory=mysqli_num_rows($query_All_Directory);

echo "<p class='SQLTableCount'><span class='searchTerm bld'>$count_All_Directory</span> Total in Directory</p>";

/*

echo "<p>$page_limit</p>";

echo "<p>$count_All_Directory</p>";

echo "<p>$dbmatrix_Join_qry</p>";

*/



$RecPerPage="10";
$TotalPgs=ceil($count_All_Directory/$RecPerPage);

echo "<p>$TotalPgs Total Pages</p>";

if(isset($_GET["page"])){ 
$page = intval($_GET["page"]); }
else { $page = 1; } 

$calc = $RecPerPage * $page; 
$start = $calc - $RecPerPage; 
$query7832="$dbmatrix_Join_stmnt
where `dbmatrix`.`PrivateBoolean`='0'
order by `agency`.`AgencyName` ASC Limit $start, $RecPerPage ; ";
$result = mysqli_query($dbconnect,$query7832); 
$rows = mysqli_num_rows($result); 

if($rows){ $upage = 0; while($col = mysqli_fetch_assoc($result))
{

$RecID=$col['RecID'];
$AgencyID=$col['AgencyID'];
$GroupID=$col['GroupID'];
$ContactID=$col['ContactID'];

$DeptHead=$col['DeptHead'];
$MtrxPrvt=$col['MtrxPrvt'];
$dbmDateEntr=$col['dbmDateEntr'];
$dbmDateRvsd=$col['dbmDateRvsd'];

$AgencyName=$col['AgencyName'];
$AgencySite=$col['AgencySite'];
$AgencyEmplSite=$col['AgencyEmplSite'];
$AgencyDateEntr=$col['AgencyDateEntr'];
$AgencyDateRvsd=$col['AgencyDateRvsd'];

$GroupName=$col['GroupName'];
$GrpDateEntr=$col['GrpDateEntr'];
$GrpDateRvsd=$col['GrpDateRvsd'];

$FirstName=$col['FirstName'];
$LastName=$col['LastName'];
$Title=$col['Title'];
$Division=$col['Division'];
$Address1=$col['Address1'];
$Address2=$col['Address2'];
$POBox=$col['POBox'];
$City=$col['City'];
$State=$col['State'];
$ZipCode=$col['ZipCode'];
$Phone=$col['Phone'];
$Fax=$col['Fax'];
$Email=$col['Email'];
$Website=$col['Website'];
$ContactEmpSite=$col['ContactEmpSite'];
$GenInfo=$col['ContGenInfo'];
$ContactDateEntr=$col['ContactDateEntr'];
$ContactDateRvsd=$col['ContactDateRvsd'];


echo "<div style='display:inline-block; width:300px; border-bottom:1px solid #eeeeee; margin:10px 15px 10px; vertical-align:top; '>";

echo "<div class='bld ' style='font-weight:bold; '>$AgencyName</div>";

echo "<div class='___' style='___'>$Division</div>";

echo "<div class='___' style='color:#0033FF; '>$FirstName $LastName</div>";

echo "<div class='___' style='font-style: italic; '>$Title</div>";

echo "<div class='___' style='___'>$Address1</div>";

echo "<div class='___' style='___'>$Address2</div>";

echo "<div class='___' style='___'>$POBox</div>";

echo "<div class='___' style='___'><span style='color:#330066; '>$City</span>, $State $ZipCode</div>";

echo "<div class='___' style='___'>$Phone</div>";

echo "</div>";


};
};

echo "<br />";
echo "<br />";


if(isset($page)) {
$result = mysqli_query($dbconnect,"select Count(*) As Total FROM `dbase`.`dbmatrix` where `dbmatrix`.`PrivateBoolean`='0' ; ");
$rows = mysqli_num_rows($result);

if($rows) { $rs = mysqli_fetch_assoc($result);
$total = $rs["Total"];
} 

$totalPages = ceil($total/$RecPerPage);


// Shows previous button
if($page <=1 ){ echo "<span id='page_links' style='font-weight:;'>Prev</span> | ";
} 
else { $CurrPage = $page - 1;
echo "<span><a id='page_a_link' href='?page=$CurrPage'>< Prev</a> | </span> ";
} 


for ($upage=1; $upage <= $totalPages; $upage++) 
{if($upage<>$page) { echo "<span> | <a id='page_a_link' href='?page=$upage'>$upage</a></span>"; }
else { echo " | <span id='page_links' style='font-weight: bold;'>$upage</span>";
} 
}

/*

*/


// shows next button
if($page == $totalPages ) { echo "<span id='page_links' style='font-weight:;'> | Next ></span>"; }
else { $CurrPage = $page + 1; echo "<span> | <a id='page_a_link' href='?page=$CurrPage'>Next ></a></span>"; } 

}

$CurrPage=$CurrPage-1;
echo "<div>Current Page: $CurrPage of $TotalPgs</div>";



echo "<br />";
echo "<br />";
echo "<br />";
echo "<br />";

真的希望得到一些帮助吗

提前谢谢!

1 个答案:

答案 0 :(得分:0)

假设$ page是当前页码,请尝试更改:

for ($upage=1; $upage <= $totalPages; $upage++)

for ($upage=max(1,$page - 5); $upage <= min($totalPages, $page + 5); $upage++)