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

PHP中嵌入的HTML代码中的分页,php,image,pagination,Php,Image,Pagination,我想在下面的脚本中添加分页,该脚本搜索数据库并将文件夹中的图像输出到行和列中。但我希望输出是以页为单位的,而不是全部在同一页上。我怎么做 <?php echo '<table border="0" cellpadding="15" cellspacing="15">'; $getImages = mysql_query("SELECT * FROM yaamembers"); if(!$getImages) die("Cannot exe

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

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

我建议对您的脚本做一个简单的更改,以便于我解释(因为我不是一个好的程序员!)。此外,我还创建了一些要使用的函数

建议的修改

  • 在HTML中进行一些更改(使用div并放置一个CSS类,然后播放 (使用CSS)
  • get_images
    作为一个函数
  • 如何使用 在“图像”文件夹中,根据需要创建两个背景图像:


    使用搜索,您将对所发现的内容感到惊讶。这里有很多关于分页的问题和答案。谢谢你的回答。我对php也不是很在行。我有点迷路了。是的,请输入css和cometic描述。但从我所能收集到的信息来看,第二部分我必须在单独的页面上创建,并将其包含在图库页面中。对于第一部分,我是否将所有这些代码放在我希望图库显示的页面部分,记住我有一个横幅和一个侧面部分。将
    get_images()
    函数从第一部分和所有第二部分代码(即“Paginate functions”)保存到一个文件中,您可以在任何需要的地方包含这些代码。您可以使用代码
    $output=”“
    echo$output
    in gallery.php@Navas:谢谢。我会尝试一下,并给你一个结果的更新。@Navas:我在下面这一行发现一个语法错误,我似乎找不到错误所在。如果(!$getImages)死亡(“无法执行查询。”.mysql_error())@纳瓦斯:谢谢。我在这一行得到一个错误,{$pagination.=''.get_lang_word('previous')。'';},即.get_lang_word是一个未定义的函数。我查看了代码,发现它没有被定义。请问.get_lang_单词的功能是什么?
    <?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;
    ?>
    
    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;
        }
    ?>
    
    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;
    }
    
    pagination_bg.jpg /* Normal button image */
    pagination_bg_active.jpg /* Active or current page button image */