views:

127

answers:

7

Hi.

I am currently creating a type of "classifieds" website, and when the user enters the site he/she is able to choose for example "Vehicles" in a dropdown list, and then choose mileage, year, price range etc...

Here is my problem, I have managed to make contact with a db using ajax to call a php script which then checks the mysql db for the search criteria and returns a result. BUT, I need to make this as "smart" as possible...

How can i make a function or maybe a loop to check what category the user chose, and then to see if the user made any specifications on price or mileage for example, and then build a query_string to send to the php code, which then checks the mysql database for the search?

Heres a bit of code from the ajax file:

//Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible!

try{
 // Opera 8.0+, Firefox, Safari
 ajaxRequest = new XMLHttpRequest();
} catch (e){
 // Internet Explorer Browsers
 try{
  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try{
   ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e){
   // Something went wrong
   alert("Your browser broke!");
   return false;
  }
 }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
 if(ajaxRequest.readyState == 4){
  var ajaxDisplay = document.getElementById('cont');
  ajaxDisplay.innerHTML = ajaxRequest.responseText;
 }
} 


var category = document.getElementById('nav_category_list').value;
var city = document.getElementById('nav_city_list').value;
var querystring = document.getElementById('nav_querystring').value;
var cars_price_from = document.getElementById('cars_price_from').value;
var cars_price_to = document.getElementById('cars_price_to').value;
var cars_year_from = document.getElementById('cars_yr_from').value;
var cars_year_to = document.getElementById('cars_yr_to').value;
var cars_mileage_from = document.getElementById('cars_mile_from').value;
var cars_mileage_to = document.getElementById('cars_mile_to').value;
var cars_grbx = document.getElementById('cars_grbx').value;
var cars_fuel = document.getElementById('cars_fuel').value;

var send_query = "?category=" + category + "&city=" + city + "&cars_price_from=" + cars_price_from + "&cars_price_to=" + cars_price_to + "&cars_year_from=" + cars_year_from + "&cars_year_to=" + cars_year_to + "&cars_mileage_from=" + cars_mileage_from + "&cars_mileage_to=" + cars_mileage_to + "&cars_grbx=" + cars_grbx + "&cars_fuel=" + cars_fuel +"&querystring=" + querystring;

ajaxRequest.open("GET", "bincgi/ajax-example.php" + send_query, true);
ajaxRequest.send(null);

}

A: 

And here is the PHP code by the way which the ajax code above calls for:

    $category = $_GET['category'];
    $city = $_GET['city'];
    $querystring = $_GET['querystring'];
// Escape User Input to help prevent SQL Injection
$category = mysql_real_escape_string($category);
$city = mysql_real_escape_string($city);
$querystring = mysql_real_escape_string($querystring);
$cars_price_from = $_GET['cars_price_from'];
$cars_price_to = $_GET['cars_price_to'];
$cars_year_from = $_GET['cars_year_from'];
$cars_year_to = $_GET['cars_year_to'];
$cars_mileage_from = $_GET['cars_mileage_from'];
$cars_mileage_to = $_GET['cars_mileage_to'];
$cars_gearbox = $_GET['cars_grbx'];
$cars_fuel = $_GET['cars_fuel'];

//build query
    $query = "SELECT * FROM cars_db WHERE price BETWEEN '$cars_price_from' AND '$cars_price_to' AND year BETWEEN '$cars_year_from' AND '$cars_year_to' AND mileage BETWEEN '$cars_mileage_from' AND '$cars_mileage_to' AND gearbox = '$cars_gearbox' AND fuel = '$cars_fuel'";
//Execute query
    $qry_result = mysql_query($query) or die(mysql_error());

//Build Result String
    $display_table = "<table class='ad_container'>";

// Insert a new row in the table for each result
    while($row = mysql_fetch_array($qry_result)){
if ($row['area_id']=='1') {$row['area_id']="Göteborg";}
$display_table .= "<tr>";
$display_table .= "<td width='110' rowspan='2'>BILD</td>";
$display_table .= "<td width='377' height='15'>$row[headline]</td>";
$display_table .= "<td width='98' rowspan='2'>$row[area_id]</td>";
$display_table .= "<td width='67' rowspan='2'>$row[insert_date]</td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td height='15'>$row[price]:-</td>";
$display_table .= "</tr>";
     }

    //echo "Query: " . $query . "<br />";
    $display_table .= "</table>";
    echo $display_table;
Camran
it would help to create some Assertions - check numeric input whether it is real numeric value - is_numeric($var)
Juraj Blahunka
+2  A: 

Why not just add the item->value to an array (using jQuery) upon user selection, and pass this array to the php?

The in the PHP explode the array and check each element?

Steven
A: 

Because i dont know any jquery, sorry...

Any other solutions?

camran
jQuery is a wide used javascript library, which would really simplify your script and is easy to learn
Juraj Blahunka
Please comment your question or other answers, or edit your original question...
Skilldrick
+1  A: 

Learn jQuery! Seriously, it's really easy.

Skilldrick
I couldn't agree more on this :)
Juraj Blahunka
jQuery is overrated. It's a "simple" JS framework, not the answer to life, the universe and everything. If you're not able to properly code in Javascript, throwing a framework against your problems won't magically fix everything.
Duroth
Of course it's not the answer to life, the universe and everything, but it is amongst other things an AJAX library, which abstracts away a lot of the problems which the OP is encountering.
Skilldrick
A: 

I dont have time to learn it I am sorry guys, but could you just point me in the right direction when it comes to the coding of this...

How would be the "best" way for doing what i want?

ALSO, there is an error if all elements in the array that I send to the php file isnt filled with values... If an array is empty, then the script will crash...

Any suggestions on how to solve this easily?

Thanx for all help...

camran
ok got it, comment!
camran
@camran: seriously learning JQuery will take shorter than doing it with pure ajax. Check this link: http://articles.sitepoint.com/article/ajax-jquery
JCasso
A: 

Create a generic methods like this

function getElementValue(id)
{
   if(document.getElementById(id) != null) 
   {
     return document.getElementById(id).value;
   }
}

Then give the unique id for your dropdowns like catagory1 to 10 and then iterate it with loops and use the method.

TuxGeek
A: 

In javascript check the each element value if its not null if(a!='') then only concatenate in string argument . make one argument with some delimiter and explode in php file and according to that make sql query .

neverSayNo