0

以下に示すphpコードの結果をページネーションする必要があります。いくつかの可能性を試しましたが、役に立ちませんでした。メイン スクリプトを改ざんしないように、いくつかのテスト コードを作成しましたが、目的の結果に最も近いコードは次のとおりです。

<?php
include( "conectiondetails.inc" );
mysql_select_db( $database, $cxn );
$query1 = "SELECT * from table";
$result1 = mysql_query( $query1, $cxn ) or die ( "Couldn't execute query." . mysql_error() );
$row1 = mysql_fetch_array( $result1 );
$category = 'tools';
for( $i = 1; $i <= 3; $i++ ) {
    if( $row1[ 'Category' . "$i" ] == "$category" ) {
        $cat = 'Category' . "$i";
        $ad = 'Placement' . "$i";
        $query2 = "SELECT Email, $ad, $cat FROM table WHERE $cat='$category'";
        $result2 = mysql_query( $query2, $cxn ) or die ( "Couldn't execute query." . mysql_error() );
        $nr = mysql_num_rows( $result2 ); // Get total of Num rows from the database query
        if( isset( $_GET[ 'pn' ] ) ) { // Get pn from URL vars if it is present
            $pn = preg_replace( '#[^0-9]#i', '', $_GET[ 'pn' ] ); // filter everything but numbers for security(new)
            //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
        } else { // If the pn URL variable is not present force it to be value of page number 1
            $pn = 1;
        }
//This is where we set how many database items to show on each page
        $itemsPerPage = 3;
// Get the value of the last page in the pagination result set
        $lastPage = ceil( $nr / $itemsPerPage );
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
        if( $pn < 1 ) { // If it is less than 1
            $pn = 1; // force if to be 1
        } else {
            if( $pn > $lastPage ) { // if it is greater than $lastpage
                $pn = $lastPage; // force it to be $lastpage's value
            }
        }
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
        $centerPages = "";
        $sub1 = $pn - 1;
        $sub2 = $pn - 2;
        $add1 = $pn + 1;
        $add2 = $pn + 2;
        if( $pn == 1 ) {
            $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
            $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
        } else {
            if( $pn == $lastPage ) {
                $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
                $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
            } else {
                if( $pn > 2 && $pn < ( $lastPage - 1 ) ) {
                    $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
                    $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
                    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
                    $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
                    $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
                } else {
                    if( $pn > 1 && $pn < $lastPage ) {
                        $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
                        $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
                        $centerPages .= '&nbsp; <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
                    }
                }
            }
        }
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
        $limit = 'LIMIT ' . ( $pn - 1 ) * $itemsPerPage . ',' . $itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
        $query3 = "SELECT Email, $ad, $cat FROM table WHERE $cat='$category' $limit";
        $result3 = mysql_query( $query3, $cxn ) or die ( "Couldn't execute query." . mysql_error() );
        $paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
        if( $lastPage != "1" ) {
            // This shows the user what page they are on, and the total number of pages
            $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage . '&nbsp;  &nbsp;  &nbsp; ';
            // If we are not on page 1 we can place the Back button
            if( $pn != 1 ) {
                $previous = $pn - 1;
                $paginationDisplay .= '&nbsp;  <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $previous . '"> Back</a> ';
            }
            // Lay in the clickable numbers display here between the Back and Next links
            $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
            // If we are not on the very last page we can place the Next button
            if( $pn != $lastPage ) {
                $nextPage = $pn + 1;
                $paginationDisplay .= '&nbsp;  <a href="' . $_SERVER[ 'PHP_SELF' ] . '?pn=' . $nextPage . '"> Next</a> ';
            }
        }
// Build the Output Section Here
        $outputList = '';
        while( $row = mysql_fetch_array( $result3 ) ) {

            $Email = $row[ "Email" ];
            $Placement1 = $row[ 'Placement' . "$i" ];
            $Category1 = $row[ 'Category' . "$i" ];

            $outputList .= '<h1>' . $Placement1 . '</h1><h2>' . $Category1 . ' </h2><hr />';

        }
    }
}// close while loop
?>

<html>
<head>
    <title>My Pagination</title>
    <style type="text/css">
        <!--
        .pagNumActive {
            color: #000;
            border: #060 1px solid;
            background-color: #D2FFD2;
            padding-left: 3px;
            padding-right: 3px;
        }

        .paginationNumbers a:link {
            color: #000;
            text-decoration: none;
            border: #999 1px solid;
            background-color: #F0F0F0;
            padding-left: 3px;
            padding-right: 3px;
        }

        .paginationNumbers a:visited {
            color: #000;
            text-decoration: none;
            border: #999 1px solid;
            background-color: #F0F0F0;
            padding-left: 3px;
            padding-right: 3px;
        }

        .paginationNumbers a:hover {
            color: #000;
            text-decoration: none;
            border: #060 1px solid;
            background-color: #D2FFD2;
            padding-left: 3px;
            padding-right: 3px;
        }

        .paginationNumbers a:active {
            color: #000;
            text-decoration: none;
            border: #999 1px solid;
            background-color: #F0F0F0;
            padding-left: 3px;
            padding-right: 3px;
        }

        -->
    </style>
</head>
<body>
<div style="margin-left:64px; margin-right:64px;">
    <h2>Total Items: <?php echo $nr; ?></h2>
</div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
<div style="margin-left:64px; margin-right:64px;"><?php print "$outputList"; ?></div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
</body>
</html> 

このコードは、列の一部として Category1、Category2、Category3、Placement1、Placement2 & PLacement3 を含むデータベースをチェックし、Category==tools である Email、Placement & Category を選択することになっています。ただし、上記のコードは、条件が true である最初の列のみを表示し、残りを破棄します (この場合、Placement1、Category1 および対応する Email のみを表示します)。このコードを必要な方法で実行するために他に何をすべきかわかりません。ヘルプ...

4

1 に答える 1

1

2 つのクエリを作成します。1 つはデータの合計数を作成するためのもので、2 つ目は表示するレコードを制限するためのものです。両方のリンクを確認して、その簡単な方法を学習してください。

于 2013-01-09T21:56:10.937 に答える