1

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).

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.

It works, but is largely clunky. once I select the city, I have to select gender, and then city again to get results.

I know the problem lies in the way I run the MySQL. I need your guidance here.

Code for the gallery.php file:

 <html>

<head>
<title>EQUIMATIC GALLERY</title>    


<link rel="stylesheet" type="text/css" href="styles.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 
<script src="jcarousel.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="jcarousel.css">
<script>

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&gender=m&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+"&gender="+gender+"&category="+category,
            success:function(data){

                    //alert("whoa, careful there!");
                    $('#thumbs').html(data);
            }


        });//end ajax
    });




});

</script>

</head>


<body>

<?php include 'conndb.php';?>


<div class="container">
<div class="sublogo"><img src="images/galogo.png"></div>
<div class="sidebaropen">
<div class="statelist">
SORT ENTRIES BY:<br/>


    <h2>01 LOCATION </h2>
    <input type="radio" value="New York" name="state" /> NYC <br>
    <input type="radio" value="Los Angeles" name="state" /> Los Angeles <br>
    <input type="radio" value="San Francisco" name="state" /> San Francisco <br>
    <input type="radio" value="Chicago" name="state" /> Chicago <br>
    <input type="radio" value="Miami" name="state" /> Miami <br>
    <input type="radio" value="Texas" name="state" /> Dallas <br>
    <input type="radio" value="District of Columbia" name="state" /> DC <br>
    <input type="radio" value="Boston" name="state" /> Boston <br>


    </div><!-- state list -->

    <div class="gender">
    <h2>02 GENDER </h2>
    <input type="radio" value="m" name="gender" /> Male <br>
    <input type="radio" value="f" name="gender" /> Female <br>
    </div>


    <div class="category">
    <h2>03 CATEGORY </h2>
    <input type="radio" value="balance" name="category" /> Balance <br>
    <input type="radio" value="confidence" name="category" /> Confidence <br>
    </div>


    </div>

    <div class="staging">
    <img src="http://www.byequinox.com/equinox_images/24447ddec24d22102eebf8a0a1d14e87.jpg" id="currentlystaged" /> 

    </div>

<div id="results">

<ul id=thumbs class='jcarousel jcarousel-skin-tango' style='width:250px; list-style:none; height:400px' >   

</ul>


</div>
</div>


</div>


</body>
</html>

Code for the sort by State:

<?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("<a href=# >");
    echo("<li style='width:60px; margin:0 10px 0 35px; float:left;'><img src=".$filter_row['link']." width=100 border=0  onclick=displayFullImage('$link'); > </a></li>");
    //echo($filter_result);
}




?>

THE LINK FOR THE ACTUAL WORKING GALLERY: http://rjwcollective.com/equinox/rishi_gallery/eqgall.php

5
  • 1
    Frishi, please introduce yourself to Bobby Tables. Commented Apr 26, 2011 at 15:08
  • How many records are you grabbing back with that SQL? Have you thought about paging say 10-20... i suspect this is where the issue is. You have to request many images, this is likely the bottlenek? Commented Apr 26, 2011 at 15:09
  • thanks diagonalbatman. the client wants everything to be shown. I guess the question is more on the lines of: Once I make a query, a resultset is generated, when I apply the second filter, how do I operate on the same resultset? Commented Apr 26, 2011 at 15:14
  • Um... SQL Injection... hello? Commented Apr 26, 2011 at 15:24
  • yea awm, saw that, thanks for the tip. Not a pro at this at all. Commented Apr 26, 2011 at 15:52

1 Answer 1

2

The problem is that you're only updating the photos when the city is chosen. Extract the photo retrieving functionality into its own function then call that function from the click event on all 3 radios

Sign up to request clarification or add additional context in comments.

1 Comment

THANKS!! yup I see what was wrong with my logic. I'll remember to vote you up when I have 15 reputation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.