Do filtering with jquery and php

3

Good, I have a catalog of products that I did with a while to show all the products that I have in the db, the thing is that the template I use brings some that I would like to use to filter the products, the thing is. Searching I did something that half worked, but when you press any of the two options in the option, only the one that has class=1 remains.

What I did was with the same data from the while put:

<div class="<?php echo $r["moneda"], ?>">

Then in the select if I choose the option Dollars has a value = 1 so if the field 'currency' of that product is 1 should be and those that are 2 disappear. The thing is that whether I choose the option of value = 1 or the value = 2 always appears to anything. I do not know what can be done that I leave my code

Show products

<?php  

while ($r = $query->fetch_array()) { ?>
    <div id="productos">
        <div class="<?php echo $r["moneda"]; ?>">
            <div class="inventory margin-bottom-20 clearfix scroll_effect fadeIn">
                <input type="checkbox" name="a" class="checkbox compare_vehicle input-checkbox" id="vehicle_1"/>
                <label for="vehicle_1"></label>
                <a class="inventory" href="inventory-listing.html">
                    <div class="title"><?php echo $r["nombre"]; ?></div>
                    <img src="../images/productos/<?php echo $r["img"]; ?>" class="preview" alt="preview">
                    <table class="options-primary">
                        <tr>
                            <td class="option primary">Body Style:</td>
                            <td class="spec">Sport Utility Vehicle</td>
                        </tr>
                        <tr>
                            <td class="option primary">Drivetrain:</td>
                            <td class="spec">4WD</td>
                        </tr>
                        <tr>
                            <td class="option primary">Engine:</td>
                            <td class="spec">4.8L V8</td>
                        </tr>
                        <tr>
                            <td class="option primary">Transmission:</td>
                            <td class="spec">8-Speed Tiptronic</td>
                        </tr>
                        <tr>
                            <td class="option primary">Mileage:</td>
                            <td class="spec">19,585</td>
                        </tr>
                    </table>
                    <table class="options-secondary">
                        <tr>
                            <td class="option secondary">Exterior Color:</td>
                            <td class="spec">Dark Blue Metallic</td>
                        </tr>
                        <tr>
                            <td class="option secondary">Interior Color:</td>
                            <td class="spec">Black / Titanium Blue</td>
                        </tr>
                        <tr>
                            <td class="option secondary">MPG:</td>
                            <td class="spec">15 city / 21 hwy</td>
                        </tr>
                        <tr>
                            <td class="option secondary">Stock Number:</td>
                            <td class="spec">590497</td>
                        </tr>
                        <tr>
                            <td class="option secondary">VIN Number:</td>
                            <td class="spec">WP1AD29P09LA65818</td>
                        </tr>
                    </table>
                    <img src="http://demo.themesuite.com/automotive/images/carfax.png" alt="carfax" class="carfax"/>
                    <div class="price"><b>Precio:</b><br>
                        <div class="figure"><?php if ($r["moneda"] == 1) {
                                echo "$ " . $r["precio"];
                            } elseif ($r["moneda"] == 2) {
                                echo "Cr " . $r["precio"];
                            } ?><br>
                        </div>
                    </div>
                    <div class="view-details gradient_button"><i class='fa fa-plus-circle'></i> Ver Detalles</div>
                    <div class="clearfix"></div>
                </a>
                <div class="view-video gradient_button" data-youtube-id="3oh7PBc33dk"><i class="fa fa-video-camera"></i>
                    Ver Video
                </div>
            </div>
        </div>
    </div>
<?php } ?>

And this is the jQuery you use:

(function(){
    var $tabla = $('#productos');

    $('#filtro_moneda').change(function(){
        var value = $(this).val();
        if (value){
            $('div.' + value, $tabla).show();
            $('div:not(.' + value + ')', $tabla).hide();
        }
        else{
            // Se ha seleccionado All
            $('div', $tabla).show();
        }
    });
})

And here is the option:

<select name="price" class="css-dropdowns" tabindex="1" id="filtro_moneda">
    <option value="">Metodo de Pago</option>
    <option value="1">Dolares</option>
    <option value="2">Créditos</option>
</select>
    
asked by Santiago D'Antuoni 24.10.2016 в 01:56
source

3 answers

3

The problem is very easy to solve and it is in the JavaScript: it lacks the dollar symbol ( $ ) at the beginning. In jQuery for something to run when the page loads, you can do $(document).ready(function() { ... }) or $(function() { ... }) . In your case it seems that you opted for the second option but you still have to complete the code with $ :

$(function(){
    var $tabla = $('#productos');

    $('#filtro_moneda').change(function(){
        var value = $(this).val();
        if (value){
            $('div.' + value, $tabla).show();
            $('div:not(.' + value + ')', $tabla).hide();
        }
        else{
            // Se ha seleccionado All
            $('div', $tabla).show();
        }
    });
})

Once you do that, the code already works without problems. You can see it in this example:

$(function(){
    var $tabla = $('#productos');

    $('#filtro_moneda').change(function(){
        var value = $(this).val();
        if (value){
            $('div.' + value, $tabla).show();
            $('div:not(.' + value + ')', $tabla).hide();
        }
        else{
            // Se ha seleccionado All
            $('div', $tabla).show();
        }
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

<select name="price" class="css-dropdowns" tabindex="1" id="filtro_moneda">
  <option value="">Metodo de Pago</option>
  <option value="1">Dolares</option>
  <option value="2">Créditos</option>
</select>

<div id="productos">
  <div class="1">Uno</div>
  <div class="2">Dos</div>
  <div class="1">one</div>
  <div class="2">two</div>
  <div class="1">Un</div>
</div>
    
answered by 24.10.2016 в 02:35
0

Try this way:

(function(){
    var $tabla = $('#productos');

    $('#filtro_moneda').change(function(){
        var value = $(this).val();
        if (value){
            $('.' + value).show();
            $('div:not(.' + value + ')').hide();
        }
        else{
            // Se ha seleccionado All
            $('div').show();
        }
    });
})

I would recommend opening the console (Ctrl + Mayus + i) and see if it throws an error. The majority of errors are due to err in the typing of the selectors

    
answered by 24.10.2016 в 02:40
0

This is what PHP returns to me in HTML

                             
                            <div id="productos">
                            <div class="0">
                            <div class="inventory margin-bottom-20 clearfix scroll_effect fadeIn animated" style="visibility: visible; animation-name: fadeIn;">
                                <input type="checkbox" name="a" class="checkbox compare_vehicle input-checkbox" id="vehicle_1">
                                <label for="vehicle_1"></label>
                                <a class="inventory" href="inventory-listing.html">
                                <div class="title">Police Car</div>
                                <img src="../images/productos/ThumbFinal_4.9.15.png" class="preview" alt="preview">
                                <table class="options-primary">
                                    <tbody><tr>
                                        <td class="option primary">Body Style:</td>
                                        <td class="spec">Sport Utility Vehicle</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Drivetrain:</td>
                                        <td class="spec">4WD</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Engine:</td>
                                        <td class="spec">4.8L V8</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Transmission:</td>
                                        <td class="spec">8-Speed Tiptronic</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Mileage:</td>
                                        <td class="spec">19,585</td>
                                    </tr>
                                </tbody></table>
                                <table class="options-secondary">
                                    <tbody><tr>
                                        <td class="option secondary">Exterior Color:</td>
                                        <td class="spec">Dark Blue Metallic</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Interior Color:</td>
                                        <td class="spec">Black / Titanium Blue</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">MPG:</td>
                                        <td class="spec">15 city / 21 hwy</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Stock Number:</td>
                                        <td class="spec">590497</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">VIN Number:</td>
                                        <td class="spec">WP1AD29P09LA65818</td>
                                    </tr>
                                </tbody></table>
                                <img src="http://demo.themesuite.com/automotive/images/carfax.png" alt="carfax" class="carfax">
                                <div class="price"><b>Precio:</b><br>
                                    <div class="figure"><br>
                                    </div>
                                </div>
                                <div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> Ver Detalles </div>
                                <div class="clearfix"></div>
                                </a>
                                <div class="view-video gradient_button" data-youtube-id="3oh7PBc33dk"><i class="fa fa-video-camera"></i> Ver Video</div>
                            </div>
</div>
</div>
                            
                            <div id="productos">
                            <div class="2">
                            <div class="inventory margin-bottom-20 clearfix scroll_effect fadeIn animated" style="visibility: visible; animation-name: fadeIn;">
                                <input type="checkbox" name="a" class="checkbox compare_vehicle input-checkbox" id="vehicle_1">
                                <label for="vehicle_1"></label>
                                <a class="inventory" href="inventory-listing.html">
                                <div class="title">Bullet</div>
                                <img src="../images/productos/50777-1344439068-gta-sa2012-08-0818-15-34-89.jpg" class="preview" alt="preview">
                                <table class="options-primary">
                                    <tbody><tr>
                                        <td class="option primary">Body Style:</td>
                                        <td class="spec">Sport Utility Vehicle</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Drivetrain:</td>
                                        <td class="spec">4WD</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Engine:</td>
                                        <td class="spec">4.8L V8</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Transmission:</td>
                                        <td class="spec">8-Speed Tiptronic</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Mileage:</td>
                                        <td class="spec">19,585</td>
                                    </tr>
                                </tbody></table>
                                <table class="options-secondary">
                                    <tbody><tr>
                                        <td class="option secondary">Exterior Color:</td>
                                        <td class="spec">Dark Blue Metallic</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Interior Color:</td>
                                        <td class="spec">Black / Titanium Blue</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">MPG:</td>
                                        <td class="spec">15 city / 21 hwy</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Stock Number:</td>
                                        <td class="spec">590497</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">VIN Number:</td>
                                        <td class="spec">WP1AD29P09LA65818</td>
                                    </tr>
                                </tbody></table>
                                <img src="http://demo.themesuite.com/automotive/images/carfax.png" alt="carfax" class="carfax">
                                <div class="price"><b>Precio:</b><br>
                                    <div class="figure">Cr 160000<br>
                                    </div>
                                </div>
                                <div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> Ver Detalles </div>
                                <div class="clearfix"></div>
                                </a>
                                <div class="view-video gradient_button" data-youtube-id="3oh7PBc33dk"><i class="fa fa-video-camera"></i> Ver Video</div>
                            </div>
</div>
</div>
                            
                            <div id="productos">
                            <div class="1">
                            <div class="inventory margin-bottom-20 clearfix scroll_effect fadeIn" style="visibility: hidden; animation-name: none;">
                                <input type="checkbox" name="a" class="checkbox compare_vehicle input-checkbox" id="vehicle_1">
                                <label for="vehicle_1"></label>
                                <a class="inventory" href="inventory-listing.html">
                                <div class="title">Infernus</div>
                                <img src="../images/productos/7sALOSB.png" class="preview" alt="preview">
                                <table class="options-primary">
                                    <tbody><tr>
                                        <td class="option primary">Body Style:</td>
                                        <td class="spec">Sport Utility Vehicle</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Drivetrain:</td>
                                        <td class="spec">4WD</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Engine:</td>
                                        <td class="spec">4.8L V8</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Transmission:</td>
                                        <td class="spec">8-Speed Tiptronic</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Mileage:</td>
                                        <td class="spec">19,585</td>
                                    </tr>
                                </tbody></table>
                                <table class="options-secondary">
                                    <tbody><tr>
                                        <td class="option secondary">Exterior Color:</td>
                                        <td class="spec">Dark Blue Metallic</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Interior Color:</td>
                                        <td class="spec">Black / Titanium Blue</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">MPG:</td>
                                        <td class="spec">15 city / 21 hwy</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Stock Number:</td>
                                        <td class="spec">590497</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">VIN Number:</td>
                                        <td class="spec">WP1AD29P09LA65818</td>
                                    </tr>
                                </tbody></table>
                                <img src="http://demo.themesuite.com/automotive/images/carfax.png" alt="carfax" class="carfax">
                                <div class="price"><b>Precio:</b><br>
                                    <div class="figure">$ 45<br>
                                    </div>
                                </div>
                                <div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> Ver Detalles </div>
                                <div class="clearfix"></div>
                                </a>
                                <div class="view-video gradient_button" data-youtube-id="3oh7PBc33dk"><i class="fa fa-video-camera"></i> Ver Video</div>
                            </div>
</div>
</div>
                            
                            <div id="productos">
                            <div class="2">
                            <div class="inventory margin-bottom-20 clearfix scroll_effect fadeIn" style="visibility: hidden; animation-name: none;">
                                <input type="checkbox" name="a" class="checkbox compare_vehicle input-checkbox" id="vehicle_1">
                                <label for="vehicle_1"></label>
                                <a class="inventory" href="inventory-listing.html">
                                <div class="title">Bullet Full</div>
                                <img src="../images/productos/50777-1344439068-gta-sa2012-08-0818-15-34-89.jpg" class="preview" alt="preview">
                                <table class="options-primary">
                                    <tbody><tr>
                                        <td class="option primary">Body Style:</td>
                                        <td class="spec">Sport Utility Vehicle</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Drivetrain:</td>
                                        <td class="spec">4WD</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Engine:</td>
                                        <td class="spec">4.8L V8</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Transmission:</td>
                                        <td class="spec">8-Speed Tiptronic</td>
                                    </tr>
                                    <tr>
                                        <td class="option primary">Mileage:</td>
                                        <td class="spec">19,585</td>
                                    </tr>
                                </tbody></table>
                                <table class="options-secondary">
                                    <tbody><tr>
                                        <td class="option secondary">Exterior Color:</td>
                                        <td class="spec">Dark Blue Metallic</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Interior Color:</td>
                                        <td class="spec">Black / Titanium Blue</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">MPG:</td>
                                        <td class="spec">15 city / 21 hwy</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">Stock Number:</td>
                                        <td class="spec">590497</td>
                                    </tr>
                                    <tr>
                                        <td class="option secondary">VIN Number:</td>
                                        <td class="spec">WP1AD29P09LA65818</td>
                                    </tr>
                                </tbody></table>
                                <img src="http://demo.themesuite.com/automotive/images/carfax.png" alt="carfax" class="carfax">
                                <div class="price"><b>Precio:</b><br>
                                    <div class="figure">Cr 30<br>
                                    </div>
                                </div>
                                <div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> Ver Detalles </div>
                                <div class="clearfix"></div>
                                </a>
                                <div class="view-video gradient_button" data-youtube-id="3oh7PBc33dk"><i class="fa fa-video-camera"></i> Ver Video</div>
                            </div>
</div>
</div>
                                                               
                                 
    
answered by 24.10.2016 в 23:35