如何使sql查询动态?

时间:2010-03-23 18:16:38

标签: php jquery mysql class pagination

我正在使用这个pagination class,并且正在寻找一种方法来使sql查询更加动态,而不是让它硬编码。

我有3 <li>个元素,我想成为过滤按钮,这意味着当用户点击其中一个元素时,我希望它发送id,以便我可以在sql查询中使用它。

对于$sql = "select * from explore where category='marketing'";(如下所示)。当用户点击“汽车”按钮时,它会将上面的类别更改为汽车。

对此的任何帮助都将受到高度赞赏,谢谢。

这就是我的主页面:

<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery_page.js"></script>


<?php
    //Include the PS_Pagination class
    include('ps_pagination.php');
    //Connect to mysql db
    $conn = mysql_connect('localhost', 'root', 'root');
    mysql_select_db('ajax_demo',$conn);
    $sql = "select * from explore where category='marketing'";
    //Create a PS_Pagination object
    $pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=valu1&param2=value2');
    //The paginate() function returns a mysql
    //result set for the current page
    $rs = $pager->paginate();
    //Loop through the result set
    while($row = mysql_fetch_assoc($rs)) {
            echo "<table width='800px'>";

                echo "<tr>";
                    echo"<td>";
                    echo $row['id'];
                    echo"</td>";

                    echo"<td>";
                    echo $row['site_description'];
                    echo"</td>";

                    echo"<td>";
                    echo $row['site_price'];
                    echo"</td>";
                echo "</tr>";

            echo "</table>";
    }

        echo "<ul id='pagination'>";

            echo "<li>";
            //Display the navigation
            echo $pager->renderFullNav();
            echo "</li>";

        echo "</ul>";

    echo "<ul id='filter'>";

        echo "<li id='marketing'>";
            echo "Marketing";
        echo "</li>";

        echo "<li id='automotive'>";
            echo "Automotive";
        echo "</li>";

        echo "<li id='sports'>";
            echo "Sports";
        echo "</li>";

    echo "</ul>";
?>

1 个答案:

答案 0 :(得分:1)

看起来像这一行

$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=valu1&param2=value2');
应该编辑

以在运行sql查询之前携带要测试的变量....可能像:

$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=automotive&param2=sports');

然后尝试这个

if($_POST["param1"]=="automotive")
{
$sql = "select * from explore where category='automotive'";
}

并解决它,看看你是否能得到你想要的结果。我以前从未使用过这门课程,但看起来你必须使用这些参数来获得你想要的结果