Trying to filter currency, but never shows results.
Trying to filter currency, but never shows results.
I'm trying to filter my results using "minprice" and "maxprice" fields. For simplicity sake, I'm starting off with just minprice.
Unfortunately, no matter what value I give it, it shows no results. Here's my code:
[code] $(document).ready(function() {
var oTable = $("#results_table").dataTable({
"bJQueryUI": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource":"get_listings.php",
"fnServerData":function (sSource, aoData, fnCallback) {
aoData.push({"name":"city", "value":$('input.cityorzip').val(),
"name":"minprice", "value":$('input.minprice').val() });
$.getJSON( sSource, aoData, function(json) {
fnCallback(json)
});
}
});
$("#cityorzip").keyup(function() {
oTable.fnFilter(this.value);
});
});
[/code]
And here's a snippet of my PHP code that is probably relevant:
[code]
$sWhere = " WHERE price > '%".mysql_real_escape_string( $_GET['minprice'])."%'";
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS id, price, listingcity, numbedrooms, numfullbaths, numhal
fbaths
FROM new_listings
$sWhere
$sOrder
$sLimit
";
[/code]
I should also point out that my money column does use "$" and "," in it.
I even tried changing the ">" to an "=" to make sure there would be a valid result, but that didn't work either.
Any idea what I'm doing wrong?
Thanks! Am going to send a donation your way as well.
(Note: I plan to use your range filter plugin when the time comes. For now, this is a proof of concept, trying to get this working.)
Unfortunately, no matter what value I give it, it shows no results. Here's my code:
[code] $(document).ready(function() {
var oTable = $("#results_table").dataTable({
"bJQueryUI": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource":"get_listings.php",
"fnServerData":function (sSource, aoData, fnCallback) {
aoData.push({"name":"city", "value":$('input.cityorzip').val(),
"name":"minprice", "value":$('input.minprice').val() });
$.getJSON( sSource, aoData, function(json) {
fnCallback(json)
});
}
});
$("#cityorzip").keyup(function() {
oTable.fnFilter(this.value);
});
});
[/code]
And here's a snippet of my PHP code that is probably relevant:
[code]
$sWhere = " WHERE price > '%".mysql_real_escape_string( $_GET['minprice'])."%'";
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS id, price, listingcity, numbedrooms, numfullbaths, numhal
fbaths
FROM new_listings
$sWhere
$sOrder
$sLimit
";
[/code]
I should also point out that my money column does use "$" and "," in it.
I even tried changing the ">" to an "=" to make sure there would be a valid result, but that didn't work either.
Any idea what I'm doing wrong?
Thanks! Am going to send a donation your way as well.
(Note: I plan to use your range filter plugin when the time comes. For now, this is a proof of concept, trying to get this working.)
This discussion has been closed.
Replies
The first thing that stands out for me is this little bit:
[code]
aoData.push({"name":"city", "value":$('input.cityorzip').val(),
"name":"minprice", "value":$('input.minprice').val() });
[/code]
You have only one object that you are pushing onto the array there, but the object defined both the 'name' and 'value' properties twice - what happens with that will depend on the Javascript engine...
What you want is this:
[code]
aoData.push({"name":"city", "value":$('input.cityorzip').val() });
aoData.push({"name":"minprice", "value":$('input.minprice').val() });
[/code]
Once you've made that change, if that doesn't fix it (and I'd say it probably won't - but you never know and it's needed anyway!) try printing out the SQL statement which is generated: echo $sQuery . Have a look at that return in Firebug (DataTables will give you a parsing error - that's fine). See if the generated SQL looks sensible (post it here :-) ), and also look for any errors from the SQL engine.
Regards,
Allan
Thank you for the response. Some progress but still stuck. Here's the output of the $sQuery:
[code]
SELECT SQL_CALC_FOUND_ROWS id, listing_id, price, listingcity, numbedrooms, numfullbaths, numhalfbaths
FROM new_lmh_listings
WHERE price > '%undefined%'
ORDER BY price
asc
LIMIT 0, 10
{"sEcho": 1, "iTotalRecords": 5, "iTotalDisplayRecords": 0, "aaData": [] }
[/code]
So the obvious problem here is that "%undefined%".
I also did an echo $_GET['minprice'], but that showed up as undefined as well, which I guess is no surprise given what we're seeing above.
I'm still poking around, but if you have any suggestions I'd much appreciate it. :-)
Thanks!
The other thing that would need to be change it the WHERE statement - at the moment you have wild cards for a condition which can't be wildcarded. Something like:
[code]
$sWhere = " WHERE price > ".mysql_real_escape_string( $_GET['minprice']);
[/code]
Allan
Ah! I fixed the minprice input (and took out the wild card). Now the SQL looks like this:
[code]
SELECT SQL_CALC_FOUND_ROWS id, listing_id, price, listingcity, numbedrooms, numfullbaths, numhalfbaths
FROM new_lmh_listings
WHERE price > '$200,000'
ORDER BY price
asc
LIMIT 0, 10
[/code]
It's still not working, but there's improvement. It now lists everything, regardless of "minprice". Could this be because I'm trying to use a "greater than" with currency? Maybe it's not understanding the dollar sign and comma...
1) If I set a default value in the numbedrooms input, the filtering works against that default value. As soon as I change that value (or remove the default), it does not filter.
2) Is there a reasonable way to get around the currency filtering issue? Or am I going to have to write code to strip the dollar signs, commas, etc...?
Thanks!
[code]
$("#minbeds").keyup(function() {
oTable.fnFilter(this.value);
});
[/code]
However, the value it gets for minbeds is off by one. If I search for a minimum of 3 beds, it searches for a minimum of 4. I worked around this by doing: $numbeds = $_GET['numbeds'] -1; in my PHP. That's a work-around, but is that expected behavior?
Still not sure how to tackle the currency issue.
Thanks again!
I'm still not sure why I need to decrease numbeds by one but, like I said, I have a work-around. :-)
Regards,
Allan