multiple days of week selection from Javascript checkbox to PHP and SQL - php

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.

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?

Related

For loop stopping before get to number needed

My idea is to make tooltip for new users on website. They will go through the tooltips and each time they complete a tooltip it inserts into DB.
However i've got a button which is skip all. So my idea is to insert all the reminder tooltips which they've not completed into the DB with for loop.
So it works if there has been no tooltips clicked already. So the tooltip would be equal to 1 because coming through the $data is equal to 0. However if the tooltip is equal to 1 when passed through $data it gets a +1 and the for loop doesn't seem to post anything into database
$NumberOfTooltips = 2;
(int)$data->tooltipLastID = ((int)$data->tooltipLastID === 0) ? 1 : (int)$data->tooltipLastID + 1;
for ($x = (int)$data->tooltipLastID; $x <= $NumberOfTooltips; $x++) {
$query = "";
$query = "INSERT INTO tooltips ";
$query .= "(tooltip_id, inserted) VALUES ($x, NOW())";
database::query($query);
$this->id = database::getInsertID();
}
On the broken loop the value of (int)$data->tooltipLastID is 2
Is it because the (int)$data->tooltipLastID is already equal to $NumberOfTooltips?
General improvements
These do not directly solve the question asked but they do give you a helping hand in clarifying your data and steaming out any secondary bugs and bloopers. Also pointing out some best (or at least, better) practise.
$x is a lazy and poorly defined counter. Prefer using descriptve veraibles such as $tooltipCounter
$data->tooltipLastID should not start at 1; use the same syntax as every other integer number system in PHP/programming and start at zero. If you need a one then add +1 only when it's needed (VALUES (".$x+1.")).
$NumberOfTooltips = 2; The number 2 is probably not high enough for adequate testing.
var_dump($data->tooltipLastID) and var_dump($NumberOfTooltips) to check both values are what you expect.
Rewrite the test code to take the variables out of the code so that you can check your Database connction works correctly (such as if you're trying to insert into a string field-type by mistake)
$query = ""; is redundant.
You should not need to type cast (int) your object variables ($data->whatever) all the time but type cast them when they're set.
Also by adding +1 to a variable PHP automatically recasts the variable as an int anyway.
Check that your $data->tooltipLastID is publicly accessible/writable.
You use $this ; so which class are you in? Are you self referencing the data class?
A bank holiday is just one day.
It is better the inserted Database column is set by the database automatically upon insert. You can use this SQL to alter your current table:
ALTER TABLE <yourTable> MODIFY COLUMN inserted timestamp DEFAULT CURRENT_TIMESTAMP
Check the type of $data->tooltipLastID? And plz use var_dump($data->tooltipLastID) and var_dump((int)$data->tooltipLastID) before the for loop to see what indeed the original value and the $x is.
Strange type casts will result in strange bugs...

Mysql INSERT SET type

Sorry for my English but it is not my native language.
I have created a user interface to insert data to MySQL. Everything except one thing is ok but when I want to read data from multiple checkboxes and write them to SET type in MySQL it just doesn't work. I have tried to find the answer but after 4 hours I can't find it or I don't understand it.
http://jyxo.info/uploads/21/21b104df77f6ca723bb708d8d0549af5430e8e91.jpg
dobaVyskytu is SET type and there are in with month you can find mushroom(my tema is online atlas of mushrooms)
in user interfacei have 12 checkbox for 12 month.
http://jyxo.info/uploads/FD/fd548760b155307dfa677ada7c4be4996abf7b93.png
In dobavyskytu i need to have multiple select and that is reason why i use $doba +=
if(isset($_POST["Leden"]))
{
$doba += "Leden";
}
if(isset($_POST["Únor"]))
{
$doba += "Únor";
}
if(isset($_POST["Březen"]))
{
$doba += "Březen";
}
Db::query("INSERT INTO houby(nazev,dobaVyskytu,mistoVyskytu,popis,jedovatost,img)VALUES(?,?,?,?,?,?)",$nazev,$doba,$misto,$popis,$jedovatost,$foto);
Thank you all for reading and for help because it works now.
For strings in PHP, it uses . as concatanation not +, so
$doba .= "Leden";
Edit:
For a better way of doing this, you should try something like...
$options = [];
if(isset($_POST["Leden"]))
{
$options[] = "Leden";
}
if(isset($_POST["Únor"]))
{
$options[] = "Únor";
}
...
$doba = implode(',', $options);
As this will give you something like Leden,Únor
My hypotheses are:
$doba is the variable you want to insert in your SET type column (I translated and it seems the values you put as example in your question is Slovak for "January", "February", "March" -- I suppose there could be more).
I suppose that your SET type column is "dobaVyskytu" and that you created it correctly in MySQL by including all the possible values in the column definition.
(Your question update seem to confirm my hypotheses!)
First, when you want to insert multiple values in a SET type column in MySQL, the string value has to be separated with commas.
With the code I see, you can end up with that string "LedenÚnorBřezen" (I suppose you use += for string concatenation, but you should really use .= like Nigel Ren mentionned). You really want to end up with a string like "Leden,Únor,Březen" if all the 3 values you show are checked in your form.
See here for how to handle SET type in MySQL:
https://dev.mysql.com/doc/refman/5.7/en/set.html
Since you do not know if you will end up with 0 or multiple values for that column, I would suggest to make $doba an array.
$doba = array(); // depending on your PHP version, you can also write $doba = [];
After, you can add your values this way (the syntax $array[] = 'value' will apprend a value to the array):
$doba[] = "Leden";
$doba[] = "Únor";
$doba[] = "Březen";
Then, before inserting it, you can convert the array to a string with the values separated by commas that way:
$csvDoba = implode(',', $doba);
Then use $csvDoba instead of $doba in your Db::query() line.
After you get this working, here are more things you can look for to improve your code:
You can also take advantage PHP magic by naming your form checkbox with a special name to avoid repeating yourself.
For example, you can name all your checkboxes with the name "doba[]", and if (isset($_POST["doba"]), it will already be an array with all the checked values! But beware, if no value is checked, it won't be set. That way, you will avoid doing an if condition for each of your checkbox.
You can do something like this in your code to retrieve the value:
$doba = isset($_POST['doba']) ? (array) $_POST['doba'] : array();
What this do?
If any checkboxes named "doba[]" is checked, then you will retrieve them and make sure the value you retrieve is of type array, the "(array)" part for the value to be an array even if it was not (e.g., an error or someone trying to hack your form). Else you will return an empty array (as no choices has been put).
If you are not familar with this syntax, do a searcch for "ternary operator".
You will of course want to do some validation of your values if not already done
You might look to put the values in another table instead of using the "SET type", but that is up to you and at this stade you probably still have a couple stuff to learn, so I don't want to flood you with too much info. ;-)

group mysql results into two separate divs

I have a mysql query that retrieves all my topic results. I then have a pagination system where results are separated into pages and the query's limit #,# changes based on what page you are on.
What I want to do is put all those results into two separate div containers. I want 21 results on each page. The first 9 I will put in one div. The next 12 will go in the other. Does anyone know an efficient way to do this? Should I use two queries, or javascript, or another way? I am just looking for the best most efficient way to do this. Unfortunately the pagination system makes two queries difficult. Any suggestions highly appreciated.
$sql = "SELECT * FROM topics LIMIT ?,?";
$stmt2 = $conn->prepare($sql);
$result=$stmt2->execute(array(somenumber,somenumber2));
I don't see any reason why you can't do a single MySQL query and use JavaScript to sort the results. Understand that I don't understand here what your data is coming back looking like, so any example I provide will have to remain pretty agnostic in this regard.
I will, however, assert as an assumption that you have a JavaScript array of length 21 with some data that is the basis for your display.
Assuming that we're just talking about the first 9, and the last 12, the sorting code is as simple as:
// assume my_array is the array mentioned above
for (var i = 0; i < 9; i += 1) {
var html = code_to_transform_data_from_array(array[i]);
$('.div1').append($(html));
}
for (var i = 9; i < 21; i += 1) {
var html = code_to_transform_data_from_array_b(array[i]);
$('.div2').append($(html));
}
If your sorting condition is any more complicated, then you'd be better off with something like...
while (my_array.length > 0) {
var item = my_array.pop();
if (sorting_condition) {
$('.div1').append(f1(item));
}
else {
$('.div2').append(f2(item));
}
}
(In the second example, I became a lazy typist and assumed f1 and f2 to be complete transformation functions. sorting_condition is your criteria for determining in which bucket something goes.
Hope that sets you off on the right track.

Communicate between PHP and Javascript

I have a table in database that has 2 columns Name | Age, I display it in a HTML page.
I want to sort the table in HTML page based on a field when the user clicks on it.
I have a PHP function to do the sorting based on a field.
But after obtaining the rows in sorted order in PHP, I'm looking for ways by which I can update the HTML table without navigating away from the page.
You do not need to communicate between the client and server to do this, just sort the table on the client directly.
There is a jQuery plug-in for this that works quite well:
http://tablesorter.com/docs/
You can do sorting in javascript, without having to communicate with the server. For example, this code will sort a table based on the content of the Nth column:
function sortTable(table, column, skipHeader) {
// Stick each row into an array.
var rows = [];
for (var i = skipHeader ? 1 : 0; i < table.rows.length; i++) {
rows.push(table.rows[i]);
}
// Sort the array based on the innerText of the column'th cell in each row
rows.sort(function(a, b){
a = a.cells[column].innerText;
b = b.cells[column].innerText;
return a < b ? -1 : (b < a ? 1 : 0);
});
// Re-order the rows by removing/appending in the sort order
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var container = row.parentElement;
container.removeChild(row);
container.appendChild(row);
}
}
For example, to sort the first table in the document, on the first column, and skip the header row:
sortTable(document.getElementsByTagName('table')[0], 0, true);
Obviously you'll want to modify this to suit your own tastes, especially the sorting, but it's a lot simpler than having to post the data back to the server, which I think is what you're proposing.
Since others have covered the fact that client-side sorting would work just fine here, I'll just point you to the resource with which I've had the most sucess doing this kind of thing: Google Data Tables, part of their Visualization Library. Here are the deets on what you can do (spoiler: everything you want and more).
Here is a link to a javascript library to make your tables sortable using javascript instead of php. I've used it many times, it works great.
Javascript Sortable Tables by: Stuart Langridge

get max value in php (instead of mysql)

I have two msyql tables, Badges and Events. I use a join to find all the events and return the badge info for that event (title & description) using the following code:
SELECT COUNT(Badges.badge_ID) AS
badge_count,title,Badges.description
FROM Badges JOIN Events ON
Badges.badge_id=Events.badge_id GROUP
BY title ASC
In addition to the counts, I need to know the value of the event with the most entries. I thought I'd do this in php with the max() function, but I had trouble getting that to work correctly. So, I decided I could get the same result by modifying the above query by using "ORDER BY badgecount DESC LIMIT 1," which returns an array of a single element, whose value is the highest count total of all the events.
While this solution works well for me, I'm curious if it is taking more resources to make 2 calls to the server (b/c I'm now using two queries) instead of working it out in php. If I did do it in php, how could I get the max value of a particular item in an associative array (it would be nice to be able to return the key and the value, if possible)?
EDIT:
OK, it's amazing what a few hours of rest will do for the mind. I opened up my code this morning, and made a simple modification to the code, which worked out for me. I simply created a variable on the count field and, if the new one was greater than the old one, changed it to the new value (see the "if" statement in the following code):
if ( $c > $highestCount ) {
$highestCount = $c; }
This might again lead to a "religious war", but I would go with the two queries version. To me it is cleaner to have data handling in the database as much as possible. In the long run, query caching, etc.. would even out the overhead caused by the extra query.
Anyway, to get the max in PHP, you simply need to iterate over your $results array:
getMax($results) {
if (count($results) == 0) {
return NULL;
}
$max = reset($results);
for($results as $elem) {
if ($max < $elem) { // need to do specific comparison here
$max = $elem;
}
}
return $max;
}

Categories