gpt4 book ai didi

php - MYSQL PHP 分页无法正常工作链接

转载 作者:行者123 更新时间:2023-11-29 21:58:23 24 4
gpt4 key购买 nike

我正在尝试进行分页,我有下一个代码,但我无法让它工作。

<?php   
$link = mysqli_connect("localhost", "", " ", "");

if($link === false) { die('<span class="rosu">EROARE:</span> Nu s-a putut realiza conexiunea la baza de date.<br/><br/>Va rog verificati conexiunea pentru baza de date.<br/>' . mysqli_connect_error()); }

$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;

$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu' ORDER BY id_contract DESC LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo '<table id="contracte">';
echo "<tr>";
echo "<th>EDT</th>";
echo "<th>Numar Contract</th>";
echo "<th>C</th>";
echo "<th>Data Realizare</th>";
echo "<th>Nume Firma</th>";
echo "<th>Nume Locatie</th>";
echo "<th>Zona Judet</th>";
echo "<th>Servicii</th>";
echo "<th>Suma Plata</th>";
echo "<th>PER. CTR.</th>";
echo "<th>Nume Contact</th>";
echo "<th>Telefon</th>";
echo "<th>E</th>";
echo "<th>T</th>";
echo "<th>W</th>";
echo "<th>DEL</th>";
echo "</tr>";

while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td class='editbutton'><a href='modifica.php?ID=" . $row['id_contract'] . "'><img src='images/edit.png'></img></a></td>";
echo '<td><a href="vezi.php?contract=' . $row['id_contract'] . '&firma=' . $row['nume_firma'] . '&numar=' . $row['numar_contract'] . '">' . $row['numar_contract'] . ' <img src="images/link.png" width="14px"></img></a></td>';
echo "<td><a href='pdf/" . $row['link_contract_pdf'] . "' target='_blank'><img src='images/pdf.png' width='20px'></img></a></td>";
echo "<td>" . $row['expirare_contract'] . "</td>";
echo "<td>" . $row['nume_firma'] . "</td>";
echo "<td>" . $row['nume_locatie'] . "</td>";
echo "<td>" . $row['zona_judet_oras'] . "</td>";
echo "<td>" . $row['servicii_oferite'] . "</td>";
echo "<td>" . $row['suma_de_plata'] . "</td>";
echo "<td>" . $row['perioada_contract'] . "</td>";
echo "<td>" . $row['nume_contact'] . "</td>";
echo "<td>" . $row['telefon_contact'] . "</td>";
echo '<td><a href="mailto:' . $row['email_contact'] . '?Subject=Contract%20Info3D" target="_blank"><img src="images/email.png"></img></a></td>';
echo '<td><a href="http://' . $row['link_turvirtual'] . '"><img src="images/turvirtual.png"></img></a></td>';
echo '<td><a href="http://' . $row['adresa_website'] . '"><img src="images/website.png"></img></a></td>';
echo "<td class='deletebutton'><a href=sterge.php?ID=" . urlencode(str_replace(' ', '-', $row['id_contract'])) . "&NR=" . urlencode(str_replace(' ', '-', $row['numar_contract'])) . "&DT=" . urlencode(str_replace(' ', '-', $row['expirare_contract'])) . "&FR=" . urlencode(str_replace(' ', '-', $row['nume_firma'])) . "&ZN=" . urlencode(str_replace(' ', '-', $row['zona_judet_oras'])) . "&PL=" . urlencode(str_replace(' ', '-', $row['servicii_oferite'])) . "><img src='images/delete.png'></img></a></td>";
echo "</tr>";
}

echo "</table>";

mysqli_free_result($result);
} else { print ('<span class="rosu">EROARE!</span> Nu am gasit contracte in baza de date.'); }
} else { print ('<span class="rosu">EROARE!</span> Nu s-a putut executa comada de listare a contractelor.<br/><br/><b>Motivul pentru care nu s-a putut accesa tabelul: </b>') . mysqli_error($link); print '.'; }

?>

<?php
$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu'";
$total_records = mysql_num_rows($sql); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);

echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> "; // Goto 1st page

for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> ";
};
echo "<a href='lista.php?page=$total_pages'><div class='pagina'>$total_pages</div></a> "; // Goto last page
?>

问题是我对它进行了分页,但它没有显示正确的链接。所以我认为问题出在下一个代码中:

<?php 
$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu'";
$total_records = mysql_num_rows($sql); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);

echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> "; // Goto 1st page

for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> ";
};
echo "<a href='lista.php?page=$total_pages'><div class='pagina'>$total_pages</div></a> "; // Goto last page
?>

谢谢!

=编辑:

[02-Oct-2015 12:18:57 Europe/Bucharest] PHP Warning:  mysql_num_rows() expects parameter 1 to be resource, string given in /lista.php on line 81

我在 error_log 中收到此错误。

81行是:

$total_records = mysql_num_rows($sql); 

最佳答案

试试这个,我猜你已经为第一页和最后一页放置了链接,并且你再次在循环中执行此操作。

for ($i=2; $i<$total_pages; $i++) { 
echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div> </a> ";
};

关于php - MYSQL PHP 分页无法正常工作链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32903965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com