0

我想在下面的脚本中添加分页,该脚本搜索数据库并将文件夹中的图像输出到行和列中。但我希望输出在页面中,而不是全部在同一页面上。我该怎么做?

<?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>';
?>

我修改了代码,得到了三行三列的图像,页面上的最后一行填充了一个图像。这对于分页链接生成的所有页面都是相同的。它应该在进入下一页之前均匀填满。我可能做错了什么?

修改后的代码如下:

<?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;
?>
4

1 回答 1

0

我建议对你的脚本做一个简单的改变,让我的解释更舒服(因为我不是一个好的程序员!:))。此外,我还创建了一些可以使用的函数。

建议的更改

  1. 对 HTML 进行一些更改(使用 div 并放置一个 CSS 类,并使用 CSS)
  2. 使get_images作为函数

如何使用

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;

以下是分页功能

<?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 代码

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;
}

在文件夹images中根据需要创建两个背景图像:

pagination_bg.jpg /* Normal button image */
pagination_bg_active.jpg /* Active or current page button image */
于 2012-09-16T18:38:03.283 回答