-
Searching a db
Hi:
I finally have some free time to learn PHP, but I need a push in the right direction for something I'm working on...
I've set up a MySQL db using PHPmyAdmin; it has 4 columns (year, make, model, url). It's a table of used car inventory, so I want to be able to search by 'year,' 'make,' or model, and produce an HTML page containing the results, including the url to the page for each row returned in the search. I will be using dropdown lists with an onchange event handler to restrict searches to only the above 3 categories, but I would like to return results in descending order, either numerically, when searching by "year," or alphabetically, when searching by "make" or "model."
Here's a page containing examples of how I'd like the results returned:
http://www.ekigroup.com/table.html
A link to a good tutorial, or better yet, some sample code I can modify for my purposes will be greatly appreciated.
Thanks,
-james
-
Hi,
this is not exactly what you are looking for, but users will be familiar with this way to show results
Code:
$query = "select * from cars where ";
$conds = array();
if(!empty($_POST["year"]))
$conds[] = "year = '{$_POST["year"]}'";
if(!empty($_POST["make"]))
$conds[] = "make = '{$_POST["make"]}'";
if(!empty($_POST["model"]))
$conds[] = "model = '{$_POST["model"]}'";
if(!count($conds))
die("please enter some search terms");
$query .= implode(" and ", $conds);
$query .= " order by year, make, model";
Next step in scripting probably should be clickable column headings that change the sort order
Musicman
-
Hi Wolfgang:
Thanks for the help. Unfortunately, I haven't been successful at incorporating your array scenario. It can't seem to populate it. with the table fields/column values.
What I came up with is...
Code:
<?php
$db = mysql_connect ('localhost', 'myusername', 'mypassword')
or die ("Unable to connect to Database Server");
mysql_select_db ('mydbname', $db)
or die ("Could not select database");
$frmval = $HTTP_GET_VARS["year"];
$result = mysql_query("SELECT * FROM mytablename WHERE year = '$frmval' ORDER BY year, make, model")
or die("Query failed : " . mysql_error());
while ($col = mysql_fetch_assoc($result)){
echo $col["year"] . $col["make"] . $col["model"] . $col["link"];
}
?>
This works, but I'm unsure of the best way to format the resultant
(HTML) page.
Thanks, again, for your help!
-james