0

Is there a quick and elegant way of selecting one or more day(s) of week from Javascript checkbox, move it to PHP and get queries from SQL accordingly after processing, which boxes are checked, in PHP side-e.g. if I checked monday and wednesday, extract (dow from timestamp) = 1 or extract(dow from timestamp) = 3 should be added to my query string on the PHP end-

I had implemented a quick and dirty solution in C++/Qt before -in this question of mine:

Day(s) of week selection from QT checkbox to Postgresql

Then got an answer showing the shortest way to do it- and now I wonder if a similar way of such a short way is possible in PHP.

4

1 回答 1

1

If the checkboxes are given, there aren't too many ways to implement this. Actually it's always the same: Read the values of the selected checkboxes and use them to build the query string.

Example (could be done easier with e.g. jQuery of course...):

var checkboxes = document.getElementById("checkboxes").getElementsByTagName("input"),
    query = [];
for (var i = 0, l = checkboxes.length; i < l; ++i) {
    // Get value of each selected checkbox and build the query string
    if (checkboxes[i].checked) {
        query.push("extract(dow from timestamp) = ", checkboxes[i].value, " or ");
    }
}
if (query.length > 0) {
    // Remove last "or"
    query = query.slice(0, -1);
}
// Join "string builder" array to get the final query string
query = query.join("");
console.log(query);

DEMO (JSFiddle)

I don't quite understand though why you want JavaScript to build the string; SQL runs server-side, so why not just sending the values to the server and create the query string there?

于 2012-10-09T21:29:45.887 回答