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

时间:2012-02-22 19:03:26

标签: php html pagination html-table

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

首先是它在分页的第一页上它似乎覆盖了CSS并将所有div对齐到页面的右侧...然后当你点击' next'查看接下来的三个结果等等,按照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

你可以看到他们的不同之处!

我希望我有道理!

1 个答案:

答案 0 :(得分:3)

对于初学者,您的</table>循环中有while标记。改变它,看看其他一切是否到位。