Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery AJAX/ PHP/MySQL live filtering
    primarykey
    data
    text
    <p>I am building a image gallery where, you can filter images by city, category and gender. The problem I am faced with is how to update the results live (real-time).</p> <p>The logic I apply is: Select a filter criteria, click on a radio button. Send query via jQuery AJAX to a PHP script that runs a MySQL query and returns HTML.</p> <p>It works, but is largely clunky. once I select the city, I have to select gender, and then city again to get results.</p> <p>I know the problem lies in the way I run the MySQL. I need your guidance here.</p> <p>Code for the gallery.php file:</p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;title&gt;EQUIMATIC GALLERY&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="jcarousel.js" type="text/javascript"&gt;&lt;/script&gt; &lt;link rel="stylesheet" type="text/css" href="jcarousel.css"&gt; &lt;script&gt; function displayFullImage(link) { //alert(link); $("#currentlystaged").attr('src', link); } $(document).ready(function(){ $('#thumbs').jcarousel({ vertical: true, scroll: 13 }); //first load $.ajax({ type:"POST", url:"sortbystate.php", data:"city=new york&amp;gender=m&amp;category=", success:function(data){ //alert("whoa, careful there!"); $('#thumbs').html(data); } });//end ajax // normal operation $(".statelist :input").click(function(){ var state = $('.statelist input:checked').attr('value'); var gender = $('.gender input:checked').attr('value'); var category =$('.category input:checked').attr('value'); $.ajax({ type:"POST", url:"sortbystate.php", data:"city="+state+"&amp;gender="+gender+"&amp;category="+category, success:function(data){ //alert("whoa, careful there!"); $('#thumbs').html(data); } });//end ajax }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;?php include 'conndb.php';?&gt; &lt;div class="container"&gt; &lt;div class="sublogo"&gt;&lt;img src="images/galogo.png"&gt;&lt;/div&gt; &lt;div class="sidebaropen"&gt; &lt;div class="statelist"&gt; SORT ENTRIES BY:&lt;br/&gt; &lt;h2&gt;01 LOCATION &lt;/h2&gt; &lt;input type="radio" value="New York" name="state" /&gt; NYC &lt;br&gt; &lt;input type="radio" value="Los Angeles" name="state" /&gt; Los Angeles &lt;br&gt; &lt;input type="radio" value="San Francisco" name="state" /&gt; San Francisco &lt;br&gt; &lt;input type="radio" value="Chicago" name="state" /&gt; Chicago &lt;br&gt; &lt;input type="radio" value="Miami" name="state" /&gt; Miami &lt;br&gt; &lt;input type="radio" value="Texas" name="state" /&gt; Dallas &lt;br&gt; &lt;input type="radio" value="District of Columbia" name="state" /&gt; DC &lt;br&gt; &lt;input type="radio" value="Boston" name="state" /&gt; Boston &lt;br&gt; &lt;/div&gt;&lt;!-- state list --&gt; &lt;div class="gender"&gt; &lt;h2&gt;02 GENDER &lt;/h2&gt; &lt;input type="radio" value="m" name="gender" /&gt; Male &lt;br&gt; &lt;input type="radio" value="f" name="gender" /&gt; Female &lt;br&gt; &lt;/div&gt; &lt;div class="category"&gt; &lt;h2&gt;03 CATEGORY &lt;/h2&gt; &lt;input type="radio" value="balance" name="category" /&gt; Balance &lt;br&gt; &lt;input type="radio" value="confidence" name="category" /&gt; Confidence &lt;br&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="staging"&gt; &lt;img src="http://www.byequinox.com/equinox_images/24447ddec24d22102eebf8a0a1d14e87.jpg" id="currentlystaged" /&gt; &lt;/div&gt; &lt;div id="results"&gt; &lt;ul id=thumbs class='jcarousel jcarousel-skin-tango' style='width:250px; list-style:none; height:400px' &gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Code for the sort by State:</p> <pre><code>&lt;?php include "conndb.php"; $city = $_POST['city']; $gender = $_POST['gender']; $category = $_POST['category']; $filter_query= "SELECT * FROM equinox where city LIKE CONVERT( _utf8 '$city' USING latin1 ) AND gender = '$gender'"; $filter_result = mysql_query($filter_query); while($filter_row= mysql_fetch_array($filter_result)) { $link = $filter_row['link']; echo("&lt;a href=# &gt;"); echo("&lt;li style='width:60px; margin:0 10px 0 35px; float:left;'&gt;&lt;img src=".$filter_row['link']." width=100 border=0 onclick=displayFullImage('$link'); &gt; &lt;/a&gt;&lt;/li&gt;"); //echo($filter_result); } ?&gt; </code></pre> <p>THE LINK FOR THE ACTUAL WORKING GALLERY: <a href="http://rjwcollective.com/equinox/rishi_gallery/eqgall.php" rel="nofollow">http://rjwcollective.com/equinox/rishi_gallery/eqgall.php</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload