-
Posts
24,584 -
Joined
-
Last visited
-
Days Won
826
Everything posted by Barand
-
-
Select "Developer tools" in browser menu Open network tab. Instigate the AJAX request When ajax call appears in network window, click on it Select response tab to response or request tab to see request
-
Have you checked your network responses in your browser's developr tools?
-
Unchecked checkboxes are not posted. I prefer to use a the null coalescing operator (??) when handling checkboxes EG $Bold = $_POST['Bold'] ?? 0; //if not set, default to '0'
-
Urgent help needed in php image validation
Barand replied to Dealmightyera's topic in PHP Coding Help
See https://www.php.net/manual/en/features.file-upload.php -
<style type='text/css'> .lr-border { display: inline-block; border-left: 3px solid gray; border-right: 3px solid gray; padding: 0 8px; margin: 30px 10px; } </style> <div class='lr-border'> Are you wanting something like this? </div> <br> <div class='lr-border'> ( where the width adjusts automatically with the text length ) </div>
-
... or get a larger monitor
-
-
Of course you should create a table for the students' answers, mainly because it gives an audit trail for the student's grade and provides a historical record of their past performance. (And if every student gave the same wrong answer you have evidence of poor teacher performance or cheating in the exam )
-
Welcome. I hope you enjoy the journey.
-
I am not prepared to do any more guessing without the data that is being used. Whether your code and query is wrong, and why, depends on the data is is used with.
-
You need to escape the backslash REPLACE(TextField, '\\n', '<br>') Example SELECT title FROM project WHERE id = 2; +----------------------+ | title | +----------------------+ | Project \n number 2 | +----------------------+ SELECT REPLACE(title, '\\n', '<br>') FROM project WHERE id = 2; +-------------------------------+ | REPLACE(title, '\\n', '<br>') | +-------------------------------+ | Project <br> number 2 | +-------------------------------+ Now you can worry about why your update process is writing two separate characters instead of a single linefeed.
-
I had to create my own test data (thanks for that) but naturally I don't know how it conforms with yours. TABLE: product TABLE: bookingItem +----+-------------+-----------+--------+ +----+-----------+---------------------+---------------------+----------+ | id | productName | category | status | | id | productid | startTime | endTime | quantity | +----+-------------+-----------+--------+ +----+-----------+---------------------+---------------------+----------+ | 1 | Room 1 | Guestroom | Active | | 1 | 1 | 2024-01-01 11:32:01 | 2024-01-02 11:32:59 | 1 | | 2 | Room 2 | Guestroom | Active | | 2 | 2 | 2024-02-01 11:34:08 | 2024-02-03 11:34:24 | 2 | | 3 | Room 3 | Guestroom | Active | | 3 | 3 | 2024-03-01 11:34:56 | 2024-03-04 11:35:08 | 3 | | 4 | Room 4 | Guestroom | NULL | | 4 | 2 | 2024-04-01 12:20:20 | 2024-04-07 12:20:41 | 6 | | 5 | Room 5 | Guestroom | NULL | | 5 | 3 | 2024-05-01 01:21:49 | 2024-05-05 12:21:58 | 4 | +----+-------------+-----------+--------+ | 6 | 5 | 2024-06-19 12:23:03 | 2024-06-29 12:23:28 | 10 | | 7 | 2 | 2024-06-01 13:02:51 | 2024-06-15 13:03:16 | 14 | +----+-----------+---------------------+---------------------+----------+ On running your code with my data I get these results for Q1 and Q2. I have written the correct totals in red. As you can see there is a distinct pattern - your totals are the correct totals squared. However, I could not spot any multiplication in the code (I ran as separate query to confirm the correct totals) I have to say, in your code you really make a meal of those dates in the years and quarters considering that SQL can handle it easily. Here's my version... <?php ############################################### # CREATE YOUR OWN PDO DATABASE CONNECTION # # # require 'db_inc.php'; $pdo = mdbConnect('db1'); # # # # ############################################### $range = [ '2020-01-01', '2024-07-31' ]; $selectedYear = $_GET['year'] ?? 0; $whereYear = ''; if ($selectedYear) { $whereYear = 'AND YEAR(d.dt) = ?'; $range[] = $selectedYear; } $res = $pdo->prepare("WITH RECURSIVE dates(dt) AS ( SELECT ? UNION ALL SELECT dt + INTERVAL 1 MONTH FROM dates WHERE dt < ? ) SELECT YEAR(d.dt) AS yr , QUARTER(d.dt) as qtr , MONTHNAME(dt) AS mth , productName AS room , COALESCE(SUM(DATEDIFF(endTime, startTime)), '-') AS nights FROM product p CROSS JOIN dates d LEFT JOIN bookingitem b ON b.productid = p.id AND YEAR(d.dt) = YEAR(b.startTime) AND MONTH(d.dt) = MONTH(b.startTime) WHERE p.`status` = 'Active' $whereYear GROUP BY yr, qtr, MONTH(d.dt), p.id "); $res->execute($range); $results = $res->fetchAll(); $rooms = array_unique(array_column($results, 'room')); $theads = "<tr><th>Quarter</th><th>Month</th><th>" . join('</th><th>', $rooms) . "</th><th>Total</th></tr>\n"; ### RESTRUCTURE THE RESULTS ARRAY foreach ($results as $r) { $data[$r['yr']][$r['qtr']][$r['mth']][$r['room']] = $r['nights']; } ?> <!DOCTYPE html> <html lang='en'> <head> <meta charset="utf-8"> <title>Example</title> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <style type='text/css'> table { width: 100%; border-collapse: collapse; } th { background-color: #808080; color: white; padding: 8px; } td { padding: 4px 12px; text-align: right; } .ca { text-align: center; background-color: #EEE; } .la { text-align: left; background-color: #EEE; color: black; } </style> </head> <body> <header class='w3-indigo w3-padding w3-margin-bottom'> <h1>Guestroom Occupancy</h1> </header> <div class='w3-content w3-padding'> <?php ## OUTPUTFROM RESTRUCTURED ARRAY foreach ($data as $yr => $ydata) { echo "<h3>$yr</h3>\n <table border='1'> $theads "; foreach ($ydata as $qtr => $qdata) { $span = 3 + count($rooms); echo "<tr><th class='la' colspan='$span'>Quarter {$qtr}</th></tr>\n"; foreach ($qdata as $mth => $mdata) { echo "<tr><td> </td><td>$mth</td><td>" . join('</td><td>', $mdata) . "</td><td><b>" . array_sum($mdata) . "</b></td></tr>\n"; } } echo "</table>\n"; } ?> </div> </body> </html> Output
-
It would also help immensely if you posted the actual code that gives your claimed results. The code you posted wil not even execute as it contains : SQL syntax error undefined variable values an infinite loop
-
Your first query is redundant - you are joining product and booking_items in the second query. You appear to have ignored @mac_gyver'scomments completely from your previous post. Still no test data so we can't see for ourselves what is happening. Are you saying the purpose of the two queries and all the php code is just to see how many days in each month?
-
The assignment operator is "=", not "==". Instead of the for() loop, why not just use in_array() as I did? function GetColour($Balls,$Ball) { return in_array($Ball, $Balls) ? "#0000ff" : "#c53ba1"; }
-
Alternative approach... <?php $winners = [ 'norm' => [1,23,27,36,48], 'stars' => [8,12] ]; $myNums = [ 'norm' => [1,3,27,38,48], 'stars' => [7,12] ]; $normCorrect = count(array_intersect($winners['norm'], $myNums['norm'])); $starsCorrect = count(array_intersect($winners['stars'], $myNums['stars'])); $results = sprintf ("\n%-15s%3d\n%-15s%3d", 'Normal', $normCorrect, 'Lucky Stars', $starsCorrect); $balls = ''; foreach ($myNums['norm'] as $b) { $bno = str_pad($b, 2, '0', STR_PAD_LEFT); $cls = (in_array($b, $winners['norm'])) ? 'match' : 'nomatch'; $balls .= "<div class='ball $cls'>$bno</div>"; } $balls .= '<br>'; foreach ($myNums['stars'] as $b) { $bno = str_pad($b, 2, '0', STR_PAD_LEFT); $cls = (in_array($b, $winners['stars'])) ? 'matchstar' : 'nomatchstar'; $balls .= "<div class='ball $cls'>$bno</div>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <meta charset="utf-8"> <style type='text/css'> body { font-family: verdana, arial, sans-serif; font-size: 12pt; } .ball { width: 40px; height: 40px; clip-path: circle(40%); text-align: center; display: inline-block; padding-top: 18px; margin: 5px; } .nomatch { background-color: #000000; color: #FFFFFF; } .nomatchstar { background-color: #808080; color: #FFFFFF; } .match { background-color: #006EFC; color: #FFFFFF; } .matchstar { background-color: #EEEE18; color: #000000; } </style> </head> <body> <div> <h3>Results</h3> <pre> <?= $results ?> </pre> <?= $balls ?> </div> </body> </html>
-
Your function is ony interested in the latest draw so why query for every draw since 2004, just LIMIT the query to the 1 row you need instead of fetchong all then discarding the 2,079 that are surplus to requirements. The speed of a query is proportional to the amount of data returned so keep it to the minimum required columns and rows. SELECT N1 FROM EuroMillions ORDER BY Draw DESC LIMIT 1;
-
The purpose of a prepared statement is to avoid placing variable contents directly into the query (and thus avoid SQL injection attacks) by using placeholders and parameters. Using a stored procedure does the same thing. Therefore using a prepared statement inside a stored procedure is a "belt and braces" approach and unnecessary overkill. You should note that your use of prepare is incorrect as you are not using placeholders, but placing the values into the query via concatenation, and is therefore a waste of time anyway.
-
TABLE: student_info +-----+---------+ | id | address | +-----+---------+ | 125 | xyz | | 126 | bbb | +-----+---------+ Procedure DELIMITER $$ CREATE PROCEDURE `Update_studentinfo`( IN p_id INT, IN p_address varchar(20)) BEGIN UPDATE student_info SET address = p_address WHERE id = p_id; select "successful as result"; END$$ DELIMITER ; ; PHP $res = $pdo->query("CALL update_studentinfo(125, 'Shimla')"); echo $res->fetchColumn(); Output successful as result TABLE student_info +-----+---------+ | id | address | +-----+---------+ | 125 | Shimla | | 126 | bbb | +-----+---------+
-
Not allowed. Only values can be passed as parameters, not table or column identifiers. Having variable table names is oftenindicative of a poor database design. Don't insert primary keys (Ans_id) in INSERT statements, let them be created automatically. $stmt = $pdo->prepare("INSERT INTO tablename (answer, answer_image) VALUES (?, ?)"); $stmt->execute( [ $answer, $image_path ] );