php分页有一些错误

时间:2018-03-25 04:30:26

标签: php pagination

我正在尝试实现分页。我想要像

一样
$sql2 = "select * from phptest.memo order by num desc limit $start,$start+$scale"; 

和我用段落显示备忘录和涟漪(涟漪数据库有父字段,说明什么数量的备忘录和它的订单号为主要字段)但我认为它似乎输入了错误的代码我该怎么办?这是我想要的形象

预期结果: Image that when it's finished assume

错误: Error

    <?php 
    session_start(); 
       // echo "<a href='memo.php?page=$i'> $i </a>";

    $scale=5;           // page per writing
        // start recored number newest writing is up
        // $page 값에 다른 시작 레코드 넘버값, 가장 높은 글번호(최신글 부터 밑으로) 
    $start = ($page - 1) * $scale;      //0,5,10 ...
        if(!isset($_REQUEST["page"]))
        {
        $page = 1;                      // initialize seeing page
        }else{
            $page = $_REQUEST["page"];
        }
        require_once '../lib/dbconn.php';
        $pdo = db_connect();

        if(isset($_SESSION["userid"])){
        $userid = $_SESSION["userid"];
        }else{
            $userid = "";
        }

        // start recored number newest writing is up


        try{
            //$sql = "select * from phptest.memo order by num desc";
            $sql2 = "select * from phptest.memo order by num desc limit         $start,$start+$scale";
            $stmh = $pdo->query($sql2);
        } catch (PDOException $ex) {`enter code here`
            print "오류: ".$ex->getMessage();
        }
        $total_record = $stmh->rowCount();


    $number = $total_record - $start;

        // 전체 페이지 수 계산..
    if ($total_record % $scale == 0)     
        $total_page = floor($total_record/$scale);      
    else
        $total_page = floor($total_record/$scale) + 1; 

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> 
<meta charset="utf-8">
<link href="../css/common.css" rel="stylesheet" type="text/css" media="all">
<link href="../css/memo.css" rel="stylesheet" type="text/css" media="all">
</head>

<body>
<div id="wrap">
  <div id="header">
    <?php include "../lib/top_login2.php"; ?>
  </div>  <!-- end of header -->

  <div id="menu">
    <?php include "../lib/top_menu2.php"; ?>
  </div>  <!-- end of menu --> 

  <div id="content">    
    <div id="col1">
        <div id="left_menu">
<?php
            include "../lib/left_menu.php";
?>
        </div>
    </div>
    <div id="col2">  
        <div id="title">
            <img src="../img/title_memo.gif">
        </div>
                <?php
//                if(isset($_SESSION["userid"])){
                    ?>

        <div id="memo_row1">
        <form  name="memo_form" method="post" action="insert.php">
            <?php if(isset($_SESSION['nick'])){ ?>
            <div id="memo_writer"><span >▷ <?=$_SESSION['nick'] ?> </span></div>  
            <?php }?>
            <div id="memo1"><textarea rows="6" cols="95" name="content"></textarea></div>
            <div id="memo2"><input type="image" src="../img/memo_button.gif"></div>
        </form> 
        </div> <!-- end of memo_row1 -->
                <?php // }      
   while ($row = $stmh->fetch(PDO::FETCH_ASSOC))        

   {

//          $stmh = $pdo->query($sql2);


                  //$row[$i] = $row;
//          $row = $stmh2->fetch(PDO::FETCH_ASSOC);


      $memo_id      = $row['id'];
      $memo_num     = $row['num'];
          $memo_date    = $row['regist_day'];
      $memo_nick    = $row['nick'];
          $memo_content = $row['content'];

      $memo_content = str_replace("\n", "<br>", $row['content']);
      $memo_content = str_replace(" ", "&nbsp;", $memo_content);
?>
        <div id="memo_writer_title">
        <ul>
        <li id="writer_title1"><?= $memo_num ?></li> <!--article number -->
        <li id="writer_title2"><?= $memo_nick ?></li> 
        <li id="writer_title3"><?= $memo_date ?></li>
        <li id="writer_title4"> 
              <?php 
                    if($userid=="admin" || $userid==$memo_id)
                      echo "<a href='delete.php?num=$memo_num'>[삭제]</a>"; 
              ?>
        </li>
        </ul>
        </div>
        <div id="memo_content"><?= $memo_content ?>
        </div>
        <div id="ripple"> 
            <div id="ripple1">덧글</div>
            <div id="ripple2">
<?php
        $sql3 = "select * from phptest.memo_ripple where parent=$memo_num";
        $stmh3 = $pdo->query($sql3);

        while ($row_ripple = $stmh3->fetch(PDO::FETCH_ASSOC))
        {
            $ripple_num     = $row_ripple["num"];
            $ripple_id      = $row_ripple["id"];
            $ripple_nick    = $row_ripple["nick"];
            $ripple_content = str_replace("\n", "<br>", $row_ripple["content"]);
            $ripple_content = str_replace(" ", "&nbsp;", $ripple_content);
            $ripple_date    = $row_ripple["regist_day"];
?>
                <div id="ripple_title">
                <ul>
                <li><?= $ripple_nick ?> &nbsp;&nbsp;&nbsp; <?= $ripple_date ?></li>
                <li id="mdi_del">
                    <?php 
                        if($userid=="admin" || $userid==$ripple_id)
                            echo "<a href='delete_ripple.php?num=$ripple_num'>삭제</a>";
                    ?>
                </li>
                </ul>
                </div>
                <div id="ripple_content"> <?= $ripple_content ?></div>
<?php
        }
                           if(isset($_SESSION["userid"])){
?>
                <form  name="ripple_form" method="post" action="insert_ripple.php"> 
                <input type="hidden" name="num" value="<?= $memo_num ?>"> 
                <div id="ripple_insert">
                    <div id="ripple_textarea">
                        <textarea rows="3" cols="80" name="ripple_content"></textarea>
                    </div>
                    <div id="ripple_button"><input type="image" src="../img/memo_ripple_button.png"></div>
                </div>
                </form>
                 <?php } ?>
            </div> <!-- end of ripple2 -->
                <div class="clear"></div>
            <div class="linespace_10"></div>
<?php
        //$number--;
          }


?>
            <div id="page_num"> ◀ 이전 &nbsp;&nbsp;&nbsp;&nbsp; 
<?php
   // page link num
   // 게시판 목록 하단에 페이지 링크 번호 출력
   for ($i=1; $i<=$total_page; $i++)
   {
        if ($page == $i)     // ���� ������ ��ȣ ��ũ ����
        {
            echo "<b> $i </b>";
        }
        else
        { 
             echo "<a href='memo.php?page=$i'> $i </a>";
        }      
   }
?>          
            &nbsp;&nbsp;&nbsp;&nbsp;다음 ▶</div>
         </div> <!-- end of ripple -->
    </div> <!-- end of col2 -->
  </div> <!-- end of content -->
</div> <!-- end of wrap -->

</body>
</html>

1 个答案:

答案 0 :(得分:0)

SQL不喜欢在limit子句中进行数学运算。首先在PHP中执行:

$sql2 = "select * from phptest.memo order by num desc limit $start," . ($start+$scale);