Jump to content

Barand

Moderators
  • Posts

    24,454
  • Joined

  • Last visited

  • Days Won

    809

Everything posted by Barand

  1. In your profile page, click in the top section of the page
  2. 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 )
  3. Welcome. I hope you enjoy the journey.
  4. 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.
  5. 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.
  6. It looks as though it is two separate characters and not a linefeed character (linefeed = 0A) w e c h a r t e r e d a \ n f i s h i n g t r i p . 77 65 20 63 68 61 72 74 65 72 65 64 20 61 20 5C 6E 20 66 69 73 68 69 6E 67 20 74 72 69 70 2E ^^ ^^
  7. 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>&nbsp;</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
  8. 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
  9. Why would you need to? If you want to output it to an html page showing the line breaks, use nl2br() EG echo nl2br( $row['TextField'] );
  10. 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?
  11. 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"; }
  12. 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>
  13. 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;
  14. 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.
  15. 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 | +-----+---------+
  16. 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 ] );
  17. Post the contents of $response.
  18. A neater way would be to have the $_POST keys matching the $neat keys. (EG $_POST['keyone'] and $_POST['keytwo']
  19. The .php extension merely tells your server to processs any php code that may be in the file. In this case there isn't any, so it is just an HTML file.
  20. Perhaps the PHP isn't working because there isn't any PHP code in there
  21. Reloaded your data and ran this query below (for branch 801) to count the customers from each section of the UNION ... billing machine master not in billing SELECT count(distinct customerid) as total_custs , SUM(JanBilling <> '-') as from_billing , SUM(JanBilling = '-') as from_mach_mast FROM ( SELECT branchname , plantName , CustomerID , Customername , CustomerSegment , CustomerType , COALESCE(bill.JanBilling, '-') as JanBilling , COALESCE(bill.FebBilling, '-') as FebBilling , COALESCE(bill.MarBilling, '-') as MarBilling , COALESCE(bill.AprBilling, '-') as AprBilling , COALESCE(bill.MayBilling, '-') as MayBilling , COALESCE(bill.JunBilling, '-') as JunBilling , COALESCE(bill.JulBilling, '-') as JulBilling , COALESCE(bill.AugBilling, '-') as AugBilling , COALESCE(bill.SepBilling, '-') as SepBilling , COALESCE(bill.OctBilling, '-') as OctBilling , COALESCE(bill.NovBilling, '-') as NovBilling , COALESCE(bill.DecBilling, '-') as DecBilling FROM ( SELECT br.branchname, p.plantName , b.sold_party as customerid , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-01' THEN gross_amount ELSE 0 END) / 100000 AS JanBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-02' THEN gross_amount ELSE 0 END) / 100000 AS FebBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-03' THEN gross_amount ELSE 0 END) / 100000 AS MarBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-04' THEN gross_amount ELSE 0 END) / 100000 AS AprBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-05' THEN gross_amount ELSE 0 END) / 100000 AS MayBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-06' THEN gross_amount ELSE 0 END) / 100000 AS JunBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-07' THEN gross_amount ELSE 0 END) / 100000 AS JulBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-08' THEN gross_amount ELSE 0 END) / 100000 AS AugBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-09' THEN gross_amount ELSE 0 END) / 100000 AS SepBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-10' THEN gross_amount ELSE 0 END) / 100000 AS OctBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-11' THEN gross_amount ELSE 0 END) / 100000 AS NovBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-12' THEN gross_amount ELSE 0 END) / 100000 AS DecBilling FROM sbms.billing b JOIN sbms.plant p ON b.sales_office = p.plantcode JOIN sbms.branch br ON p.branchid = br.branchid WHERE b.sales_doc_type IN ('ZIEC', 'ZISC', 'ZEXS', 'ZSP', 'ZISS') AND b.product_div NOT IN ('X1', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'XA', 'XB', 'XC', 'XD', 'XE', 'XG') AND br.branchcode = '801' GROUP BY branchname, customerid UNION SELECT m.branchname, Null , m.customerid , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL FROM sbms.machinemaster m LEFT JOIN sbms.billing b ON m.customerid = b.sold_party WHERE m.machinestatus = 'A' AND b.sold_party IS NULL AND m.branchcode='801' GROUP BY branchcode, customerid ) bill LEFT JOIN sbms.customermaster USING (customerid) ORDER BY branchname, customerid ) data; giving these counts +-------------+--------------+----------------+ | total_custs | from_billing | from_mach_mast | +-------------+--------------+----------------+ | 844 | 609 | 235 | +-------------+--------------+----------------+ 1 row in set (2.95 sec) I also ran this one to get the counts for all branches SELECT branchname , SUM(JanBilling <> '-') as from_billing , SUM(JanBilling = '-') as from_mach_mast FROM ( SELECT branchname , plantName , CustomerID , Customername , CustomerSegment , CustomerType , COALESCE(bill.JanBilling, '-') as JanBilling , COALESCE(bill.FebBilling, '-') as FebBilling , COALESCE(bill.MarBilling, '-') as MarBilling , COALESCE(bill.AprBilling, '-') as AprBilling , COALESCE(bill.MayBilling, '-') as MayBilling , COALESCE(bill.JunBilling, '-') as JunBilling , COALESCE(bill.JulBilling, '-') as JulBilling , COALESCE(bill.AugBilling, '-') as AugBilling , COALESCE(bill.SepBilling, '-') as SepBilling , COALESCE(bill.OctBilling, '-') as OctBilling , COALESCE(bill.NovBilling, '-') as NovBilling , COALESCE(bill.DecBilling, '-') as DecBilling FROM ( SELECT br.branchname, p.plantName , b.sold_party as customerid , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-01' THEN gross_amount ELSE 0 END) / 100000 AS JanBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-02' THEN gross_amount ELSE 0 END) / 100000 AS FebBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-03' THEN gross_amount ELSE 0 END) / 100000 AS MarBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-04' THEN gross_amount ELSE 0 END) / 100000 AS AprBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-05' THEN gross_amount ELSE 0 END) / 100000 AS MayBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-06' THEN gross_amount ELSE 0 END) / 100000 AS JunBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-07' THEN gross_amount ELSE 0 END) / 100000 AS JulBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-08' THEN gross_amount ELSE 0 END) / 100000 AS AugBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-09' THEN gross_amount ELSE 0 END) / 100000 AS SepBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-10' THEN gross_amount ELSE 0 END) / 100000 AS OctBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-11' THEN gross_amount ELSE 0 END) / 100000 AS NovBilling , SUM(CASE DATE_FORMAT(billing_date, '%Y-%m') WHEN '2024-12' THEN gross_amount ELSE 0 END) / 100000 AS DecBilling FROM sbms.billing b JOIN sbms.plant p ON b.sales_office = p.plantcode JOIN sbms.branch br ON p.branchid = br.branchid WHERE b.sales_doc_type IN ('ZIEC', 'ZISC', 'ZEXS', 'ZSP', 'ZISS') AND b.product_div NOT IN ('X1', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'XA', 'XB', 'XC', 'XD', 'XE', 'XG') GROUP BY branchname, customerid UNION SELECT m.branchname, Null , m.customerid , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL FROM sbms.machinemaster m LEFT JOIN sbms.billing b ON m.customerid = b.sold_party WHERE m.machinestatus = 'A' AND b.sold_party IS NULL GROUP BY branchcode, customerid ) bill LEFT JOIN sbms.customermaster USING (customerid) ORDER BY branchname, customerid ) data GROUP BY branchname; giving +--------------+--------------+----------------+ | branchname | from_billing | from_mach_mast | +--------------+--------------+----------------+ | AHMEDABAD | 624 | 282 | | BANGALORE | 464 | 246 | | BHUVANESHWAR | 245 | 197 | | CHENNAI | 609 | 235 | | COIMBATORE | 418 | 157 | | DELHI | 398 | 216 | | Export | 0 | 66 | | GUWAHATI | 328 | 156 | | HYDERABAD | 348 | 92 | | INDORE | 335 | 212 | | JAIPUR | 290 | 106 | | KOCHI | 266 | 104 | | KOLKATA | 385 | 112 | | KOLKATTA | 0 | 7 | | LUCKNOW | 276 | 128 | | MOHALI | 511 | 266 | | MUMBAI | 420 | 291 | | NAGPUR | 235 | 126 | | PATNA | 233 | 61 | | PROD | 0 | 11 | | Projects | 0 | 23 | | PUNE | 717 | 383 | | QUALITY | 0 | 2 | | RAIPUR | 200 | 56 | | RANCHI | 176 | 76 | | SCEN | 0 | 1 | | SCHWING | 0 | 11 | | VISAKAPATNAM | 241 | 76 | | XCMG | 0 | 1 | +--------------+--------------+----------------+ 29 rows in set (6.18 sec) So where are you getting your number 658 and 71 from?
  22. Try adding some content to the div. Such as... <div class="div-with-curve"> On what planet is this curve at the top? </div>
  23. You customermaster table isn't masterful enough - several used ids are not in there mysql> SELECT COUNT(DISTINCT m.customerid) as unmatched -> FROM machinemaster m -> LEFT JOIN -> customermaster c USING (customerid) -> WHERE m.machinestatus = 'A' -> AND c.customerid IS NULL; +-----------+ | unmatched | +-----------+ | 242 | +-----------+ Try using a LEFT JOIN to the customermaster at the bottom of my query in case the missing rows are amongst those.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.