如果我已经将票证表设置为拆分为多个页面,如何使其底部具有页面链接?

发布于 2024-12-05 03:44:59 字数 4821 浏览 0 评论 0原文

这个问题已得到解答。 单击此处查看回答我问题的答案。


编辑:

我仍然不知道该怎么做。有人可以帮忙吗?我知道它被称为“分页”,但我不知道如何让它在底部显示页面链接。我希望有人能够提供帮助。


I have a table of tickets and all of the data is loaded in from a database to the table. I have pages on it (using ?page=1, ?page=2, etc.) but, how can I make it where it has "Next Page", "Previous Page", "Last Page", "Page something of something", etc. at the bottom?

完成这一切的代码是:

<table id="tickets">
 <tr>
 <th>Name</th>
 <th>Email</th>
 <th>Subject</th>
 <th>Created on</th>
 <th style="width:65px;">Status</th>
 <th>Actions</th>
 </tr><?php
$startpoint = $_GET["page"];
if (isset($_GET["page"])) {
    $startpoint = $_GET["page"];
}
else {
    $startpoint = 1;
}
$startpoint = $startpoint - 1;
$startpoint = $startpoint * 10;
$endpoint = $startpoint + 10;

function addEllipsis($string, $length) {
    $end = "...";
    if (strlen($string) > $length) {
        $length -= strlen($end); // $length =  $length – strlen($end);
        $string = substr($string, 0, $length);
        $string .= $end; //  $string =  $string . $end;
    }
    return $string;
}
$x = 1;
if ($show == "active") {
    $result = mysql_query("SELECT * FROM tickets WHERE is_active='1'");
    while ($info = mysql_fetch_array($result)) {
        $x++;
        $name = $info['name'];
        $email = $info['email'];
        $subject = $info['subject'];
        $ticketid = $info['ticket'];
        $isActive = $info['is_active'];
        $created = $info['created'];
        if ($isActive == '1') {
            $status = "<span class=\"open\">Open</span>";
        }
        if ($isActive == '0') {
            $status = "<span class=\"closed\">Closed</span>";
        }
        if ($x > $startpoint && $x < $endpoint) {
            echo "
 <tr>
 <td>".$name."</td>
 <td>".$email."</td>
 <td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
 <td>".$created."</td>
 <td>".$status."</td>
 <td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket &raquo;</a></td>
 </tr>";
        }
    }
}
else if ($show == "closed") {
    $result = mysql_query("SELECT * FROM tickets WHERE is_active='0'");
    while ($info = mysql_fetch_array($result)) {
        $x++;
        $name = $info['name'];
        $email = $info['email'];
        $subject = $info['subject'];
        $ticketid = $info['ticket'];
        $isActive = $info['is_active'];
        $created = $info['created'];
        if ($isActive == '1') {
            $status = "<span class=\"open\">Open</span>";
        }
        if ($isActive == '0') {
            $status = "<span class=\"closed\">Closed</span>";
        }
        if ($x > $startpoint && $x < $endpoint) {
            echo "
 <tr>
 <td>".$name."</td>
 <td>".$email."</td>
 <td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
 <td>".$created."</td>
 <td>".$status."</td>
 <td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket &raquo;</a></td>
 </tr>";
        }
    }
}
else {
    $result = mysql_query("SELECT * FROM tickets");
    while ($info = mysql_fetch_array($result)) {
        $x++;
        $name = $info['name'];
        $email = $info['email'];
        $subject = $info['subject'];
        $ticketid = $info['ticket'];
        $isActive = $info['is_active'];
        $created = $info['created'];
        if ($isActive == '1') {
            $status = "<span class=\"open\">Open</span>";
        }
        if ($isActive == '0') {
            $status = "<span class=\"closed\">Closed</span>";
        }
        if ($x > $startpoint && $x < $endpoint) {
            echo "
 <tr>
 <td>".$name."</td>
 <td>".$email."</td>
 <td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
 <td>".$created."</td>
 <td>".$status."</td>
 <td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket &raquo;</a></td>
 </tr>";
        }
    }
}
  ?>

 </table>

我怎样才能让它在底部显示每个页面的链接(只是一个简单的“1”、“2”等)?我无法自己创建它们,因为我不知道有多少页面之类的东西。如果当您位于某个页面时链接可以变成灰色的不可点击文本,那就太好了。有人帮助我使用页面功能,但他们不在线,所以他们无法帮助我。 (我还不太擅长 PHP)有人知道如何实现这一点吗?

This question is answered. Click here to see the answer that answered my question.


Edit:

I still don't know exactly how to do this. Could someone help? I get the fact that it's called "pagination" but I don't know how to make it display page links at the bottom. I hope someone will be able to help.


I have a table of tickets and all of the data is loaded in from a database to the table. I have pages on it (using ?page=1, ?page=2, etc.) but, how can I make it where it has "Next Page", "Previous Page", "Last Page", "Page something of something", etc. at the bottom?

The code that does it all is:

<table id="tickets">
 <tr>
 <th>Name</th>
 <th>Email</th>
 <th>Subject</th>
 <th>Created on</th>
 <th style="width:65px;">Status</th>
 <th>Actions</th>
 </tr><?php
$startpoint = $_GET["page"];
if (isset($_GET["page"])) {
    $startpoint = $_GET["page"];
}
else {
    $startpoint = 1;
}
$startpoint = $startpoint - 1;
$startpoint = $startpoint * 10;
$endpoint = $startpoint + 10;

function addEllipsis($string, $length) {
    $end = "...";
    if (strlen($string) > $length) {
        $length -= strlen($end); // $length =  $length – strlen($end);
        $string = substr($string, 0, $length);
        $string .= $end; //  $string =  $string . $end;
    }
    return $string;
}
$x = 1;
if ($show == "active") {
    $result = mysql_query("SELECT * FROM tickets WHERE is_active='1'");
    while ($info = mysql_fetch_array($result)) {
        $x++;
        $name = $info['name'];
        $email = $info['email'];
        $subject = $info['subject'];
        $ticketid = $info['ticket'];
        $isActive = $info['is_active'];
        $created = $info['created'];
        if ($isActive == '1') {
            $status = "<span class=\"open\">Open</span>";
        }
        if ($isActive == '0') {
            $status = "<span class=\"closed\">Closed</span>";
        }
        if ($x > $startpoint && $x < $endpoint) {
            echo "
 <tr>
 <td>".$name."</td>
 <td>".$email."</td>
 <td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
 <td>".$created."</td>
 <td>".$status."</td>
 <td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket »</a></td>
 </tr>";
        }
    }
}
else if ($show == "closed") {
    $result = mysql_query("SELECT * FROM tickets WHERE is_active='0'");
    while ($info = mysql_fetch_array($result)) {
        $x++;
        $name = $info['name'];
        $email = $info['email'];
        $subject = $info['subject'];
        $ticketid = $info['ticket'];
        $isActive = $info['is_active'];
        $created = $info['created'];
        if ($isActive == '1') {
            $status = "<span class=\"open\">Open</span>";
        }
        if ($isActive == '0') {
            $status = "<span class=\"closed\">Closed</span>";
        }
        if ($x > $startpoint && $x < $endpoint) {
            echo "
 <tr>
 <td>".$name."</td>
 <td>".$email."</td>
 <td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
 <td>".$created."</td>
 <td>".$status."</td>
 <td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket »</a></td>
 </tr>";
        }
    }
}
else {
    $result = mysql_query("SELECT * FROM tickets");
    while ($info = mysql_fetch_array($result)) {
        $x++;
        $name = $info['name'];
        $email = $info['email'];
        $subject = $info['subject'];
        $ticketid = $info['ticket'];
        $isActive = $info['is_active'];
        $created = $info['created'];
        if ($isActive == '1') {
            $status = "<span class=\"open\">Open</span>";
        }
        if ($isActive == '0') {
            $status = "<span class=\"closed\">Closed</span>";
        }
        if ($x > $startpoint && $x < $endpoint) {
            echo "
 <tr>
 <td>".$name."</td>
 <td>".$email."</td>
 <td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
 <td>".$created."</td>
 <td>".$status."</td>
 <td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket »</a></td>
 </tr>";
        }
    }
}
  ?>

 </table>

How could I make it show links to the each page (just a simple "1", "2", etc.) at the bottom? I can't create them myself because I wont know how much pages there is and stuff. It would also be great if the link could turn into grey non-clickable text when you are on that certain page. Someone helped me with the page feature but they aren't online right so they can't help me. (I'm not that good at PHP yet) Anyone know how to accomplish this?

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

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

发布评论

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

评论(2

我为君王 2024-12-12 03:44:59

-编辑-
聊天后,解决方案包括向查询添加 LIMIT 而不是使用 $x 来防止显示某些行,如下所示:https://gist.github.com/1233850


运行以下代码,因为它可以单独工作,所以可能会给您一个想法:

<style>span{background:#ccc;padding:2px 6px;margin:0 2px;}</style>

<?php
    $currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
    $totalPages = 5; //this must be fetched from the db

    //define previous and next
    $previous = ($currentPage > 1) ? $currentPage - 1 : false; 
    $next = ($currentPage < $totalPages) ? $currentPage + 1 : false;

    //here we'll save the resulting code
    $pagination = array();
    //first
    $pagination[] = ($currentPage == 1) ? "First" : "<a href='?page=1'>First</a>";
    //previous
    $pagination[] = $previous ? "<a href='?page=$previous'>Previous</a>" : "Previous";
    //current/total
    $pagination[] = "Page $currentPage / $totalPages";
    //next
    $pagination[] = $next ? "<a href='?page=$next'>Next</a>" : "Next";
    //last
    $pagination[] = ($currentPage == $totalPages) ? "Last" : "<a href='?page=$totalPages'>Last</a>";

    //show it
    foreach($pagination as $part){
        echo "<span>$part</span>";
    }
?>

-edit-
After chat, the solution that includes adding a LIMIT to the query instead of using $x to prevent showing certain rows was the following: https://gist.github.com/1233850


Run the following code, since it works on its own it may give you an idea:

<style>span{background:#ccc;padding:2px 6px;margin:0 2px;}</style>

<?php
    $currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
    $totalPages = 5; //this must be fetched from the db

    //define previous and next
    $previous = ($currentPage > 1) ? $currentPage - 1 : false; 
    $next = ($currentPage < $totalPages) ? $currentPage + 1 : false;

    //here we'll save the resulting code
    $pagination = array();
    //first
    $pagination[] = ($currentPage == 1) ? "First" : "<a href='?page=1'>First</a>";
    //previous
    $pagination[] = $previous ? "<a href='?page=$previous'>Previous</a>" : "Previous";
    //current/total
    $pagination[] = "Page $currentPage / $totalPages";
    //next
    $pagination[] = $next ? "<a href='?page=$next'>Next</a>" : "Next";
    //last
    $pagination[] = ($currentPage == $totalPages) ? "Last" : "<a href='?page=$totalPages'>Last</a>";

    //show it
    foreach($pagination as $part){
        echo "<span>$part</span>";
    }
?>
明媚如初 2024-12-12 03:44:59

好的,一般可以这样完成:

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

Good start。要获取页数,您需要计算每页 db/items 中的记录数。

$count = mysql_query("SELECT COUNT(id) FROM tickets"); //yop, it's kind of shortcut
$pages = ceil($count/$itemsPerPage);

好的,生成菜单的页码(假设我们想要像“< 1 ... 3 4 5 ...最后>”->其中$curretnPage = 4):

$menu = array();  //array indexes are strings to make this clearer, it normally should be $menu[] = $sth; to preserve adding order which is important here
if($currentPage > 1) {
     $menu['previous'] = $currentPage - 1;
}
$menu['first'] = 1;
if($currentPage > 3) {
     $menu['dots'] = 0;         
}
else if($currentPage == 3) {
     $menu['oneBeforeCurrent'] = $currentPage - 1;
     $menu['current'] = $currentPage;
}
else if($currentPage == 2) {         
     $menu['current'] = $currentPage;
}
$menu['oneAfterCurrent'] = $currentPage + 1;

...它变得有点复杂,也许有更好、更快的方法,但你可以看到它是如何完成的(不知道它“应该如何完成”)。它并不完整,但是......你知道该怎么做:)
我希望你的意思是,你不能创建菜单...如果你有带有数字的数组,只需循环并创建带有数字的html链接,然后将“...”插入0。

干杯

Ok, generally it may be done like this:

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

Good start. To get the number of pages you need to count records in db/items per page.

$count = mysql_query("SELECT COUNT(id) FROM tickets"); //yop, it's kind of shortcut
$pages = ceil($count/$itemsPerPage);

Ok, generate the page numbers for menu(lets say we want sth like "< 1 ... 3 4 5... last >" -> where $curretnPage = 4):

$menu = array();  //array indexes are strings to make this clearer, it normally should be $menu[] = $sth; to preserve adding order which is important here
if($currentPage > 1) {
     $menu['previous'] = $currentPage - 1;
}
$menu['first'] = 1;
if($currentPage > 3) {
     $menu['dots'] = 0;         
}
else if($currentPage == 3) {
     $menu['oneBeforeCurrent'] = $currentPage - 1;
     $menu['current'] = $currentPage;
}
else if($currentPage == 2) {         
     $menu['current'] = $currentPage;
}
$menu['oneAfterCurrent'] = $currentPage + 1;

... it's getting kind of complicated, maybe there is a better, faster way, but you can see how it can be done(dunno exactly how it "should be done"). It's not complete, but... you know what to do:)
I hope that what you meant, that you can't create menu... If you have array with numbers just loop and create html links with numbers and put "..." insted of 0.

Cheers

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