drmet46444 2016-06-18 21:04
浏览 98

简单的php分页或用php / mysql进行css分页

I am trying to built a simple php pagination ( a sort of ). or to be more specific css pagination with php/mysql.

Accessing and storing the values from db

 while($row = $result->fetch_assoc()) {
        $id[]=$row["id"];
        $name[]=$row["name"];
        $url[]=$row["url"];
        //echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["url"]. "<br>";
    }

Displaying them

    <ul class="pagination">
        <li><a href="#">«</a></li>
        <?php 
         $i=1;
        for ($x = 0; $x < count($id); $x++) { ?>
        <li>
         <a href=<?php echo "$url[$x]"; ?> target="iframe1"
 title="<?php echo "$name[$x]"; ?>"><?php echo "$i";?></a> 

         </li>
        <?php   $i++;}   ?>


        <li><a href="#">»</a></li>
      </ul>

Once any number shown above is clicked required url is displayed inside iframe1

 <div align="center">
    <iframe name="iframe1" src="http://www.w3schools.com"
 frameborder='0'  height='1000'   width='1000' align="center"></iframe>
    </div>

when I click any number it display the url inside the iframe "iframe1" .

Question:

How do i display first and NEXT {NOT LAST} record in

  <li><a href="#">«</a></li>  [First]
  <li><a href="#">»</a></li>  [Next]

I am looking for a very small code to do the same.

  • 写回答

1条回答 默认 最新

  • doumu6997 2016-06-18 21:52
    关注

    The first link is pretty easy:

    <a href=<?php echo "$url[0]"; ?> target="iframe1" 
        title="<?php echo $name[0]"; ?>"><<</a>
    

    For next link It would kind of depend on how your urls are set out. If the $url values are just a name and your url is domain.com/blog?page=page1 where page1 is the value from $url then we get use $_GET['page'] to grab the current page. Then using:

    $next = array_search($_GET['page'], $url);
    

    will give us the number in the order of the current page so the next page will simply be $url[$next + 1]

    EDIT:

    OK if its a url that changes in domain part then the $_GET wont work. You could grab the current url using $_SERVER[REQUEST_URI] and then use the same technique as above to find its position in the array of urls.

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害