PHP - 分页和位置结果是一个表格

发布于 2025-01-08 03:20:56 字数 3250 浏览 1 评论 0原文

我尝试过一些分页,但它给了我非常意想不到的结果....

第一个是它在分页的第一页上似乎覆盖了 CSS 并将所有 div 对齐到页面的右侧。 ..然后单击“下一步”查看接下来的三个结果,依此类推,页面将按照 CSS 的指示返回!

第二个问题是显示结果的表格似乎不起作用。分页每页上的第一个结果在表格中,接下来的两个结果(每页三个结果)显示在页面上,但在下面而不是在表格中!

代码很长,但我们开始吧:

<?php
session_start();
if ( !isset($_SESSION['username']))
{
header("Location:index.php");
exit();
}

//connect to database

require "dbconn.php";


$per_page = 3;
$start = $_GET['start'];





$sort = @$_POST['order'];  
if (!empty($sort)) {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
            FROM booktable
            ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC";
}
 else { 
$query = "SELECT bookname, bookauthor, bookpub, bookisbn 
                FROM booktable 
                ORDER BY bookname ASC"; 
}


$results = mysql_query($query)
            or die (mysql_error());

$record_count = mysql_num_rows($results);
if (!$start)
   $start = 0;

$get = mysql_query("SELECT * FROM booktable LIMIT $start, $per_page");
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>

然后我将剪掉所有不必要的 html

<div id="mid">

<?php
echo "<table border='2px'>";
echo "<tr>";
echo "<th>";

echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";

echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
 // get data
 $bookname = $row['bookname'];
 $bookauthor = $row['bookauthor'];
 $bookpub = $row['bookpub'];
 $bookisbn = $row['bookisbn'];



    echo "<tr>";
    echo "<td>";
    echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";

    echo "</td>";

    echo "<td>";
    echo $bookname;
    echo "</td>";

    echo "<td>";
    echo $bookauthor;
    echo "</td>";

    echo "<td>";
    echo $bookpub;
    echo "</td>";

    echo "<td>";
    echo $bookisbn;
    echo "</td>";

    echo "</tr>";
    echo "</table>";

}

$prev = $start - $per_page;
$next = $start + $per_page;

if (!($start<=0))
       echo "<a href='products.php?start=$prev'>Prev</a> ";

       //set variable for first page number
$i=1;

//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
 if ($start != $x)
   echo "<a href='products.php?start=$x'>$i</a>";
 else
    echo "<a href='products.php?start=$x'><b>$i</b></a>";
 $i++;
}

//show next button
if (!($start >= $record_count - $per_page))
       echo "<a href='products.php?start=$next'>Next</a>";
?>



<?php echo $record_count; ?>

这就是页面的外观(图 1)(表格边框显示了问题)

image1

正如我之前所说,当列表首次显示并且页面最终看起来像这样时,我也会遇到问题: (image2)

image2

您可以看到它们有何不同!

我希望我说得有道理!

I have tried my hand at some pagination but it gives me very unexpected results....

The first is that it on the first page of the pagination it seems to override the CSS and align all divs to the right hand side of the page... when you then click 'next' to view the next three results and so on the page goes back as per the CSS tells it!

The second problem is that the table displaying the results doesn't seem to work. The very first result on each page of the pagination is in the table, the next two results (three results per page) are displayed on the page however below and not in the table!

The code is quite long but here we go:

<?php
session_start();
if ( !isset($_SESSION['username']))
{
header("Location:index.php");
exit();
}

//connect to database

require "dbconn.php";


$per_page = 3;
$start = $_GET['start'];





$sort = @$_POST['order'];  
if (!empty($sort)) {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
            FROM booktable
            ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC";
}
 else { 
$query = "SELECT bookname, bookauthor, bookpub, bookisbn 
                FROM booktable 
                ORDER BY bookname ASC"; 
}


$results = mysql_query($query)
            or die (mysql_error());

$record_count = mysql_num_rows($results);
if (!$start)
   $start = 0;

$get = mysql_query("SELECT * FROM booktable LIMIT $start, $per_page");
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>

Then I will cut out all the unnecesary html

<div id="mid">

<?php
echo "<table border='2px'>";
echo "<tr>";
echo "<th>";

echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";

echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
 // get data
 $bookname = $row['bookname'];
 $bookauthor = $row['bookauthor'];
 $bookpub = $row['bookpub'];
 $bookisbn = $row['bookisbn'];



    echo "<tr>";
    echo "<td>";
    echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";

    echo "</td>";

    echo "<td>";
    echo $bookname;
    echo "</td>";

    echo "<td>";
    echo $bookauthor;
    echo "</td>";

    echo "<td>";
    echo $bookpub;
    echo "</td>";

    echo "<td>";
    echo $bookisbn;
    echo "</td>";

    echo "</tr>";
    echo "</table>";

}

$prev = $start - $per_page;
$next = $start + $per_page;

if (!($start<=0))
       echo "<a href='products.php?start=$prev'>Prev</a> ";

       //set variable for first page number
$i=1;

//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
 if ($start != $x)
   echo "<a href='products.php?start=$x'>$i</a>";
 else
    echo "<a href='products.php?start=$x'><b>$i</b></a>";
 $i++;
}

//show next button
if (!($start >= $record_count - $per_page))
       echo "<a href='products.php?start=$next'>Next</a>";
?>



<?php echo $record_count; ?>

This is how the page looks (image 1) (the table border shows the problem)

image1

As I said before I also get the problem when the list first gets displayed and the page ends up looking like this: (image2)

image2

You can see how they differ!

I hope that I have made sense!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2025-01-15 03:20:56

首先,您的 while 循环中有 标记。改变它,看看其他一切是否都到位。

For starters, you have your </table> tag in your while loop. Change that and see if everything else falls into place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文