/    Sign up×
Community /Pin to ProfileBookmark

PHP: Input MySQL query

Hey guys I am a little confused, I have a listing page that displays all of my rows in my SQL table.

An example of what I am developing can be seen here: [url]http://www.drivencarsales.co.uk/used-cars.php[/url]

So as you can see I have developed a listing page that fetches all of the table rows, each row is equivalent to one vehicle.

I now want to let users filter the results using the form to the left, I was going to use AJAX originally however I feel as if it would take way to long to learn how to develop it that way.

Here is the code setup I am using to achieve the example I have shown:

[CODE]<?php
include(‘database.php’);
try {
$results = $db->query(“SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make ASC”);
} catch (Exception $e) {
echo “Error.”;
exit;
}

try {
$filterres = $db->query(“SELECT DISTINCT Make FROM import ORDER BY Make ASC”);
} catch (Exception $e) {
echo “Error.”;
exit;
}
?>[/CODE]

As you can see the first block of code has two SQL selectors, the $results is used to fetch the whole table and list all vehicles.

The second block is used to display the ‘Make’ column for the form.

This block of code is the actual form:

[CODE]<form>
<select class=”form-control select-box” name=””>
<option value=”make-any”>Make (Any)</option>
<?php while($make = $filterres->fetch(PDO::FETCH_ASSOC))
{
echo ‘
<option value=””>’.$make[“Make”].'</option>
‘;
} ?>
</select>
<button href=”#” class=”btn btn-block car-search-button btn-lg btn-success”><span class=”glyphicon car-search-g glyphicon-search”></span> Search cars
</button>
</form>[/CODE]

As you can see this block is using a while loop to display all of the ‘Make’s’ in the ‘Make’ column and uses a DISTINCT clause so that it doesn’t show identical options.

Here is the block that lists the results to the page:

[CODE]<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
{
echo ‘
<div class=”listing-container”>
<a href=”carpage.php”><h3 class=”model-listing-title clearfix”>’.$row[“Make”].’ ‘.$row[“Model”].’ ‘.$row[“Variant”].'</h3></a>
<h3 class=”price-listing”>£’.number_format($row[‘Price’]).'</h3>
</div>
<div class=”listing-container-spec”>
<img src=”‘.(explode(‘,’, $row[“PictureRefs”])[0]).'” class=”stock-img-finder”/>
<div class=”ul-listing-container”>
<ul class=”overwrite-btstrp-ul”>
<li class=”diesel-svg list-svg”>’.$row[“FuelType”].'</li>
<li class=”saloon-svg list-svg”>’.$row[“Bodytype”].'</li>
<li class=”gear-svg list-svg”>’.$row[“Transmission”].'</li>
<li class=”color-svg list-svg”>’.$row[“Colour”].'</li>
</ul>
</div>
<ul class=”overwrite-btstrp-ul other-specs-ul h4-style”>
<li>Mileage: ‘.number_format($row[“Mileage”]).'</li>
<li>Engine size: ‘.$row[“EngineSize”].’cc</li>
</ul>
<button href=”#” class=”btn h4-style checked-btn hover-listing-btn”><span class=”glyphicon glyphicon-ok”></span> History checked
</button>
<button href=”#” class=”btn h4-style more-details-btn hover-listing-btn tst-mre-btn”><span class=”glyphicon glyphicon-list”></span> More details
</button>
<button href=”#” class=”btn h4-style test-drive-btn hover-listing-btn tst-mre-btn”><span class=”test-drive-glyph”></span> Test drive
</button>
<h4 class=”h4-style listing-photos-count”><span class=”glyphicon glyphicon-camera”></span> 5 More photos</h4>
</div>
‘;
} ?>[/CODE]

So down to my question… How can I filter these results displayed in the listing block using the select element, when a user selects a ‘Make’ from the select element I want them to be able to submit the form and return all rows in the SQL table containing the same ‘Make’ string and hide other rows that are false.

Any ideas how I can achieve this or any easier ways?

Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmSep 21.2014 — Add the selected option to your first query's where clause?
×

Success!

Help @Nickmadd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...