-
Posts
24,602 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
Have you tried my suggestion?
-
Probably because of the sequence of events. I suspect it is ... page loads <script> is run client/list is loaded Try moving the code from <script> to the ready() function after the load so that the elements are loaded before you create their event handlers..
-
Inserting multiple values for one user in new column
Barand replied to PNewCode's topic in PHP Coding Help
The correct way to do it is use a JOIN in a query when you need the reqid, not to duplicate values from one table into another. -
Your "( .. )" are in the wrong places.
- 7 replies
-
- php
- array_search
-
(and 1 more)
Tagged with:
-
I agree with Gizmola^. Using a multidimensional array ... $probs = [ 'QB' => [ 'RB' => [ 1 => 11.22, 2 => 3.91, 3 => 0.15 ], 'TE' => [ 1 => 5.17, 2 => 0.80, 3 => 0.00 ], 'QB' => [ 1 => 9.44, 2 => 0.00, 3 => 0.00 ], 'WR' => [ 1 => 10.46, 2 => 8.67, 3 => 4.53 ], 'K' => [ 1 => 3.81, 2 => 0.19, 3 => 0.00 ], 'D' => [ 1 => 2.85, 2 => 0.07, 3 => 0.00 ] ] // rinse and repeat with the other MVPs and FLEXs ]; $lineup = 'QB, WR, WR, RB, K'; $arr = array_map('trim', explode(',', $lineup)); $mvp = array_shift($arr); $counts = array_count_values($arr); $probability = 0; foreach ($counts as $p => $n) { $probability += $probs[$mvp][$p][$n]; } echo '<br>'.$probability; // 23.7
-
Student subject Positioning based on score using php and mysql database
Barand replied to sule's topic in PHP Coding Help
I won't know until I get some useable data (table dump) to test with. Then I will gladly post a picture of the solution. -
Student subject Positioning based on score using php and mysql database
Barand replied to sule's topic in PHP Coding Help
I'd love to help but my query is having difficulty reading that picture of your data. -
My earlier post, showing 5 night occupied, was wrong. If one arrives on the 1st and departs on the 5th then the room is occupied on only 4 nights (1st, 2nd, 3rd, 4th) so you would ... LEFT JOIN ON r.room_id = rb.room_id AND occupied BETWEEN arrival AND departure - INTERVAL 1 DAY
-
It has a potential effect on any process that uses that table. For example, a search for rooms already booked now becomes a simple search for a date between X and Y instead of looking for overlapping date ranges.
-
I'd opt for CREATE TABLE `room_booking` ( `room_booking_id` int(11) NOT NULL AUTO_INCREMENT, `room_id` int(11) DEFAULT NULL, `booking_id` int(11) DEFAULT NULL, `occupied` date DEFAULT NULL, PRIMARY KEY (`room_booking_id`), UNIQUE KEY `room_id_2` (`room_id`,`occupied`), KEY `booking_id` (`booking_id`) ) ENGINE=InnoDB so a booking for 1st to 5th would be stored as room_id | date_occupied ----------+--------------- 1 | 2023-11-01 1 | 2023-11-02 1 | 2023-11-03 1 | 2023-11-04 1 | 2023-11-05 then the unique constraint on (roomid, occupied) does work. More records but safer.
-
Not sure that constraint is very useful. If you have Room 1 - 1st - 5th then no one else can book the same room from 1st to 5th, however it would allow 2nd to 4th (or any other overlap) which would still be a double booking.
-
ORDER BY level IS NULL, level
-
Whe you join a table with M records to a table with N matching records you get M*N records returned. Therefore you are now totalling Nx more records than before. You need to ensure you join 1 row from 1 tablw with 1 matching record in the other. You can do this with subqueries to get the individal tables' totals. SELECT t1.customer_id , t2.total_sales , t3.amt_paid FROM customers t1 LEFT JOIN ( SELECT customer_id , SUM(total_price) as total_sales FROM tbl_sales GROUP BY customer_id ) t2 USING (customer_id) LEFT JOIN ( SELECT customer_id , SUM(amt_paid) as amt_paid FROM tbl_sales_total WHERE status = 0 GROUP BY customer_id ) t3 USING (customer_id) WHERE t1.customer_id = 489639 Also, you have a LEFT JOIN to tbl_sales_total t3. You therefore cannot use WHERE t3.status = 0. That condition needs to go into the join condition otherwise the query behaves as though it were an ordinary (INNER) JOIN
-
Student subject Positioning based on score using php and mysql database
Barand replied to sule's topic in PHP Coding Help
Hint: Instead of a string of OR conditions it is much simpler to use IN(...) WHERE marks.subjectid IN ('CREATIVE ARTS', 'ENGLISH', 'HISTORY', 'ICT', 'MATHEMATICS', 'SCIENCE', 'O. W. O. P.') -
Something ike ... $positions = [ 1 => [ 'Team A' => 1, 'Team B' => 2, 'Team C' => 3, 'Team D' => 4 ], 2 => [ 'Team C' => 1, 'Team B' => 2, 'Team D' => 3, 'Team A' => 4 ] ]; $result = []; foreach ($positions as $person => $plist) { foreach ($plist as $t => $pos) { if (!isset($result[$t])) { $result[$t] = $pos; } else { $result[$t] += $pos; } } } asort($result); echo '<pre>' . print_r($result, 1) . '</pre>'; outputs... Array ( [Team B] => 4 [Team C] => 4 [Team A] => 5 [Team D] => 7 )
-
if() statements are useful in situations like this $import_data = []; fgetcsv($fp); // discard the header row while ($allrow = fgetcsv($fp)) { $row = array_intersect_key($allrow, $req_cols); if (empty($row[0])) continue; // skip rows where no value in first col $randdays = rand(5,30); $row[2] = date('Y-m-d', strtotime($row[2])); $row[25] = str_replace(',', '', $row[25]); $import_data[] = vsprintf("('%s','%s','%s','%s','%s','%s')", $row); }
-
Do an fgetcsv() prior to the loop to read the header and discard it $import_data = []; fgetcsv($fp); // discard the header row while ($allrow = fgetcsv($fp)) { $row = array_intersect_key($allrow, $req_cols); $randdays = rand(5,30); $row[2] = date('Y-m-d', strtotime($row[2])); $row[25] = str_replace(',', '', $row[25]); $import_data[] = vsprintf("('%s','%s','%s','%s','%s','%s')", $row); } The first row is easy - you know where it is. The last row is more of a problem. Is there a way of recognising it?
-
Mysql has a couple of functions for determining the week number for a given date WEEK(date[, mode]) WEEKOFYEAR(date) - equivalent to WEEK(date, 3) The week number depends on how you define the first week of a year and on whether you want weeks starting on Sunday or Monday. Refer to the manual. mysql> select week('2023-11-17') as wkno; +------+ | wkno | +------+ | 46 | +------+ Having a table for each test type seems a very clunky way of storing your data and has already given you a problem. Consider combining the data... +--------------+ | test_type | +--------------+ | id |------+ | description | | +--------------+ | +--------------+ | | test_log | | +--------------+ | | id | +------<| test_type_id | | date | +--------------+ TABLE: test_type TABLE: test_log +------+----------------------------------+ +-----+--------------+------------+ | id | description | | id | test_type_id | date | +------+----------------------------------+ +-----+--------------+------------+ | 1 | Fire alarm | | 1 | 1 | 2023-01-05 | | 2 | Fire doors | | 2 | 2 | 2023-01-06 | | 3 | Shower head temperatures | | 3 | 5 | 2023-01-06 | | 4 | Wash basin temperatures | | 4 | 3 | 2023-01-07 | | 5 | Health and safety | | 5 | 3 | 2023-01-07 | +------+----------------------------------+ | 6 | 4 | 2023-01-08 | | 7 | 2 | 2023-01-09 | | 8 | 4 | 2023-01-09 | | | | |
-
I want to setup a manual payment confirmation page
Barand replied to Olumide's topic in PHP Coding Help
Probably because you have a query which is searching for a specific semester_id and you are passing it a student_id expecting it to find the student. -
PHP Get Maximum Value From DB and Set Start Value for Loop
Barand replied to experience40's topic in PHP Coding Help
Something like this, maybe.... BEFORE TABLE: stockid; TABLE: product +----+ (empty) | id | +----+ | 1 | | 2 | | 3 | +----+ CSV DATA ------------------------ "A1","A2","A3","A4","A5" "B1","B2","B3","B4","B5" "C1","C2","C3","C4","C5" RUN CODE $fp = fopen('products.csv', 'r'); // prepare product insert query $stmt = $pdo->prepare("INSERT INTO product (stock_id, prod_name) VALUES (?, ?)"); while ($row = fgetcsv($fp)) { $pdo->exec("INSERT INTO stockid (id) VALUES (NULL)"); $stock_id = $pdo->lastInsertId(); // get next stock id foreach ($row as $prod) { $stmt->execute([ $stock_id, $prod ]); } } fclose($fp); AFTER TABLE: stockid; TABLE: product +----+ +----+----------+-----------+ | id | | id | stock_id | prod_name | +----+ +----+----------+-----------+ | 1 | | 1 | 4 | A1 | | 2 | | 2 | 4 | A2 | | 3 | | 3 | 4 | A3 | | 4 | | 4 | 4 | A4 | | 5 | | 5 | 4 | A5 | | 6 | | 6 | 5 | B1 | +----+ | 7 | 5 | B2 | | 8 | 5 | B3 | | 9 | 5 | B4 | | 10 | 5 | B5 | | 11 | 6 | C1 | | 12 | 6 | C2 | | 13 | 6 | C3 | | 14 | 6 | C4 | | 15 | 6 | C5 | +----+----------+-----------+ -
PHP Get Maximum Value From DB and Set Start Value for Loop
Barand replied to experience40's topic in PHP Coding Help
Define the table you are importing into so its stock_id is auto_incementing and leave it to the database. As for your existing function ... Don't connect the db every time you call the function (connecting is the slowest component). Instead, connect once in your script and pass the connection variable to the function when you call it. The function should return the id. Use PDO instead of mysqli - it's better and easier. function get_stock_id($pdo) { $res = $pdo->query("SELECT MAX(stock_id) AS max_stock_id FROM stock_1"); return res->fetchColumn(); } -
Count links on a page, sort them and grade them.
Barand replied to guymclarenza's topic in PHP Coding Help
This thread may help -