简体   繁体   中英

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)
  2. Make the get_images as a function

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

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 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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