简体   繁体   English

嵌入在PHP中的HTML代码中的分页

[英]Pagination in HTML code embeded in PHP

I want to add pagination to the script below which searches the database and outputs the images in a folder into rows and columns. 我想在下面的脚本中添加分页,该脚本搜索数据库并将文件夹中的图像输出成行和列。 But I want the output to be in pages, not all on the same page. 但我希望输出在页面中,而不是全部在同一页面上。 How can I do it? 我该怎么做?

<?php
    echo '<table border="0" cellpadding="15" cellspacing="15">';
    $getImages = mysql_query("SELECT * FROM yaamembers");
    if(!$getImages)
        die("Cannot execute query. " . mysql_error());
    $countRows = mysql_num_rows($getImages);
    $i = 0;
    if ($countRows > 0)
    {
        while ($row_rsYaamembers = mysql_fetch_assoc($getImages))
        {
            if ($i % 3 == 0) echo ($i > 0? '</tr>' : '') . '<tr>';
                echo '<td valign="top"><a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"><img src="/home/youngatart/yaamembers/'.$row_rsYaamembers['photo'].'" border="0" width="120" /></td>';
            if ($i == $countRows - 1)
                echo '</tr>';
            $i++;
        }
    }
    echo '</table>';
?>

I have modified the code and I get three rows of three columns of images and the last row on the page filled with one image. 我修改了代码,得到三行三列的图像,页面的最后一行充满了一张图像。 This is the same for all the pages generated by the pagination links. 对于分页链接生成的所有页面都是相同的。 Its supposed to fill up evenly before going to the next page. 它应该在下一页之前均匀填满。 What could i be doing wrong? 我可能做错了什么?

Modified code below: 修改后的代码如下:

<?Php
    $getImages = mysql_query("SELECT * FROM yaamembers");
    if(!$getImages)
        die("Cannot execute query. " . mysql_error());

    $output = "";

    $getImages = get_images($start,$limit);
    if ($getImages && mysql_num_rows($getImages) > 0)
    {
        /* Pagination section2 */
        $getAllImages = get_images();
        $total_items = mysql_num_rows($getAllImages);
        $paginate = paginate($targetpage,$total_items,$limit,$pagenum);
        $paginate = trim($paginate);
        /* Pagination section2 End */

        echo '<table border="0" cellpadding="15" cellspacing="15">';
        $countRows = mysql_num_rows($getImages);
        $i = 0;
        if ($countRows > 0)
        {
            while ($row_rsYaamembers = mysql_fetch_assoc($getImages))
            {
                if ($i % 3 == 0) echo ($i > 0) . '<tr>';
                    echo '<td valign="top"><a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"><img src="http://localhost/youngatart/yaamembers/'.$row_rsYaamembers['photo'].'" border="0" width="120" /></a><div id="text2"><div class="clear_4"></div><div align="center" id="text2"><a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"><div align="center" id="text2"> '.$row_rsYaamembers['school'].'</a></div><div class="clear_4"></div><div align="center" id="text2"><a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '"> '.$row_rsYaamembers['year'].'</a></div></div></td>';
                if ($i == $countRows - 1)
                    echo '</tr>';
                $i++;
            }
        }
        echo '</table>';

        $output .= $paginate;
    }
    echo $output;
?>

I suggest a simple change in your script, to make it comfortable for my explanations (because I'm not a good programmer! :) ). 我建议您对脚本进行简单的更改,以使其易于理解(因为我不是一个好的程序员!:))。 Also, I have created some functions to work with. 另外,我创建了一些要使用的功能。

Suggested changes 建议的变更

  1. Make some changes in HTML (use div and put a CSS class, and play with CSS) 在HTML中进行一些更改(使用div并放置CSS类,然后使用CSS进行播放)
  2. Make the get_images as a function 使get_images作为一个函数

How to use 如何使用

function get_images($start=0,$limit=0)
{
    mysql_connect("host","user","pass")or die("Cannot connect DB");
    mysql_select_db("db_name")or die("DB does not exist");
    $sql = "SELECT * FROM yaamembers";

    if($start!=0 || $limit!=0)
    {
        $sql .= " LIMIT ". $start .", ". $limit;
    }

    $sql .= ";";

    $data = mysql_query($sql);
    mysql_close();
    return $data;
}
/* Pagination section1 */
$pagenum = 1;
$limit = 10; /* Items per page*/
$start = 0;
if(isset($_GET['pagenum'])) {
    $pagenum = $_GET['pagenum'];
}
$url = get_current_url();
$targetpage = format_url($url, "pagenum");
if($pagenum)
{
    $start = ($pagenum - 1) * $limit;
}
/* Pagination section1 End */

$output = "";

$getImages = get_images($start,$limit)
if ($getImages && mysql_num_rows($getImages) > 0)
{
    /* Pagination section2 */
    $getAllImages = get_images();
    $total_items = mysql_num_rows($getAllImages);
    $paginate = paginate($targetpage,$total_items,$limit,$pagenum);
    $paginate = trim($paginate);
    /* Pagination section2 End */

    while ($row_rsYaamembers = mysql_fetch_assoc($getImages))
    {
        $output .= "
            <div>
                <a target="_blank" href="yimagelarge.php?yaaid=' . $row_rsYaamembers['yaaID'] . '">
                <img src="/home/youngatart/yaamembers/'.$row_rsYaamembers['photo'].'" border="0" width="120" />
                </a>
            </div>";
    }
    $output .= $paginate;
}
echo $output;

Here follows the functions to paginate 以下是分页功能

<?php
    function paginate($targetpage,$total_items=0,$limit=10,$pagenum=1)
    {
        /*
        * Remember to remove pagenum variable from $targetpage variable
        */
        $adjacents = 3; /* How many items adjascent to page link */
        /* How many items to show per page $limit = 30; */

        /* Setup page vars for display. */
        if ($pagenum == 0)
            $pagenum = 1;                             /* If no page var is given, default to 1. */
        $prev = $pagenum - 1;                         /* Previous page is page - 1 */
        $next = $pagenum + 1;                         /* Next page is page + 1 */
        $lastpage = ceil($total_items/$limit);        /* Last page is equal to total pages / items per page, rounded up. */
        $lpm1 = $lastpage - 1;                        /* Last page minus 1 */
        /*
            Now we apply our rules and draw the pagination object.
            We're actually saving the code to a variable in case we want to draw it more than once.
        */
        $pagination = "";
        if($lastpage > 1)
        {
            $pagination .= "<div class=\"pagination\">";
            /* previous button */
            if ($pagenum > 1)
            {
                $pagination .= "<a href=\"$targetpage&pagenum=$prev\">Previous</a>";
            }
            else
            {
                $pagination .= "<span class=\"disabled\">Previous</span>";
            }

            /* Pages */
            if ($lastpage < 7 + ($adjacents * 2))  /* Not enough pages to bother breaking it up. */
            {
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $pagenum)
                    {
                        $pagination .= "<span class=\"current\">$counter</span>";
                    }
                    else
                    {
                        $pagination .= "<a href=\"$targetpage&pagenum=$counter\">$counter</a>";
                    }
                }
            }
            elseif($lastpage > 5 + ($adjacents * 2))    /* Enough pages to hide some */
            {
                /* Close to beginning; only hide later pages */
                if($pagenum < 1 + ($adjacents * 2))
                {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                    {
                        if ($counter == $pagenum)
                        {
                            $pagination .= "<span class=\"current\">$counter</span>";
                        }
                        else
                        {
                            $pagination .= "<a href=\"$targetpage&pagenum=$counter\">$counter</a>";
                        }
                    }
                    $pagination .= "...";
                    $pagination .= "<a href=\"$targetpage&pagenum=$lpm1\">$lpm1</a>";
                    $pagination .= "<a href=\"$targetpage&pagenum=$lastpage\">$lastpage</a>";
                }
                /* In middle; hide some front and some back */
                elseif($lastpage - ($adjacents * 2) > $pagenum && $pagenum > ($adjacents * 2))
                {
                    $pagination .= "<a href=\"$targetpage&pagenum=1\">1</a>";
                    $pagination .= "<a href=\"$targetpage&pagenum=2\">2</a>";
                    $pagination .= "...";
                    for ($counter = $pagenum - $adjacents; $counter <= $pagenum + $adjacents; $counter++)
                    {
                        if ($counter == $pagenum)
                        {
                            $pagination .= "<span class=\"current\">$counter</span>";
                        }
                        else
                        {
                            $pagination .= "<a href=\"$targetpage&pagenum=$counter\">$counter</a>";
                        }
                    }
                    $pagination .= "...";
                    $pagination .= "<a href=\"$targetpage&pagenum=$lpm1\">$lpm1</a>";
                    $pagination .= "<a href=\"$targetpage&pagenum=$lastpage\">$lastpage</a>";
                }
                /* close to end; only hide early pages */
                else
                {
                    $pagination .= "<a href=\"$targetpage&pagenum=1\">1</a>";
                    $pagination .= "<a href=\"$targetpage&pagenum=2\">2</a>";
                    $pagination .= "...";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $pagenum)
                        {
                            $pagination .= "<span class=\"current\">$counter</span>";
                        }
                        else
                        {
                            $pagination .= "<a href=\"$targetpage&pagenum=$counter\">$counter</a>";
                        }
                    }
                }
            }

            /* Next button */
            if ($pagenum < $counter - 1)
            {
                $pagination .= "<a href=\"$targetpage&pagenum=$next\">next</a>";
            }
            else
            {
                $pagination .= "<span class=\"disabled\">next</span>";
            }
            $pagination .= "</div>\n";
        }
        return $pagination;
    }

    function format_url($url, $fileds)
    {
        /*-To remove queries from URL, the field input may be single string or an array of strings. */
        $url_filed_array = explode("&",$url);
        $return_url = '';
        for($i=0; $i<count($url_filed_array); $i++)
        {
            if(is_array($fileds))
            {
                if($i==0)
                {
                    $_url = explode('?',$url_filed_array[$i]);
                    $return_url .=$_url[0] .'?';
                    $url_filed = explode('=',$_url[1]);
                }
                else
                {
                    $url_filed = explode('=',$url_filed_array[$i]);
                }
                if(!in_array($url_filed[0],$fileds))
                {
                    if($i==0 && isset($_url[1]))
                    {
                        $return_url .=$_url[1];
                    }
                    else
                    {
                        $return_url .=$url_filed_array[$i];
                    }
                    if(($i+1) != count($url_filed_array))
                    {
                        $return_url .="&";
                    }
                }
            }
            else
            {
                if($i==0)
                {
                    $_url = explode('?',$url_filed_array[$i]);
                    $return_url .=$_url[0] .'?';
                    $url_filed = explode('=',$_url[1]);
                }
                else
                {
                    $url_filed = explode('=',$url_filed_array[$i]);
                }
                if($url_filed[0]!=$fileds)
                {
                    if($i==0 && isset($_url[1]))
                    {
                        $return_url .=$_url[1];
                    }
                    else
                    {
                        $return_url .=$url_filed_array[$i];
                    }
                    if(($i+1) != count($url_filed_array))
                    {
                        $return_url .="&";
                    }
                }
            }
        }
        if(substr($return_url,-1)=='&')
        {
            $return_url = substr($return_url, 0, -1);
        }
        if(substr($return_url,-1)=='?')
        {
            $return_url = substr($return_url, 0, -1);
        }
        return $return_url;
    }

    function get_current_url()
    {
        $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
        if ($_SERVER["SERVER_PORT"] != "80")
        {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        }
        else
        {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }
?>

CSS code CSS代码

div.pagination {
    padding:3px;
    margin:3px;
    text-align:center;
}

div.pagination span.disabled ,
div.pagination span.current ,
div.pagination a
{
    background:url('../images/pagination_bg.jpg') #ffffff repeat-x right center;
    height: 28px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    padding: 5px 8px;
    margin-right: 8px;
    color: #6B6868;
}

div.pagination a {
    border: 1px solid #ddd;
    text-decoration: none;
}
div.pagination a:hover, div.pagination a:active {
    border:1px solid #f2813a;
    color: #f2813a;
    background-color: #F46F1B;
}
div.pagination span.current {
    border: 1px solid #f2813a;
    font-weight: bold;
    color: #FFF;
    background:url('../images/pagination_bg_active.jpg') #F46F1B repeat-x right center;
}
div.pagination span.disabled {
    border: 1px solid #f3f3f3;
    color: #ccc;
}

div.pagination span.current,
div.pagination span.disabled {
    cursor: default;
}

Create two background images as you like, in folder images : 根据需要在文件夹图像中创建两个背景图像

pagination_bg.jpg /* Normal button image */
pagination_bg_active.jpg /* Active or current page button image */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM