Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Graham Stoker
Graham Stoker
2,566 Points

searching sql from multi drop downs to change data in table

i have 9 drop downs in index.php brand, model, width, aspect, rim size, speed, load, ply and location when i change one of them it fires off a ajax request to results.php and returns results in the table (only when re defined )

$brand="*"; or maby $brand="pirelli"; etc $query = "SELECT * from tyres where brand='$brand'";

this would be the basic example

if there a way to search sql with the value like * for all results in the table ? or do i need a long php code

end result user changes any 1 drop down and it changes the entire table contents user changes another and it changes the table with the two values etc... if the user changes say the drop down brand from pirelli to brand the table would add in all other brands with the same filters still applied

SELECT * from tyres where brand='*' width='120' etc..

Jeff Lemay
Jeff Lemay
14,268 Points

If you want all results from a table you would just leave out the WHERE clause.

So if you want all tyre brands from the tyres table, you would leave out the WHERE brand = "something".

Or you can use WHERE brand IS NOT NULL.

Graham Stoker
Graham Stoker
2,566 Points

here is my code which may translate better to what I'm trying to achieve

here is index.php which seems to not show the first table prefix of the first option value but shows the first option value text ?

'''html <table> <tr> <th class="head" id="pn">Part Number</th> <th class="head" id="brand"> <select id="tBrand" onchange="showResults()"> <option value="*">Brand</option> <option value="pirelli">Pirelli</option> <option value="bridgestone">Bridgestone</option> <option value="michelin">Michelin</option> </select> </th> <th class="head" id="model"> <select id="tModel" onchange="showResults()"> <option value="*">Model</option> <option value="ms3">MS3</option> <option value="ac10">AC10</option> <option value="mh3">MH3</option> </select> </th>

            <th class="head" id="width">    
                <select id="tWidth" onchange="showResults()">
                    <option value="*">Width</option>
                    <option value="100">100</option>
                    <option value="110">110</option>
                    <option value="120">120</option>
                </select>
            </th>
            <th class="head" id="aspect">
                <select id="tAspect" onchange="showResults()">
                    <option value="*">Aspect Ratio</option>
                    <option value="90">90</option>
                    <option value="100">100</option>
                    <option value="110">110</option>
                </select>
            </th>
            <th class="head" id="rim">
                <select id="tRim" onchange="showResults()">
                    <option value="*">Rim</option>
                    <option value="17">17</option>
                    <option value="18">18</option>
                    <option value="19">19</option>
                </select>
            </th>
            <th class="head" id="speed">
                <select id="tSpeed" onchange="showResults()">
                    <option value="*">Speed</option>
                    <option value="h">H</option>
                    <option value="s">S</option>
                    <option value="r">R</option>
                </select>
            </th>
            <th class="head" id="load">
                <select id="tLoad" onchange="showResults()">
                    <option value="*">Load</option>
                    <option value="60">60</option>
                    <option value="65">65</option>
                    <option value="70">70</option>
                </select>
            </th>
            <th class="head" id="plyType">
                <select id="tPly" onchange="showResults()">
                    <option value="*">Ply Type</option>
                    <option value="b">bias</option>
                    <option value="d">diagonal</option>
                    <option value="r">radial</option>
                </select>
            </th>
            <th class="head" id="location">
                <select id="tLocation" onchange="showResults()">
                    <option value="*">Location</option>
                    <option value="f">Front</option>
                    <option value="r">Rear</option>
                    <option value="b">Both</option>
                </select>
            </th>
        </tr>   
        <tr id="Results" class="Results">
        </tr>

        </table>

''' here is the javascript which is located at the BOTTOM of index.php '''js

    function showResults(){
        var tBrand = document.getElementById("tBrand").value;
        var tModel = document.getElementById("tModel").value;
        var tWidth = document.getElementById("tWidth").value;
        var tAspect = document.getElementById("tAspect").value;
        var tRim = document.getElementById("tRim").value;
        var tSpeed = document.getElementById("tSpeed").value;
        var tLoad = document.getElementById("tLoad").value;
        var tPly = document.getElementById("tPly").value;
        var tLocation = document.getElementById("tLocation").value;

        $("#Results").html("<td colspan='10'>loading Results . . .</td>").show();
        alert(tRim);


        var url="results.php";
        $.post(url, {tBrand:tBrand, tModel:tModel, tWidth:tWidth, tAspect:tAspect, tRim:tRim, tSpeed:tSpeed, tLoad:tLoad, tPly:tPly, tLocation:tLocation} ,function(data){
        $("#Results").html(data).show();});



    };

window.onload = showResults;

'''

here is results.php '''php $brand=$_POST['tBrand']; $model=$_POST['tModel']; $width=$_POST['tWidth']; $aspect=$_POST['tAspect']; $rim=$_POST['tRim']; $speed=$_POST['tSpeed']; $load=$_POST['tLoad']; $plyType=$_POST['tPly']; $location=$_POST['tLocation'];

include "connect_to_mysql.php";

$query = "SELECT * FROM tyres WHERE brand='$brand'"; '''

each time the function is fired off it grabs all values from all drop downs and sends then to results.php

so the value * in a drop down is intended for all values to be returned from the data base for that drop down but then filtered by all other values

i.e. SELECT * FROM tyres WHERE brand='$brand' AND model='$model' AND width='$width' etc... with values might be SELECT * FROM tyres WHERE brand=' * ' AND model=' * ' AND width=' 120 ' etc...

The bit i carnet get to work each drop down changes one of these values in any order

i can get it to change one value at a time and display all results but not changing either value and updating ?

hopefully this is a little better explanation than first time and thanks for the quick response