How to make sql query dynamic?

Posted by ClarkSKent on Stack Overflow See other posts from Stack Overflow or by ClarkSKent
Published on 2010-03-23T18:16:37Z Indexed on 2010/03/23 18:23 UTC
Read the original article Hit count: 317

Filed under:
|
|

Hello, I am using this pagination class and was looking for a way to make the sql query more dynamic instead of having it hardcoded.

I have a 3 <li> elements that I want to be filter buttons, meaning when a user clicks on one of these elements I want It to send the id so I can use it in a sql query.

So for the $sql = "select * from explore where category='marketing'"; (as seen below). When the user clicks on the 'automotive' button it will change the category above to automotive.

Any help on this would be highly appreciated, Thanks.

This is what my main page looks like:

<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>";
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql