-
Posts
24,604 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
Perhaps $cartidOK = $_SESSION['cartid'] ?? 0; $cartitemOK = $_POST['cartitem'] ?? 0 $quantityOK = isset($_POST['quantity']) && is_numeric($_POST['quantity']); $lockedcard = $_SESSION['lockedcard'] ?? 0; $lockedpaypal = $_SESSION['lockedpaypal'] ?? 0; if ( $cartidOK && $cartitemOK && $quantityOK && !$lockedcard && !$lockedpaypal) { // do it }
-
I would have thought it better to this client-side. This example prompts for address and date moved in, then until date oder than three years is entered it prompts for a previous address <?php // HANDLE THE AJAX REQUEST if (isset($_GET['prevdate'])) { $dt = new DateTime($_GET['prevdate']); $years = $dt->diff(new DateTime())->y; if ($years >= 3) { exit("OK"); } else { exit('<div> <b>Previous Address</b><br> <input type="text" name="prevadd[]" class="prevadd" size="55"> <b>Moved in</b> <input type="date" name="prevdate[]" class="prevdate" onchange="checkDate(this)"> </div>'); } } ?> <!DOCTYPE html> <html> <head> <title>Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> function checkDate(dobj) { var date = $(dobj).val() $.get( "", {"prevdate":date}, function(resp) { if (resp != "OK") { $("#prevaddresses").append(resp) } }, "TEXT" ) } </script> <style type="text/css"> #prevaddresses { padding: 16px; border: 1px solid gray; width: 600px; margin: 16px auto; } </style> </head> <body> <h1>Example</h1> <div id="prevaddresses"> <div> <b>Address</b><br> <input type="text" name="prevadd[]" class="prevadd" size="55"> <b>Moved in</b> <input type="date" name="prevdate[]" class="prevdate" onchange="checkDate(this)"> </div> </div> </body> </html> It could be pure JS but I used ajax to take advantage of PHP's date arithmetic.
-
Can't say as you are the only person who knows what, precisely, you are trying to specify.
-
Make sure to use parentheses to enforce the correct logic when mixng AND with OR Instead of A AND B OR C specify (A AND B) OR C or A AND (B OR C)
-
https://css-tricks.com/almanac/properties/p/page-break/ Or write a custom script using FPDF or similar so you have complete control
-
https://www.php.net/datePeriod
-
Then just put that week's dates in the date table. The CROSS JOIN of staff and date gives a row for every date for each staff member that you want to check. staff date staff CROSS JOIN date ----- ----- --------------------- A 1 A 1 B 2 A 2 C 3 A 3 4 A 4 5 A 5 B 1 B 2 B 3 B 4 B 5 C 1 C 2 C 3 C 4 C 5 It then LEFT JOINS with the attendance records to see where there is no match (IE they are absent). staff CROSS JOIN date staff CROSS JOIN date attendance_record LEFT JOIN attendance_record --------------------- ----------------- --------------------------- A 1 A 1 A 1 A 1 A 2 A 3 A 2 NULL NULL A 3 A 4 A 3 A 3 A 4 A 5 A 4 A 4 A 5 B 1 A 5 A 5 B 1 B 2 B 1 B 1 B 2 B 4 B 2 B 2 B 3 B 5 B 3 NULL NULL B 4 C 1 B 4 B 4 B 5 C 2 B 5 B 5 C 1 C 3 C 1 C 1 C 2 C 5 C 2 C 2 C 3 C 3 C 3 C 4 C 4 NULL NULL C 5 C 5 C 5
-
What do think the date table is for? But OK, have it your way. Bye.
-
$sdate1 and $edate1 would define the date range to put in the date table.
-
You would need to test for the oracleid in the staff table IE WHERE s.oracleid = ? AND a.oracleid IS NULL since it is doing a LEFT JOIN the attendance_records table (alias a) to find missing dates.
-
To insert a record you need to execute the sql, not just define a string.
-
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Lang" content="en"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <title>Example</title> <script type="text/javascript"> $().ready( function() { $("#firstname").change( function() { makeUsername() }) $("#surname").change( function() { makeUsername() }) }) function makeUsername() { var a = $("#surname").val().substring(0,8).toLowerCase() var b = $("#firstname").val().substring(0,1).toLowerCase() $("#username").val(a+b) } </script> </head> <body> <form> <label for="firstname"> <i class="fa fa-user"></i> </label> <input type="text" name="firstname" placeholder="Firstname" id="firstname" required><br> <label for="surname"> <i class="fa fa-user"></i> </label> <input type="text" name="surname" placeholder="Surname" id="surname" required><br> <label for="username"> <i class="fa fa-user"></i> </label> <input type="text" name="username" placeholder="Username" id="username" required readonly><br> </body> <label for="password"> <i class="fa fa-lock"></i> </label> <input type="password" name="password" placeholder="Password" id="password" required> </form> </body> </html> Now, is there anything else I can do for you? Cut up your food into bite-size pieces maybe?
-
javascript, for example ... var a = $("#surname").val().substring(0,8).toLowerCase() var b = $("#firstname").val().substring(0,1).toLowerCase() $("#username").val(a+b)
-
How do I use prepared statements with functions and classes
Barand replied to Stoffer's topic in PHP Coding Help
Your saveWorkout class has no database connection -
Because we are looking for missing dates. It's null because it isn't there. If it were there, they would not be absent. Read about LEFT JOINS.
-
You need to set a few attributes when you create your PDO connection. $conn = new PDO("mysql:host=$servername;dbname=timeclock", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); Then you will be notified of any sql problems. Did you read my edit to earlier post about subtracting 1 day from the dates (to get Sun - Thu working week)?
-
I haven't a clue. Which foreach() is it complaining about?
-
You can set the start date with $start_date = new DateTime('2020-03-01'); Set the $incr value with $incr = new DateInterval('P1D'); You will now get every day in the DatePeriod so you now have two choices: Load every day (as I am) but then use a DELETE query to remove Fridays and Saturdays, or Don't add Fri and Sat when iterating through the date period values. EDIT: On further thought, my code writes Mon-Fri dates so if you always subtract 1 day when writing to the date table, you should get Sun-Thu dates. foreach ($period as $d) { $d->sub(new DateInterval('P1D')); // subtract one day before adding to table $dates[] = "('{$d->format('Y-m-d')}')" ; }
-
I thought you might be able manage that bit. Here's a fuller version <?php $month = 'March'; $start_date = new DateTime("first monday of $month"); $incr = DateInterval::createFromDateString('next weekday'); $period = new DatePeriod($start_date, $incr, new DateTime()); // create temporary date table $conn->exec("CREATE TEMPORARY TABLE date (date date not null primary key)"); // populate it foreach ($period as $d) { $dates[] = "('{$d->format('Y-m-d')}')" ; } $conn->exec("INSERT INTO date VALUES " . join(',', $dates)); // now get the days absent $res = $conn->query("SELECT s.oracleid , s.name , date_format(date, '%W %d/%m/%Y') as absent FROM attendance_staff s CROSS JOIN date d LEFT JOIN attendance_record a ON s.oracleid = a.oracleid AND d.date = DATE(a.clockingindate) WHERE a.oracleid IS NULL ORDER BY s.oracleid, d.date "); $tdata = ''; foreach ($res as $r) { $tdata .= "<tr><td>" . join('</td><td>', $r) . "</td></tr>\n"; } ?> <html> <head> <title>Example</title> <style type="text/css"> table { border-collapse: collapse; width: 600px; } th, td { padding: 8px; } th { background-color: black; color: white; } </style> </head> <body> <table border='1'> <tr><th>ID</th><th>Name</th><th>Absent</th></tr> <?=$tdata?> </table> </body> </html>
-
As it says on the tin - it's a temporary table. It lasts until the connection closes, which is whe when the script terminates,
-
For the record, here's how SAMPLE INPUT attendance_record staff +----------+---------------------+---------------------+ +----------+--------+-------------+ | oracleid | clockingindate | clockingoutdate | | oracleid | name | designation | +----------+---------------------+---------------------+ +----------+--------+-------------+ | 533349 | 2020-03-02 09:00:00 | 2020-03-02 14:00:00 | | 533349 | Rami | DT Teacher | | 533349 | 2020-03-03 09:00:00 | 2020-03-03 11:00:00 | | 533355 | Fatima | CS Teacher | | 533349 | 2020-03-04 09:00:00 | 2020-03-04 10:00:00 | +----------+--------+-------------+ | 533349 | 2020-03-09 09:00:00 | 2020-03-09 13:00:00 | | 533349 | 2020-03-10 09:00:00 | 2020-03-10 11:00:00 | | 533349 | 2020-03-11 09:00:00 | 2020-03-11 15:00:00 | | 533349 | 2020-03-12 09:00:00 | 2020-03-12 12:00:00 | | 533349 | 2020-03-13 09:00:00 | 2020-03-13 15:00:00 | | 533349 | 2020-03-16 11:52:31 | 2020-03-16 17:52:52 | | 533349 | 2020-03-18 07:59:58 | 2020-03-18 15:00:10 | | 533349 | 2020-03-23 09:00:00 | 2020-03-23 14:00:00 | | 533349 | 2020-03-24 09:00:00 | 2020-03-24 13:00:00 | | 533355 | 2020-03-02 09:00:00 | 2020-03-02 16:00:00 | | 533355 | 2020-03-03 09:00:00 | 2020-03-03 16:00:00 | | 533355 | 2020-03-04 09:00:00 | 2020-03-04 11:00:00 | | 533355 | 2020-03-05 09:00:00 | 2020-03-05 14:00:00 | | 533355 | 2020-03-06 09:00:00 | 2020-03-06 14:00:00 | | 533355 | 2020-03-10 09:00:00 | 2020-03-10 15:00:00 | | 533355 | 2020-03-11 09:00:00 | 2020-03-11 15:00:00 | | 533355 | 2020-03-12 09:00:00 | 2020-03-12 13:00:00 | | 533355 | 2020-03-13 09:00:00 | 2020-03-13 12:00:00 | | 533355 | 2020-03-16 00:26:08 | 2020-03-16 00:26:17 | | 533355 | 2020-03-16 02:50:27 | 2020-03-16 11:50:49 | | 533355 | 2020-03-23 09:00:00 | 2020-03-23 15:00:00 | | 533355 | 2020-03-24 09:00:00 | 2020-03-24 16:00:00 | +----------+---------------------+---------------------+ CODE $month = 'March'; $start_date = new DateTime("first monday of $month"); $incr = DateInterval::createFromDateString('next weekday'); $period = new DatePeriod($start_date, $incr, new DateTime()); // create temporary date table $conn->exec("CREATE TEMPORARY TABLE date (date date not null primary key)"); // populate it foreach ($period as $d) { $dates[] = "('{$d->format('Y-m-d')}')" ; } $conn->exec("INSERT INTO date VALUES " . join(',', $dates)); // now get the days absent $res = $conn->query("SELECT s.oracleid , s.name , date_format(date, '%W %d/%m/%Y') as absent FROM staff s CROSS JOIN date d LEFT JOIN attendance_record a ON s.oracleid = a.oracleid AND d.date = DATE(a.clockingindate) WHERE a.oracleid IS NULL ORDER BY s.oracleid, d.date "); QUERY RESULTS +----------+--------+----------------------+ | oracleid | name | absent | +----------+--------+----------------------+ | 533349 | Rami | Thursday 05/03/2020 | | 533349 | Rami | Friday 06/03/2020 | | 533349 | Rami | Tuesday 17/03/2020 | | 533349 | Rami | Thursday 19/03/2020 | | 533349 | Rami | Friday 20/03/2020 | | 533349 | Rami | Wednesday 25/03/2020 | | 533355 | Fatima | Monday 09/03/2020 | | 533355 | Fatima | Tuesday 17/03/2020 | | 533355 | Fatima | Wednesday 18/03/2020 | | 533355 | Fatima | Thursday 19/03/2020 | | 533355 | Fatima | Friday 20/03/2020 | | 533355 | Fatima | Wednesday 25/03/2020 | +----------+--------+----------------------+
-
1 ) Why do you need it as "Tuesday 24-3-2020" to add it to the DB. The only date format you should use in a database id yyyy-mm-dd. 2 ) Why do you need to store it at all? If you are storing the dates when they logged in you easily find the dates when they didn't.
-
One way is reorganise your form's field naming convention. Here is an example which will send the data in the format you want, IE Array ( [1] => Array ( [weight] => W1 [repetition] => R1 [field_id] => F1 [exercise] => E1 [planned_workout_id] => P1 ) [2] => Array ( [weight] => W2 [repetition] => R2 [field_id] => F2 [exercise] => E2 [planned_workout_id] => P2 ) [3] => Array ( [weight] => W3 [repetition] => R3 [field_id] => F3 [exercise] => E3 [planned_workout_id] => P3 ) [4] => Array ( [weight] => W4 [repetition] => R4 [field_id] => F4 [exercise] => E4 [planned_workout_id] => P4 ) [5] => Array ( [weight] => W5 [repetition] => R5 [field_id] => F5 [exercise] => E5 [planned_workout_id] => P5 ) ) Example code <?php if ($_SERVER['REQUEST_METHOD']=='POST') { // handle the AJAX request if (isset($_POST['data'])) { exit(print_r($_POST['data'], 1)); // and return the data as the response } } ?> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { $("#btnSend").click( function() { $.post( "", // send ajax request to self $("#form1").serialize(), function(resp) { $("#output").html(resp) }, "TEXT" ) }) }) </script> </head> <body> <form id="form1"> <?php for ($i=1; $i<=5; $i++) { echo "Weight : <input type='text' name='data[$i][weight]' value='W$i'><br> Repetition : <input type='text' name='data[$i][repetition]' value='R$i'><br> Field_id : <input type='text' name='data[$i][field_id]' value='F$i'><br> Exercise : <input type='text' name='data[$i][exercise]' value='E$i'><br> Planned workout : <input type='text' name='data[$i][planned_workout_id]' value='P$i'><hr> "; } ?> </form> <button id="btnSend">Send</button> <br> <h3>Data received from form:</h3> <textarea cols="50" rows="50" id="output"></textarea> </body> </html>
-
That's because there is an "entry_id" column in both tables. You need to specify which one (as I did) SELECT e.entry_id ^