-
Posts
24,551 -
Joined
-
Last visited
-
Days Won
821
Community Answers
-
Barand's post in seperate grouped radio button for each photo - keep/delete options was marked as the answer
Think of radio buttons as menu options. They need the same name (so the browser knows they are part of the same menu) but different values that the user selects.
Use the id (image id I presume) as part of the name to group them
<input type="radio" name="action[<?=$result['id']?>]" value="A"> Approve <input type="radio" name="action[<?=$result['id']?>]" value="D"> Delete Then to process the form data
foreach ($_POST['action'] as $id => $action) { if ($action == 'D') { // delete image whose ID is $id } elseif ($action == 'A') { // approve image whose ID is $id } }
-
Barand's post in Loginform with session and Ldap was marked as the answer
That line is setting the session value to an array. did you mean = $_POST['username'] ?
-
Barand's post in How to Mysql count only higest values per row was marked as the answer
The subquery calcultaing the maximum scores .behaves just like a temporary table. So you can the join it to tbale using user and count by country.
SELECT a.country , sum(b.version=1) as V1 , sum(b.version=2) as V2 , sum(b.version=3) as V3 , sum(b.version=4) as V4 , sum(b.version=5) as V5 , sum(b.version=6) as V6 FROM table1 a JOIN ( SELECT user , MAX(version) as version FROM table2 GROUP BY user ) b USING (user) GROUP BY country ORDER BY country DESC; +---------+------+------+------+------+------+------+ | country | V1 | V2 | V3 | V4 | V5 | V6 | +---------+------+------+------+------+------+------+ | USA | 0 | 0 | 0 | 0 | 0 | 1 | | France | 0 | 0 | 2 | 0 | 0 | 0 | +---------+------+------+------+------+------+------+
-
Barand's post in Display options conditionally in <select> element was marked as the answer
This example will repopulate the category menu depending on the selected type
<html> <head> <title>Sample</title> <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> // array of categories for each type var cat_types = JSON.parse('{"A":{"1":"Cat 1","2":"Cat 2","3":"Cat 3"},"B":{"4":"Cat 4","5":"Cat 5","6":"Cat 6"},"C":{"7":"Cat 7","8":"Cat 8","9":"Cat 9"}}'); $().ready(function() { // populate type menu $.each(cat_types, function(k,v) { $("#type").append($("<option>", {"val":k, "text":k})) }) // when type selected, populate cat menu $("#type").change( function() { $("#cat").html("<option value=''> - select category -</option"); var type = $(this).val() $.each(cat_types[type], function(k, v) { $("#cat").append($("<option>", {"val":k, "text":v})) }) }) }) </script> <style type='text/css'> </style> </head> <body> <form> Type <select name='type' id='type'> <option value=""> - select type -</option> </select> <br><br> Cat <select name='cat' id='cat'></select><br> </form> </body> </html>
-
Barand's post in Join error was marked as the answer
Well, the error disappeared when I did it that way.
You can only do INNER JOINS with the FROM A, B, C method. You have to put the join conditions in the WHERE clause which mixes the table structure with selection criteria In my experience, explicit joins are faster. -
Barand's post in Create an array which is based on a given value was marked as the answer
Ah! Two arrays
$amts_due = [ '15' => 1088.00, '13' => 2206.00, '11' => 1716.00, '9' => 1210.00, '7' => 140.00 ]; $amts_paid = []; $payment = 5000.00; ksort($amts_due); // oldest first foreach ($amts_due as $bid => &$due) { if ($payment >= $due) { $payment -= $due; $amts_paid[$bid] = $due; $due = 0; } else { $due -= $payment; $amts_paid[$bid] = $payment; $payment = 0; } } $amts_due = array_filter($amts_due); $amts_paid = array_filter($amts_paid); Giving
Due Array ( [13] => 272 [15] => 1088 ) Paid Array ( [7] => 140 [9] => 1210 [11] => 1716 [13] => 1934 )
-
Barand's post in Introductions was marked as the answer
Good idea. Do you think we should have something like this ?
-
Barand's post in OOP point to array value inside of a stdClass Object was marked as the answer
-> for object properties [] for array keys So
$response->data->result[0]->value
-
Barand's post in Updating Not Working was marked as the answer
I'd suggest something like this
if($_SERVER['REQUEST_METHOD']=='POST') { $post = array_map('trim', $_POST); unset($post['submit']); // not wanted on journey $post['id'] = trim($_SESSION['id']); $post['submittedby'] = trim($_SESSION["username"]); if ($_FILES['image']['error'] = UPLOAD_ERR_OK) { // image file directory $target = "images/".basename($_FILES['image']['name']); if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { $msg = "Image uploaded successfully"; $post['image'] = $target; }else{ $msg = "Failed to upload image"; $post['image'] = null; } } else { $post['image'] = null; } try { $sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, position = :position, role = :role, submittedby = :submittedby, image = :image WHERE id = :id "; $stmt= $db->prepare($sql); $stmt->execute($post); $message = ' <i class="fa fa-check text-success"> Record Updated!</i>'; } catch (PDOException $e) { $message = ' <i class="fa fa-check text-danger"> Something went wrong please contact the server admin.</i>'; } }
-
Barand's post in Randomizing posts in word press - by a random seed + a day variable was marked as the answer
You could just forget about the session value and
SELECT ... ORDER BY RAND(dayofyear(CURDATE()))
-
Barand's post in Hours Worked Do Not WERK was marked as the answer
How you do it depends on how you are entering the data. The are basically two approaches...
Create the timesheet record first then, each day add that days stafftime record Have a form where you enter the timesheet header data and all days' times at once for a user. If you use the first, have a dropdown so the user can select the timesheet record id and write that id with the day's times into stafftime.
Doing it the second way, on posting the form you would first write the timesheet record (id would be an autoincrement column) then call lastInserId() to get the id of that new record. You then insert the time records with that last inserted id as the sheet_id.
-
Barand's post in Not Calculating Correctly was marked as the answer
Never store data with multiple values like this
'$week_ending', '$staff_name', '$monday_start', '$monday_finish', '$tuesday_start', '$tuesday_finish', '$wednesday_start', '$wednesday_finish', '$thursday_start', '$thursday_finish', '$friday_start', '$friday_finish', '$saturday_start', '$saturday_finish', '$sunday_start', '$sunday_finish', '$total_hours', '$timesheet_comment' You should always normalize your data when using an RDBMS. Once that's done, the processing can become a whole lot simpler.
For example, if you table were like this...
TABLE: driver TABLE: driver_timesheet +-----------+-----------+----------+ +--------------+-----------+-------------+-------------------+ | driver_id | firstname | lastname | | timesheet_id | driver_id | week_ending | timesheet_comment | +-----------+-----------+----------+ +--------------+-----------+-------------+-------------------+ | 1 | Peter | Dowt | | 1 | 1 | 2021-09-05 | 4-day week | | 2 | Laura | Norder | ----------------------------------------------------------------< | 2 | 2 | 2021-09-05 | No Saturday work | | 3 | Tom | DiCanari | | 3 | 3 | 2021-09-05 | No Sunday work | +-----------+-----------+----------+ +--------------+-----------+-------------+-------------------+ | | +------------------------------------------------------------------------------------+ TABLE: driver_time | +----------------+--------------+-------------+------------+----------+------------+ | driver_time_id | timesheet_id | driver_date | time_start | time_end | break_time | +----------------+--------------+-------------+------------+----------+------------+ | 1 | 1 | 2021-08-30 | 08:15:00 | 18:05:00 | 01:20:00 | | 2 | 1 | 2021-08-31 | 08:30:00 | 17:35:00 | 01:10:00 | | 3 | 1 | 2021-09-01 | 08:20:00 | 18:00:00 | 01:00:00 | | 4 | 1 | 2021-09-05 | 08:00:00 | 17:55:00 | 01:10:00 | | 19 | 2 | 2021-08-30 | 08:16:00 | 17:17:00 | 01:19:00 | | 20 | 2 | 2021-08-31 | 08:19:00 | 17:25:00 | 01:06:00 | | 21 | 2 | 2021-09-01 | 08:18:00 | 17:41:00 | 01:24:00 | | 22 | 2 | 2021-09-02 | 08:07:00 | 17:26:00 | 01:20:00 | | 23 | 2 | 2021-09-03 | 08:09:00 | 17:21:00 | 01:01:00 | | 25 | 2 | 2021-09-05 | 08:06:00 | 17:46:00 | 01:24:00 | | 26 | 3 | 2021-08-30 | 08:23:00 | 17:38:00 | 01:02:00 | | 27 | 3 | 2021-08-31 | 08:04:00 | 17:23:00 | 01:21:00 | | 28 | 3 | 2021-09-01 | 08:21:00 | 17:30:00 | 01:13:00 | | 29 | 3 | 2021-09-02 | 08:22:00 | 17:36:00 | 01:01:00 | | 30 | 3 | 2021-09-03 | 08:23:00 | 17:32:00 | 01:09:00 | | 31 | 3 | 2021-09-04 | 08:21:00 | 17:05:00 | 01:00:00 | +----------------+--------------+-------------+------------+----------+------------+ then all you need to do is write the data from your input form to the tables.
To get the daily hours worked ...
SELECT concat(firstname,' ', lastname) as name , dayname(driver_date) as day , date_format(driver_date, '%b %d') as date , time_start , time_end , break_time , timediff(timediff(time_end, time_start), break_time) as hrs_worked FROM driver_time JOIN driver_timesheet USING (timesheet_id) JOIN driver USING (driver_id) WHERE week_ending = '2021-09-05' ORDER BY driver_id, driver_date; +--------------+-----------+--------+------------+----------+------------+------------+ | name | day | date | time_start | time_end | break_time | hrs_worked | +--------------+-----------+--------+------------+----------+------------+------------+ | Peter Dowt | Monday | Aug 30 | 08:15:00 | 18:05:00 | 01:20:00 | 08:30:00 | | Peter Dowt | Tuesday | Aug 31 | 08:30:00 | 17:35:00 | 01:10:00 | 07:55:00 | | Peter Dowt | Wednesday | Sep 01 | 08:20:00 | 18:00:00 | 01:00:00 | 08:40:00 | | Peter Dowt | Sunday | Sep 05 | 08:00:00 | 17:55:00 | 01:10:00 | 08:45:00 | | Laura Norder | Monday | Aug 30 | 08:16:00 | 17:17:00 | 01:19:00 | 07:42:00 | | Laura Norder | Tuesday | Aug 31 | 08:19:00 | 17:25:00 | 01:06:00 | 08:00:00 | | Laura Norder | Wednesday | Sep 01 | 08:18:00 | 17:41:00 | 01:24:00 | 07:59:00 | | Laura Norder | Thursday | Sep 02 | 08:07:00 | 17:26:00 | 01:20:00 | 07:59:00 | | Laura Norder | Friday | Sep 03 | 08:09:00 | 17:21:00 | 01:01:00 | 08:11:00 | | Laura Norder | Sunday | Sep 05 | 08:06:00 | 17:46:00 | 01:24:00 | 08:16:00 | | Tom DiCanari | Monday | Aug 30 | 08:23:00 | 17:38:00 | 01:02:00 | 08:13:00 | | Tom DiCanari | Tuesday | Aug 31 | 08:04:00 | 17:23:00 | 01:21:00 | 07:58:00 | | Tom DiCanari | Wednesday | Sep 01 | 08:21:00 | 17:30:00 | 01:13:00 | 07:56:00 | | Tom DiCanari | Thursday | Sep 02 | 08:22:00 | 17:36:00 | 01:01:00 | 08:13:00 | | Tom DiCanari | Friday | Sep 03 | 08:23:00 | 17:32:00 | 01:09:00 | 08:00:00 | | Tom DiCanari | Saturday | Sep 04 | 08:21:00 | 17:05:00 | 01:00:00 | 07:44:00 | +--------------+-----------+--------+------------+----------+------------+------------+ and to get the weekly hours for each driver for payroll ...
SELECT date_format(week_ending, '%d %b %Y') as wk_ending , concat(firstname,' ', lastname) as name , round(sum(time_to_sec( timediff(timediff(time_end, time_start), break_time) ))/3600, 2) as tot_hrs_dec FROM driver_time JOIN driver_timesheet USING (timesheet_id) JOIN driver USING (driver_id) WHERE week_ending = '2021-09-05' GROUP BY week_ending, driver_id +-------------+--------------+-------------+ | wk_ending | name | tot_hrs_dec | +-------------+--------------+-------------+ | 05 Sep 2021 | Peter Dowt | 33.83 | | 05 Sep 2021 | Laura Norder | 48.12 | | 05 Sep 2021 | Tom DiCanari | 48.07 | +-------------+--------------+-------------+ All your calculations done with a couple of queries.
-
Barand's post in json_decode/encode confusion was marked as the answer
This fails
$j = "{'admin': 1, 'moderator': 1}" ; $a = json_decode($j, 1); echo '<pre> a ' . print_r($a, 1) . '</pre>'; This works
$j = '{"admin": 1, "moderator": 1}' ; $b = json_decode($j, 1); echo '<pre> b ' . print_r($b, 1) . '</pre>'; Note the quotes in the JSON string.
-
Barand's post in Unsupported Operand Types? was marked as the answer
PHP v8 doesn't let you mix indeterminate types, such as adding an empty string to an int.
$a = 1; $b = '2'; $c = ''; echo $a + $b; // 3 OK echo $a + $c; // Unsupported operand types: int + string echo $a + (int)$c; // 1 OK Use a cast to int.
$this->hou += (int)$split[0]; That said, you really are doing the calculations the hard way
$t1 = '09:30:00'; $t2 = '17:15:00'; $dt1 = new DateTime($t1); $dt2 = new DateTime($t2); echo $dt2->diff($dt1)->format('%H:%I:%S'); // 07:45:00
-
Barand's post in <div> table and css problems. Doesent do what i want it to do. Seen me blind on it. Please, HELP! was marked as the answer
Perhaps...
<link rel='stylesheet' href='https://www.w3schools.com/w3css/4/w3.css'> <form id="form1"> <div class='w3-container w3-display-topright w3-light-gray w3-third' > <div class='w3-cell-row w3-padding-small'> <div class='w3-container w3-cell' style='width:120px'>Email</div> <div class='w3-container w3-cell'> <input class='w3-input w3-card w3-tiny' type='text' name='email'> </div> </div> <div class='w3-cell-row w3-padding-small'> <div class='w3-container w3-cell' style='width:120px'>Password</div> <div class='w3-container w3-cell'> <input class='w3-input w3-card w3-tiny' type='text' name='password'> </div> </div> <div class='w3-cell-row w3-padding-small'> <div class='w3-container w3-cell w3-right' style='width: 60%'> <button class='w3-btn w3-small w3-light-grey w3-border w3-right'>LogIn</button> </div> </div> </div> </form> Laptop...
Mobile...
-
Barand's post in mySQL query: try for one minute. was marked as the answer
Looks OK. I'd use a "do" loop i this case and count the records found.
$start = time(); $query = $db->prepare("select count(*) from mytable where sku = ?"); do { sleep(1); $query->execute(['XYZ123']); $recs = $query->fetchColumn(); } while ($recs == 0 && time() < $start + 60); echo $recs == 0 ? "Try again later" : "$recs found";
-
Barand's post in UPDATE query not working... was marked as the answer
Alternatively
... WHERE server NOT IN ('gmail.com', 'yahoo.com')
-
Barand's post in Perfect mySQL one liner? was marked as the answer
Not necessarily...
DATA
+-------+-------+ | fruit | price | +-------+-------+ | grape | 5.00 | | melon | NULL | +-------+-------+ Code (PDO)
$var = $db->query("select price from fruit where fruit = 'grape'")->fetch()['price'] ?? ''; echo '<br>Grape: ' . $var; $var = $db->query("select price from fruit where fruit = 'melon'")->fetch()['price'] ?? ''; echo '<br>Melon: ' . $var; $var = $db->query("select price from fruit where fruit = 'lemon'")->fetch()['price'] ?? ''; echo '<br>Lemon: ' . $var; Output (No exceptions were thrown during the running of this code)
Grape: 5.00
Melon: (why no price?)
Lemon: (why no price?)
-
Barand's post in Create a Table from pairs of array was marked as the answer
Rinse and repeat - exchanging u1 and u2
$new = []; foreach ($array as $a) { if (!isset($new[$a['u1']])) { $new[$a['u1']] = []; } $new[$a['u1']][] = $a['u2']; //repeat exchanging u1 and u2 if (!isset($new[$a['u2']])) { $new[$a['u2']] = []; } $new[$a['u2']][] = $a['u1']; } // // Output $new array // echo '<pre>'; foreach ($new as $u1 => $u2s) { printf('<br><b>%4d</b> | ', $u1); foreach ($u2s as $u) { printf('%4d ⋮', $u); } }
-
Barand's post in Easy Forecast Delivery Day code was marked as the answer
I'd revise the function so it just gave a plain array of dates, then nudge the delivery by a day if the due date is the holiday array.
So array is ...
Array ( [0] => 2021-01-01 [1] => 2021-04-02 [2] => 2021-04-05 [3] => 2021-05-03 [4] => 2021-05-31 [5] => 2021-08-30 [6] => 2021-12-25 [7] => 2021-12-26 [8] => 2021-12-27 [9] => 2021-12-28 ) My solution: (If you don't want to be spoon-fed, ignore)
-
Barand's post in Pagination - duplicating data was marked as the answer
Try
$start_from = ($page - 1) * $num_per_page;
-
Barand's post in Display records in a table format dynamically was marked as the answer
Do you mean something like this?
<?php // get the "name" headings that you need for the columns // and also use them as keys in a "template" array // $res = $db->query("SELECT DISTINCT name FROM dataset ORDER BY name "); $names = $res->fetchAll(); $heads = array_column($names, 'name'); $temp = array_fill_keys($heads, ''); $table_header = "<tr><td></td><td class='thead'>Result</td><td class='thead'>" . join("</td><td class='thead'>", $heads) . "</td></tr>\n"; // now get the data // store in an array by "id" // witd subarrays for each name $res = $db->query("SELECT id , edate , result , name , nos FROM maintab m JOIN dataset d ON m.id = d.mid ORDER BY id "); $data = []; foreach ($res as $r) { if (!isset($data[$r['id']])) { $data[$r['id']] = [ 'edate' => $r['edate'], 'result' => $r['result'], 'names' => $temp // the template array from earlier ]; } $data[$r['id']]['names'][$r['name']] = $r['nos']; // put value in tempate array } // now we simply output data array into html table rows $tdata = ''; foreach ($data as $row) { $tdata .= "<tr><td>{$row['edate']}</td><td>{$row['result']}</td><td>" . join('</td><td>', $row['names']) . "</td></tr>\n"; } ?> <html> <head> <title>Example</title> <style type='text/css'> td { padding: 4px 10px; } .thead { font-weight: 600; border-top: 1px solid gray; border-bottom: 1px solid gray; } </style> </head> <body> <table> <?= $table_header ?> <?= $tdata ?> </table> </body> </html> OUTPUT
[edit] PS Sorry about the data typo. That's what happens when people post pictures instead of copyable text.
-
Barand's post in unpacking a binary number (string) was marked as the answer
Still forgetting unpack(), for 8 bits to 8 vars
$binary3 = 0b10110001; for ($k=0; $k<8; $k++) { $vars[$k] = ($binary3 & 2**$k) ? 1:0; } giving
$vars = Array ( [0] => 1 [1] => 0 [2] => 0 [3] => 0 [4] => 1 [5] => 1 [6] => 0 [7] => 1 )