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

コードを修正したところ、3 行 3 列の画像が表示され、ページの最後の行には 1 つの画像が表示されます。これは、ページネーション リンクによって生成されるすべてのページで同じです。次のページに進む前に、均等にいっぱいになるはずです。私は何が間違っているのでしょうか?

以下の変更されたコード:

<?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に、好きなように 2 つの背景画像を作成します。

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