我遇到了问题,我不知道如何修复它。 如果我单击从此php生成的链接,结果与第1页上显示的结果相同。例如,view.php?page = 2显示与view.php?page = 1或3等相同的数据。 这种情况发生在'view.php?ID ='以及。
<?php
require_once('connection.php');
require_once('auth.php');
echo "<br /><br /><font color=red size='6'><center>Facturi emise</center></font><br /><br />";
$query = "SELECT * FROM out_fact ORDER BY ID DESC LIMIT 0, 20";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0) {
echo "<table cellpadding=10 border=1 align=center>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>"."<a href=javascript:window.open('view.php?ID=".$row[0]."','NAME','location=false')><font color=blue size='4'><center>".$row[1]." din data de ".$row[2]."</a></center>"."</font></td>";
echo "<td>"."<font color=red size='4'><center>".$row[3]." ".$row[12]."</center></font></td>";
echo "</tr>";
};
echo "</table>";
}
else {
echo "No rows found!";
}
echo "<tr><td> <a href='view.php?ID=".$row[0]."'>".$row[1]."</a></td></tr>";
mysql_free_result($result);
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "SELECT COUNT(ID) FROM out_fact";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$total_records = $result[0];
$total_pages = ceil($total_records / 20);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='view.php?page=".$i."'>".$i."</a> ";
};
?>
如何发布正确的数据或缺少的数据。我是php编码新手...... 提前谢谢。
答案 0 :(得分:0)
至少尝试使用 MySQLi 而不是弃用 MySQL 。你要做的就是称为分页。
<强> connection.php 强>:
<?php
$con=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
您的主文件:
<?php
include('connection.php');
require_once('auth.php');
echo "<br><br><font color=red size='6'><center>Facturi emise</center></font><br><br>";
$query = "SELECT * FROM out_fact ORDER BY ID DESC";
$result = mysqli_query($con,$query);
$count=mysqli_num_rows($result);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 10; /* NUMBER OF ROWS TO SHOW PER PAGE */
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$result=mysqli_query($con,"SELECT * FROM out_fact ORDER BY ID DESC LIMIT $offset,$rowsperpage");
echo "<table cellpadding=10 border=1 align=center>";
while($row = mysqli_fetch_array($result)){
/* JUST REPLACE THE NECESSARY DATA YOU WANT TO INPUT INSIDE THIS WHILE LOOP */
$rowdata=mysqli_real_escape_string($con,$row[0]);
echo "<tr>";
echo "<td><a href='view.php?ID=$rowdata&location=false' target='_blank'><font color=blue size='4'><center>".$row[1]." din data de ".$row[2]."</a></center>"."</font></td>"; /* JUST REPLACE THE NECESSARY DATA IN THE LINK IF YOU MUST */
echo "<td>"."<font color=red size='4'><center>".$row[3]." ".$row[12]."</center></font></td>";
echo "</tr>";
} /* END OF WHILE LOOP */
if($count==0){
echo "<tr><td></td><td>No results found.</td><td></td></tr>";
}
else { /* START OF PAGINATION LINK */
echo '<tr height="30px;" valign="bottom"><td></td><td>';
echo "<table style='border-collapse:separate; border-spacing:3px;'><tr>";
/****** build the pagination links ******/
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo "<td style='width:70px; background-color:fff; border:solid #08c 1px; font-size:14px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage' style='background-color:fff;'>Previous</a> </td>";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #ccc 2px;' align='center'> <font color='#ccc'><b>$x</b></font> </td>";
} else {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x' style='background-color:fff;'>$x</a> </td>";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo "<td style='width:70px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage' style='background-color:fff;'>Next</a> </td>";
} // end if
/****** end build pagination links ******/
echo "</tr></table></td></tr>";
} /* END OF ELSE IF COUNT 0 */
echo '</table>';
?>