每次单击分页链接(下一个,上一个)时生成的查询字符串,帮助

时间:2009-10-30 07:24:17

标签: php html css

我正在尝试实现分页,我这样做了。问题是当我单击搜索按钮时,会显示搜索结果 没问题。

1st Snap http://www.flickr.com/photos/41695354@N08/4057149857/

第二次拍摄 http://www.flickr.com/photos/41695354@N08/4057149861/

当我点击下一个按钮时,它再次从默认情况下为空的所有文本框中获取值,并将查询设置为 select * from rental_ads where city =“”,这是0结果,因此显示'No Results Found'。

第三次拍摄 http://www.flickr.com/photos/41695354@N08/4057149863/

如何仅在单击搜索按钮时才能使查询字符串形成?

任何帮助都将受到高度赞赏。

我的代码:

rentals_search.template.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="../css/style.css"> </link>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RBlog - For the Consumers - By the Sellers</title>



<link href="css/Pager.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
    <script src="js/jquery.pager.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">

        $(document).ready(function() {
            $("#pager").pager({ pagenumber: 1, pagecount: 15, buttonClickCallback: PageClick });
        });

        PageClick = function(pageclickednumber) {
            $("#pager").pager({ pagenumber: pageclickednumber, pagecount: 10, buttonClickCallback: PageClick });
            //$("#result").html("Clicked Page " + pageclickednumber);
            $("#result").html("Clicked Page " + pageclickednumber);



        }
        gen();

    </script>





</head>

<body>
    <?php include("includes/header.php"); ?>
    <?php include("dbclass/paging_class.php"); ?>
    <div class="afterpicer_total">
        <?php include("includes/menu.php"); ?>
    <div class="wrapper">
            <div class="cont">
                <?php include("includes/left_menu.php"); ?>
                <div class="reg_cont">
                    <form action="search_results.php" method="GET">
                        <div class="reg_label">Looking for</div>
                        <div class="reg_tbox">
                            <select name="type" class="reg_combo_style">
                                <option>Living House</option>
                                <option>Office</option>
                            </select>
                        </div><br/>
                        <div class="reg_label">Rent/Month</div>
                        <div class="reg_tbox">
                            <select name="rent" class="reg_combo_style">
                                <option></option>
                                <option><2000</option>
                                <option>2000-4000</option>
                                <option>4000-6000</option>
                                <option>6000-10000</option>
                                <option>>10000</option>
                            </select>
                        </div><br/>
                        <div class="reg_label" >City</div>

                        <div class="reg_tbox">
                            <select name="city" class="reg_combo_style" >
                                <option></option>
                                <option>Chennai</option>
                                <option>Salem</option>
                                <option>Madurai</option>
                                <option>Trichy</option>
                            </select>
                        </div><br/>
                        <div class="reg_label" >Area</div>
                        <div class="reg_tbox"><input type="text" size="32" name="area" class="reg_tbox_style" value='<?php echo(htmlspecialchars($_SESSION["lastarea"], ENT_QUOTES)); ?>' ></input></div><br/><br/><br/>



                        <div class="reg_tbox"><input type="submit" name="subm" value="Search" class="reg_but_style"style="margin-left:155px;"></input></div><br/><br/>
                        <?php


                            //doPages(10, '/rentals_search.php', 'city=Madurai', 5);
                               /*if (isset($_GET['subm'])) {
                                     $_SESSION['searchflag'] = 'on';
                                    }

                                    if(isset($_SESSION['searchflag']))
                                    {
                                     $newpage=new paging();
                                    //$newpage->pager(" SELECT * FROM rentals_ads WHERE city='Salem'");
                                    $type=$_GET[type];
                                    $city=$_GET[city];
                                    $newpage->pager("SELECT * FROM rentals_ads WHERE city='$city'");
                                    }  */


                        ?>
                        <div class="reg_bl"></div>
                 </form>
             </div>
         </div>
         <div class="adspace"> Advertisement Space</div>
     </div>
</div>

<script language="javascript">
function gen()
{
for (var page = 0; page <= 10; page++) {
    var currentButton = $('<li class="page-number">' + (page) + '</li>');
    <a class="pager" href="/ajax_pages/get_results.php?page=3">page </a>

      }
      }


</script>
</body>
</html>

paging_class.php

<?php
ob_start();


    require_once("common/class.Database.php");
    require_once("common/varDeclare.php");

class paging extends Database
{
   function pager()
   {



        $no_results = TRUE;   // No results found yet
        $howmany    = 5;     // Return 10 results per query

        // Set default starting point of query to 0, or, if set, to $_GET['rs']
        $row_start  = (isset($_GET['rs'])) ? $_GET['rs'] : 0;


        // Do our SQL query, with something like LIMIT 0, 10

        $qcity=$_GET[city];
        $sql="SELECT * FROM rentals_ads WHERE city='$qcity'";
        $result=mysql_query($sql);
        while($row = mysql_fetch_array($result))
        {
             $count=$count+1;
        }
        if($count>0)
        {
        echo "$count results found" ;
        }



        $sql    = "SELECT * FROM rentals_ads WHERE city='$qcity' LIMIT ". $row_start .", ". $howmany ."";


        $result = mysql_query($sql);



        // Get the number of rows that would have been returned WITHOUT a limit clause, to be used later for paging.
        $count_sql        = "SELECT FOUND_ROWS() AS total";
        $count_sql_result = mysql_query($count_sql);

        $count_row    = mysql_fetch_array($count_sql_result);

        $count_result     = $count_row['total'];

        ?>
    <div class="paging_title">
        <div class="paging_title_title"> Title</div>
        <div class="paging_title_type">Room Type</div>
        <div class="paging_title_city">City</div>
        <div class="paging_title_rent">Rent</div>
    </div>
       <?
                // Start looping through our result set

        while($row = mysql_fetch_array($result)) {
            $no_results = FALSE;

            // Save results of query to $line_output
            $line_output .= "
                <div class=\"paging_ad\">
                    <div class=\"paging_ad_title\">". $row['rentals_title'] ."</div>
                    <div class=\"paging_ad_type\">". $row['rentals_type'] ."</div>
                    <div class=\"paging_ad_city\">". $row['city'] ."</div>
                    <div class=\"paging_ad_rent\">". $row['rent'] ."</div>
                </div>";
        }

        // Don't bother building paging if we don't have records
        if ($no_results) {
            $line_output = "No records found...";
            $page_output = "";
        }
        else {
            // Build <prev> and <next> links and save to $page_output
            $rs_prev = $row_start - $howmany; // where would prev page start, given current start less no. of records
            $rs_next = $row_start + $howmany; // where would next page start, given current start plus no. of records

            // If for some reason the next <prev> starting point is negative, do not display <prev>
            // This happens when our current starting point is already 0
            // This may happen if some smartass manually changes the rs= bit in the url
            $page_output_prev   = ($rs_prev < 0) ? "" : "<a href='?rs=".$rs_prev."'>Previous</a>";

            // Will the next page jump start point exceed the number of records returned?
            // If so, don't display <next>'
            $page_output_next   = ($rs_next <= $count_result) ?  "<a href='?rs=".$rs_next."'>Next</a>": "";

            // Just something to put between <prev> & <next>, IF they are both active
            if (($page_output_prev == ""))
            {
                 $page_output ="       <div class=\"paging_nav_cont\">


                    <div class=\"paging_nav_but\">". $page_output_next ."</div>
                    </div>";

            }
           else if (($page_output_next == ""))
            {
                $page_output ="       <div class=\"paging_nav_cont\">
                    <div class=\"paging_nav_but\">". $page_output_prev ."</div>


                    </div>";
            }

            else
            {
                $page_output ="       <div class=\"paging_nav_cont\">
                    <div class=\"paging_nav_but\">". $page_output_prev ."</div>

                    <div class=\"paging_nav_but\">". $page_output_next ."</div>
                    </div>";
            }
            // Build final paging output

            //$page_output = $page_output_prev . $page_output_breaker . $page_output_next;





        }

        // Write the outputs
        echo $line_output;
        echo $page_output;
   }
}
?>

search_results.template.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RBlog - For the Consumers - By the Sellers</title>
<?
require_once("common/class.Database.php");
require_once("dbclass/paging_class.php");
?>
</head>
<body>
    <?php include("includes/header.php"); ?>
    <div class="afterpicer_total">
        <?php include("includes/menu.php"); ?>
    <div class="wrapper">
            <div class="cont">

                <?
                                    $newpage=new paging();
                                    //$newpage->pager(" SELECT * FROM rentals_ads WHERE city='Salem'");
                                    $type=$_GET[type];
                                    $city=$_GET[city];
                                    $newpage->pager();
               ?>
            </div>
            <div class="adspace"> Advertisement Space</div>
       </div>
    </div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

$ _SESSION PHP变量可能正是您要找的。它在页面加载之间存储值。 PHP manual

答案 1 :(得分:0)

不,Scavenger是对的,你必须使用$ _SESSION变量。这是让PHP记住上一页加载的唯一方法。

它不适合你的原因可能是因为你没有正确设置它。你必须添加

<?php session_start(); ?>

到要在其中使用$ _SESSION变量的每个PHP文件的开头。然后,你必须编写类似的代码:

if (isset($_GET['city']) {
    $qcity = $_GET['city'];
    $_SESSION['city'] = $qcity;
} else {
    $qcity = $_SESSION['city'];
}

让它记住上一次的城市。

这是解决问题的一种方法,我认为你也可以用AJAX解决问题。如果这样做,则必须确保页面永远不会完全重新加载。这使输入字段保持有效。您可以从Javascript调用PHP文件(包含数据库查询),而不是重新加载,并相应地操作页面。

我会参加PHP会议(但我也很想学习更多的AJAX,它很整洁: - )。