PHP POST WITH DATATABLE PAGINATION - php

Sorry if I am repeating the question, I have a HTML FORM page which displays the employee_id & attendance check box (IN, OUT) using datatable.
I made pagination with initial load value in the screen as 10, the data are loaded from mysql DB and all the data loads perfectly with pagination. I will update attendance status of each employee which checkbox and submit the form.
Once i submit the form, i called the PHP _POST (if(isset($_POST['save']))) , inside this i am trying to get the value of all employee_id & checkbox value but, i can able to get only first 10 rows, remaining in page 2,3,4,5 are not available, is there any option to get them, i tried with jquery for each too but it also alerts on first 10 rows.
Please help me out.
I need to post the data to database from data table regardless of pagination either using PHP post method (or) Jquery.

If you using pagination by LIMIT in database query there is, of course, no way you would get more than 10 rows, so, your <form> submit will not give you more than those 10 data arrays you can save to database.
Can you explain why you need to update all rows in database? Because limiting by pagination guarantees that other rows will not be affected (if query is right).

Related

Mysqli php ajax Pagination

can someone please explain to me how to approach this issue?
I have a table with 4000 records, a select option and a search field to filter out the table
however I'm trying to figure out the best way to set up pagination.
1st Approach:
Should I make one query against the database and populate the table with all records and then set up the pagination with javascript so I can be able to search for records that are not only shown on the current page?
2nd Approach
Set up the pagination with PHP, make a query via ajax for each page? however I'm afraid that this approach would not allow me to filter out the table if I need to search for a record that is not on the current page.
1st approach: definitely not, loading large number of results for nothing is not recommended.
2nd approach makes more sense. You can use the LIMIT statement inside your SELECT statement to decide which records you want. Ex. if you paginate on 10 elements perpage, you could do
SELECT * FROM Table LIMIT 10
then
SELECT * FROM Table LIMIT 10,10
and so on. Indexes start at 0.
Note that you don't even need Ajax to do that, your next button can specify the offset for the next load of the page. It's a choice based on your knowledge, the size of the rest of the page (minimize load time), ...

inRandomOrder() query with ajax return same elements

I have a query with Laravel
$query = Work::inRandomOrder()->where('orientation', $request->get('orientation')->paginate(11);
And I have a "load more" button who call 11 others works with each click.
But, I would like display my data in a random order. But with this query, he repeats to me several data. So in my list of works, I have 2 or 3 times the same and obviously, it's not good. Is there a way with RandomOrder () to avoid duplicates?
Thank you
I believe data is randomized each time the script is loaded, including each time loading dynamicaly 11 more data fields. One way to achieve this it to randomize, store the data in a user's session variable and then get next elements from it. However this might be very heavy.
I would go to a some easily predictable mathemtics, which will look 'random' to the end-user. For example you can get each N-th entry of a query like 1-st, 5-th, 9-th, 13-th ... if N=4 and store just the number N in the user's session.

mysql - Query to display record just added

I have two pages. One is a form that I use to simply input data that will be sent to my database and the second page that actually takes the data inputted into the form and sends it to the database and is supposed to display the information that I've just added.
Everything works fine, however I'm struggling with the query slightly. What I need it to do is display all the information for the last data inputted to the database.
The query I currently have just displays the data with the highest ID:
$sql = "SELECT * FROM Results ORDER BY ID DESC LIMIT 1";
So as an example I would be left with the following information after completing my form:
Success! Data being saved:
ID = 900 Amount = 206 Date = 2016-12-26
This is obviously just showing the data with the highest ID, but since the ID and all the data fluctuates, I need it to just show the data that has just been inputted.
I came accross this: Query to select newly added records only. But I don't believe this soultion to be viable as the database is external and I don't want to be creating new tables.
I was thinking that it might be possible to assign a hidden value to each newly added record via the query. e.g. New 1, New 2, New 3 etc. Then printing the latest record for New. However, I couldn't find anything on how to do this.
Any help would be greatly appreciated!
You must use this method to have very correct value:
Input form must send to another file that do inserting (we call it here insert.php)
insert.php must insert the data after validation and after that you can fetch the last ID number from database. Depending on the method you are working with it can be different. for example if you are using PDO you can get it by PDO::lastInsertId
after getting the ID you need to forward it to the viewing or editing page. for example view.php?id=LastInsertId. This forward have some reasons:
Codes can be cleaner.
We prevent refresh and resend inserting. for example if you do inserting inside view.php and user hit F5 to refresh the page, The insertion happening again.
This is the whole idea. you can use this method for only one page:
page.php?do=new
page.php?do=insert
forward to the page.php?do=view&id=lastInsertID
why you trying to get just inputted data from database? you can do it using HTTP POST/GET method easily.just send data as parameters and show them in second page.
If you already have the data you are inserting, you don't need to run a query to get it back from the database again, you could just ensure that the query was successful and display the data directly. Anyways:
You can get the insert ID from the last insert using the MySQLi object. For example:
$sql = "<your insert statement>"
$conn->query($sql);
$last_id = $conn->insert_id; //Id of the row you just inserted
$sql = "SELECT * FROM Results WHERE id=$last_id";
This is assuming you do the insert in the same page that you display the result.

PHP/MySQL Update Query

I have a form with several select and text boxes that gets dynamically generated from a database query.
The number of rows (The form lives in a html table) depends on how many records gets returned from the database.
For instance:
Row 1 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
Row 2 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
Row 3 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
...
...
...
Row 10 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
Not all rows are going to be updated all the time. Maybe a user gets a 10-row form and updates only one.
My processing script needs to loop through all rows to figure out which one has changed in order to update. This causes
the update query to run 10 times even if the user only updates one row.
How can I get it to update only the number of rows that has been changed on the form?
Currently this is my processing script (not sanatized yet. Just in test mode)
for($i = 0; $i < $_POST['totalRecords']; $i++){
if($_POST['muStatus'] != $providerMUStatusArray[$i] ||
$_POST['att1IncentivePlanDropDown'!= $providerATT1IncentivePlanArray[$i] ||
$_POST['att1AttestationNumber'!= $providerATT1NumberArray[$i]){
UPDATE QUERY GOES HERE
}//END IF
}//END FOR
Any help is appreciated
This is how I would do it: I would add a hidden field in each row called "Updated" with a start value of 0 then use JQuery or just JavaScript to set it to 1 every time a value in that row is changed.
Even better, I would use AJAX to update each row as soon as the user is done editing it.
I would create a session and store the values you expect. Then when the form is submitted you just check to see if the submitted value equals the session saved value. If it does not equal it then process.

Checkbox filter using Mysql and PHP

In my application there is a filter option with checkboxes.Checkboxes indicates top five categories and all other categories under another single checkbox.That means total I have six checkboxes(top five + other).When all checked which will fetch all values from database table and when all unchecked which never fetch any values from table.Please anybody give any suggestions for my unchecked case...What will be the condition there?
In the unchecked case i wouldn't do a mysql query - cause it is not necessary and you can save that time. Depending on your application and your code you need to implement a switch here and insert the result on your page as if no result (or entry) has been found. For further details more application code is needed.

Categories