How to make sql query dynamic?
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¶m2=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'>";
开发者_如何学JAVA 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>";
?>
looks like this line
$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=valu1¶m2=value2');
should be edited to carry variables to be tested before the sql query is run.... something maybe like:
$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=automotive¶m2=sports');
then try this
if($_POST["param1"]=="automotive")
{
$sql = "select * from explore where category='automotive'";
}
and work around with it to see if you can get your desired results. I've never used the class before, but it looks like you'll have to play with these params to get your desired result
精彩评论