开发者

jQuery AJAX/ PHP/MySQL live filtering

开发者 https://www.devze.com 2023-02-28 15:45 出处:网络
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).

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号