如何在使用PHP的两个不同查询的单个页面中创建两个分页?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何在使用PHP的两个不同查询的单个页面中创建两个分页?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
本周刚刚开始学习 PHP,我会在此寻找可能的解决方案,但我可以做对.虽然我成功地创建了一个按字母顺序对查询进行排序的分页,但我没有成功创建数字分页,其中它将限制要显示查询数.此外,我无法将这两种类型的分页与两个不同的查询相结合,一个用于A-Z排序,另一个用于在单个页面显示查询数量.
这是我的代码

`<?PHP
        include 'includes/connection.PHP';

        $character = '';
        $characters = '';



        if (isset($_GET['character']))
        {
            $character = $_GET['character'];
               $character = PReg_replace('#[^a-z]#i','',$character);
            $sql = "SELECT * From drivers_info WHERE last_name LIKE '$character%' ORDER BY last_name";
       }
        else{

            $sql = "SELECT * From drivers_info ORDER BY rfid ";
            $pageno = 1 
        }
        $result = MysqLi_query($db,$sql);

    <body>
            <br /><br />
            <br /> <br />

这是创建按字母顺序排列的分区的代码

<div class="table-responsive">
                <div align="center">
                 <?PHP
                 $character = range('A','Z'); 
                 echo '<ul class="pagination">';
                 foreach($character as $alphabet)
                 {
                     echo '<li><a href = "users.PHP?character='.$alphabet.'">'.$alphabet.'</a></li>';
                 }
                 echo '</ul>';
                 ?>
                 </div>
          <?PHP

这是按字母顺序显示查询代码

if(MysqLi_num_rows($result) > 0)
            {
                    while($row = MysqLi_fetch_array($result)){

            ?> 
            <tr>
                <td><?PHP echo $row["rfid"]; ?></td>
                <td><?PHP echo $row["last_name"]; ?></td>
                <td><?PHP echo $row["First_name"]; ?></td>
                <td><?PHP echo $row["middle_name"]; ?></td>
                <td><?PHP echo $row["gender"]; ?></td>
                <td><?PHP echo $row["age"]; ?></td>
                <td><?PHP echo $row["height"]; ?></td>
                <td><?PHP echo $row["weight"]; ?></td>
                <td><?PHP echo $row["address"]; ?></td>
                <td><?PHP echo $row["contact_no"]; ?></td>
                <td><?PHP echo $row["license_tyPE"]; ?></td>
                <td><?PHP echo $row["license_no"]; ?></td>
                <td><?PHP echo $row["expiration"]; ?></td>
                <td><?PHP echo $row["nationalITy"]; ?></td>
                <td><?PHP echo $row["eyes_color"]; ?></td>
                <td><?PHP echo $row["blood_type"]; ?></td>
            </tr>
            <?PHP
                    }
            }



</body>

如果你有任何代码或想法,你介意与我分享吗?非常感谢你,我需要这个来完成我的学校项目.

`

解决方法

这是理解分页逻辑的最佳参考代码

<?PHP

$host = "localhost";
$user = "root";
$pass = "New";
$db = "Booksgood";

// open connection
$connection = MysqL_connect($host,$user,$pass) or die ("Unable to connect!");

// select database
MysqL_select_db($db) or die ("Unable to select database!"); 

// how many rows to show per page
$rowsPerPage = 10;

// by default we show first page
$page_num = 1;

// if $_GET['page'] defined,use it as page number,$_GET gets the page number out of the url
//set by the $page_pagination below
if(isset($_GET['page'])){$page_num = $_GET['page'];}

//the point to start for the limit query
$offset = $page_num; 

// Zero is an incorrect page,so switch the zero with 1,mainly because it will cause an error with the sql
if($page_num == 0) {$page_num = 1;}

// counting the offset
$sql = "SELECT * FROM bookdata where titles like 's%' order by titles LIMIT $offset,$rowsPerPage ";
$res = MysqL_query($sql) or die(MysqL_error());

// how many rows we have in database
$sql2  = "SELECT COUNT(id) AS numrows FROM bookdata where titles like 's%'";
$res2  = MysqL_query($sql2) or die(MysqL_error());
$row2  = MysqL_fetch_array($res2);
$numrows = $row2['numrows'];

// print the random numbers
while($row = MysqL_fetch_array($res))
{
//Echo out your table contents here.

echo $row[1].'<BR>';
echo $row[2].'<BR>';
echo '<BR>';
}

// how many pages we have when using paging?
$numofpages = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = "/paging3.PHP?";

if ($numofpages > '1' ) {

            $range =15; //set this to what ever range you want to show in the pagination link
            $range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
            $range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
            $page_min = $page_num- $range_min;
            $page_max = $page_num+ $range_max;

            $page_min = ($page_min < 1) ? 1 : $page_min;
            $page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
            if ($page_max > $numofpages) {
                $page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
                $page_max = $numofpages;
            }

            $page_min = ($page_min < 1) ? 1 : $page_min;

            //$page_content .= '<p class="menuPage">';

            if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) {
                $page_pagination .= '<a class="num"  title="First" href="'.$self.'page=1">&amp;lt;</a> ';
            }

            if ($page_num != 1) {
                $page_pagination .= '<a class="num" href="'.$self.'page='.($page_num-1). '">PrevIoUs</a> ';
            }

            for ($i = $page_min;$i <= $page_max;$i++) {
                if ($i == $page_num)
                $page_pagination .= '<span class="num"><strong>' . $i . '</strong></span> ';
                else
                $page_pagination.= '<a class="num" href="'.$self.'page='.$i. '">'.$i.'</a> ';
            }

            if ($page_num < $numofpages) {
                $page_pagination.= ' <a class="num" href="'.$self.'page='.($page_num + 1) . '">Next</a>';
            }


            if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) {
                $page_pagination .= ' <a class="num" title="Last" href="'.$self.'page='.$numofpages. '">&gt;</a> ';
            }

            //$page['PAGINATION'] ='<p id="pagination">'.$page_pagination.'</p>';
        }//end if more than 1 page 

echo $page_pagination.'<BR><BR>';

echo 'Number of results - '.$numrows ;
echo ' and Number of pages   - '.$numofpages.'<BR><BR>';

// Free resultset
MysqL_free_result($res);

// and close the database connection
MysqL_close($con); 

?>

这里是参考代码link

脚本宝典总结

以上是脚本宝典为你收集整理的如何在使用PHP的两个不同查询的单个页面中创建两个分页?全部内容,希望文章能够帮你解决如何在使用PHP的两个不同查询的单个页面中创建两个分页?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。