没有页面加载的PHP分页

时间:2013-06-17 08:53:17

标签: php pagination

我有一个php主页,其中包含不同div中的其他php页面。我想在一个div中应用pagenation,并且只在选择页面时加载该div。问题是我的输出不在数据库中,而且我正在使用控制台应用程序的输出,即我将在控制台应用程序执行后获得所有数据。每次更改页面时都不可能调用控制台应用程序,因为执行它需要10秒以上。我想我应该使用ajax,但我不知道在哪里使用它。主页面使用ajax显示输出显示的div和要进行分页的位置。我发布我的代码只是为了说清楚。

主页:

<html>
    <head>

         <script type="text/javascript">

                    function myfunction()
                    {
                      $("#display").html('');
                            $("#retrieveimageform").ajaxForm(
                    {
                    target: '#display'
                    }).submit();

                    };

        </script>
        <title></title>
    </head>
    <body>
        <div id="maindiv">

        <div id="right">
        <div id="grpbox">
                 <form action="retrieveimage1.php" method="post"
                       id="retrieveimageform">
                 <div id='type'>  
                        <fieldset id="typefield" name="typebox">
                        <legend>
                          <label>
                            Type
                          </label>
                        </legend>
                        <p><input type="radio" id="shoes" name="cat" value="shoes"><label for="shoes">Shoes: </label></input><br/>
                        <input type="radio" id="dress" name="cat" value="dress"><label for="dress">Dress: </label></input><br/>
                        <input type="radio" id="handbag" name="cat" value="handbag"><label for="handbag">Handbag: </label></input></p>
                      </fieldset>   
                 </div>
                </div>  
                     <div id="button">
                     <input type="submit" id="btnSearch" value="Search" class="button" onclick="myfunction()">
                     </div>

                </form>
        </div>

              <div id="display"></div>
            </div>
    </div>.....

retrieveimageform1.php,其中显示输出并显示在上面的显示div中。我想在此div中进行分页,而不加载整个页面。下面的代码显示了页面加载的分页,但它不适合我的情况,所以如何使用它来进行页面加载分页。

<?php

                        $data=array();
                          session_start();
                        $start_time=  microtime(true);
                if (isset($_SESSION['img']))
                {   
                        $image=$_SESSION['img'];


                            if (isset($_POST['cat']))
                            {
                               if($_POST['cat']=='handbag')
                                {

                                    $addr="/home/fashion/fashion_proj/handbagsfinal $image $cwt $fwt $swt $twt";
                                     exec($addr,$data);
                                       echo "<div id='tags'>";
                    echo ...................
                                      }
                                      echo "</div>";
                    echo "<fieldset id='outputfield' name='output' class='field_set'><legend><label>Similar items</label></legend>";

                    // find out how many rows are in the table 

                    $numrows = 50;

                    // number of rows to show per page
                    $rowsperpage = 10;
                    // find out total pages
                    $totalpages = ceil($numrows / $rowsperpage);

                    // get the current page or set a default
                    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
                       // cast var as int
                       $currentpage = (int) $_GET['currentpage'];
                    } else {
                       // default page num
                       $currentpage = 1;
                    } // end if

                    // if current page is greater than total pages...
                    if ($currentpage > $totalpages) {
                       // set current page to last page
                       $currentpage = $totalpages;
                    } // end if
                    // if current page is less than first page...
                    if ($currentpage < 1) {
                       // set current page to first page
                       $currentpage = 1;
                    } // end if

                    // the offset of the list, based on current page 
                    $c=12*$currentpage; datas start from in first page 12

                    echo "<div id='imgdisplay'>";
                                        ....... echo datas to be dispalyed

                    /******  build the pagination links ******/
                    // range of num links to show
                    $range = 3;

                    // if not on page 1, don't show back links
                    if ($currentpage > 1) {
                       // show << link to go back to page 1
                       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
                       // get previous page num
                       $prevpage = $currentpage - 1;
                       // show < link to go back to 1 page
                       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
                    } // end if 

                    // loop to show links to range of pages around current page
                    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
                       // if it's a valid page number...
                       if (($x > 0) && ($x <= $totalpages)) {
                          // if we're on current page...
                          if ($x == $currentpage) {
                         // 'highlight' it but don't make a link
                         echo " [<b>$x</b>] ";
                          // if not current page...
                          } else {
                         // make it a link
                         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
                          } // end else
                       } // end if 
                    } // end for

                    // if not on last page, show forward and last page links        
                    if ($currentpage != $totalpages) {
                       // get next page
                       $nextpage = $currentpage + 1;
                        // echo forward link for next page 
                       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
                       // echo forward link for lastpage
                       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
                    } // end if
                    /****** end build pagination links ******/


                    echo "</fieldset>";

..........

我应该在主页面或retreive页面的哪些地方应用ajax任何有用的链接,tutorail或建议?

0 个答案:

没有答案