-
Posts
24,608 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
The manual says the fields should be type date or datetime, which I is why I tested it with a time field before posting. Maybe your version doesn't support time types (Mine is MySQL 5.7) Alternative... SELECT name , NOW() as Now , action_time , round(time_to_sec(timediff(action_time, time(now())))/60, 0) as mins FROM a_test_table; +--------+---------------------+-------------+------+ | name | Now | action_time | mins | +--------+---------------------+-------------+------+ | John | 2020-12-08 09:51:28 | 15:30:00 | 339 | | Paul | 2020-12-08 09:51:28 | 18:00:00 | 489 | | George | 2020-12-08 09:51:28 | 12:00:00 | 129 | | Ringo | 2020-12-08 09:51:28 | 10:00:00 | 9 | +--------+---------------------+-------------+------+
-
It looks like you need to include config.php in register.php and not in register.php.
-
Strange, ENT_IGNORE was added in v5.3. However its use is discouraged as a potential security risk. Try replacing ENT_IGNORE with NULL EDIT: As of 5.4, UTF-8 is the default so just go with htmlspecialchars($tagContent)
-
$metaTagsHTML .= '<meta property="' . $propertyName . '" content="' . htmlspecialchars($tagContent,'ENT_IGNORE','UTF-8') . '" />' . "\n"; | | |__REMOVE__| ENT_IGNORE is a constant definition, not a string literal - remove the quotes.
-
Simple php calendar plugin trying to add linkable dates
Barand replied to OldGrim's topic in PHP Coding Help
I've rewritten the code for you, cleaned up the structure cleaned up the HTML using classes and CSS and removed deprecated markup implemented mac_gyver's array suggestion implemented the links <?php const HOST = 'localhost'; ##### const USERNAME = '????'; # const PASSWORD = '????'; # const DATABASE = '????'; # These lines would # function pdoConnect($dbname=DATABASE) # normally be in { # $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); # an included file $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); # $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); # return $db; # } ##### /********************************* TEST DATA ***************************************** +---------+------------+--------+------------+ | blog_id | date | title | content | +---------+------------+--------+------------+ | 1 | 2020-11-15 | Blog 1 | Blog one | | 2 | 2020-12-07 | Blog 2 | Blog two | | 3 | 2020-12-08 | Blog 3 | Blog three | | 4 | 2020-12-10 | Blog 4 | Blog four | | 5 | 2020-12-21 | Blog 5 | Blog five | | 6 | 2021-01-02 | Blog 6 | Blog six | +---------+------------+--------+------------+ *************************************************************************************************/ $pdo = pdoConnect(); $today = new DateTime( $_GET['date'] ?? '' ); $prev = (clone $today)->modify('-1 month')->format('Y-m-d'); $next = (clone $today)->modify('+1 month')->format('Y-m-d'); $caption = $today->format('F Y'); $this_month = $today->format('n'); $day1 = (clone $today)->modify("first day of this month"); $queryStart = $day1->format('Y-m-d'); if ($day1->format('w') != 0) { $day1->modify('last Sunday'); } $dayN = $today->modify('last day of this month'); $queryEnd = $dayN->format('Y-m-d'); if ($dayN->format('w') != 6) { $dayN->modify('next sunday'); } $period = new DatePeriod($day1, new DateInterval('P1D'), $dayN); // calendar display dates ## ## get this month's blogs ## $blogs = []; $res = $pdo->prepare("SELECT blog_id , date as day , title FROM blog WHERE date BETWEEN ? AND ? ORDER BY date "); $res->execute( [ $queryStart, $queryEnd ] ); foreach ($res as $r) { $blogs[$r['day']] = $r; } ## ## Functions ## function calheads() { $out = '<tr>'; $days = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ]; foreach ($days as $d) { $out .= "<th abbr='$d' title='$d'>{$d[0]}</th>"; } return $out; } function caldata($per, $mth, &$blogs) { $i = 0; $out = ''; foreach ($per as $d) { if ($i%7 == 0) { if ($out != '') $out .= "</tr>\n"; $out .= "<tr>"; } ++$i; $dno = $d->format('d'); $cls = $link = $title = ''; if ($d->format('n') != $mth) { $cls .= 'blank '; } else { $cls .= 'daycell '; if (isset($blogs[$d->format('Y-m-d')])) { $cls .= 'active '; $title = $blogs[$d->format('Y-m-d')]['title']; $dno = "<a href='https://whisperwillow.net/infusions/grims_blog/filtered.php?post_id={$blogs[$d->format('Y-m-d')]['blog_id']}'>$dno</a>"; } if ($d->format('Y-m-d') == date('Y-m-d')) $cls .= 'today '; } $out .= "<td class='$cls' title='$title'>$dno</td>"; } $out .= "</tr>\n"; return $out; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Examle calendar</title> <meta name="creation-date" content="12/06/2020"> <style type='text/css'> body, table { font-family: verdana, sans-serif; font-size: 10pt; } table { border-collapse: collapse; width: 63%; margin: 50px auto; } caption { background-color: #A91723; color: #FFF; padding: 4px; text-align: center; font-size: 16pt; } th { background-color: #5F8874; color: #FFF; padding: 4px; } td { background-color: #EEE; padding: 8px; text-align: center; font-weight: 600; } td.active { background-color: #CCC; color: #FFF; } td.blank { background-color: #FFF; color: #CCC; } td.today { border-color: #A91723; border-width: 5px; color: #666; } #navbtn1, #navbtn2 { font-weight: 600; color: #FFF; text-decoration: none; } #navbtn1 { float: left; } #navbtn2 { float: right; } </style> </head> <body> <input type='hidden' id='caldata' value=''> <table border='1'> <caption> <a id='navbtn1' href='?date=<?=$prev?>'><</a> <?=$caption?> <a id='navbtn2' href='?date=<?=$next?>'>></a> </caption> <thead> <?=calheads()?> </thead> <tbody> <?=caldata($period, $this_month, $blogs)?> </tbody> </table> </body> </html> output: -
Try SELECT A FROM TableA LEFT OUTER JOIN TableB ON Table A. Column A = Table B. Column B AND Table A. Column A = Fixed value WHERE Column B IS NULL -- or -- SELECT A FROM TableA LEFT OUTER JOIN TableB ON Table A. Column A = Table B. Column B WHERE Table A. Column A = Fixed value AND Column B IS NULL Record selection conditions normally go in the WHERE clause. However, they can be in the JOIN's ON clause. If the condition were on a column in TableB (which is LEFT JOINed) then the condition would have to be in the ON clause.
-
Simple php calendar plugin trying to add linkable dates
Barand replied to OldGrim's topic in PHP Coding Help
Can you define "doesn't work" for us. What should it do that it isn't doing? What is it doing that it shouldn't do? -
If you use MySql's timestampdiff() function you can go directly to minutes. DATA: +----+--------+-------------+ +-------------+-------------+------+-----+---------+----------------+ | id | name | action_time | | Field | Type | Null | Key | Default | Extra | +----+--------+-------------+ +-------------+-------------+------+-----+---------+----------------+ | 1 | John | 15:30:00 | | id | int(11) | NO | PRI | NULL | auto_increment | | 2 | Paul | 18:00:00 | | name | varchar(45) | YES | | NULL | | | 3 | George | 12:00:00 | | action_time | time | YES | | NULL | | | 4 | Ringo | 10:00:00 | +-------------+-------------+------+-----+---------+----------------+ +----+--------+-------------+ SELECT name , NOW() as Now , action_time , timestampdiff(MINUTE, now(), action_time) as mins FROM a_test_table; RESULTS: +--------+---------------------+-------------+------+ | name | Now | action_time | mins | +--------+---------------------+-------------+------+ | John | 2020-12-06 11:10:15 | 15:30:00 | 259 | | Paul | 2020-12-06 11:10:15 | 18:00:00 | 409 | | George | 2020-12-06 11:10:15 | 12:00:00 | 49 | | Ringo | 2020-12-06 11:10:15 | 10:00:00 | -70 | +--------+---------------------+-------------+------+
-
Looking to populate text boxes based upon dropdown menu selection
Barand replied to itmagician's topic in Javascript Help
Check for change events on the dropdown. Take appropriate action depending on whether its value changes to A or B.- 1 reply
-
- 1
-
-
this worked $team = 65; $grade = 2020; $level = 1; /**** CSV CONTENT (export from Excel) uniform,nameFirst,nameLast,position,height 10,Laura,Norder,PG,"6'4""" 20,Tom,DiCanari,SF,"4'11""" ********************************************/ $sql = <<<SQL LOAD DATA LOCAL INFILE 'c:/inetpub/wwwroot/test/roster2.csv' INTO TABLE roster FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 ROWS (uniform, nameFirst, nameLast,position,@height) SET feet = substring_index(@height, '\'', 1) , inches = substring_index(substring_index(@height, '"', 1), '\'', -1) , team = $team , grade = $grade , level = $level SQL; $stmt = $my->query($sql); /**** TABLE: roster CREATE TABLE `roster` ( `roster_id` int(11) NOT NULL AUTO_INCREMENT, `team` int(11) DEFAULT NULL, `grade` int(11) DEFAULT NULL, `uniform` varchar(45) DEFAULT NULL, `nameFirst` varchar(45) DEFAULT NULL, `nameLast` varchar(45) DEFAULT NULL, `position` varchar(15) DEFAULT NULL, `feet` tinyint(4) DEFAULT NULL, `inches` tinyint(4) DEFAULT NULL, `level` int(11) DEFAULT NULL, PRIMARY KEY (`roster_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +-----------+------+-------+---------+-----------+----------+----------+------+--------+-------+ | roster_id | team | grade | uniform | nameFirst | nameLast | position | feet | inches | level | +-----------+------+-------+---------+-----------+----------+----------+------+--------+-------+ | 1 | 65 | 2020 | 10 | Laura | Norder | PG | 6 | 4 | 1 | | 2 | 65 | 2020 | 20 | Tom | DiCanari | SF | 4 | 11 | 1 | +-----------+------+-------+---------+-----------+----------+----------+------+--------+-------+ */
-
Ok, keep it a secret. Good luck.
-
Your queries seem to be sprouting columns. What do the csv and your a_rosters table actually look like?
-
then why have you defined the csv columns as (uniform,nameFirst,nameLast,position,@feet,@inches) ? With the mix of quotes in the SQL you would better using heredoc syntax when defining the query.
-
That's why I laid it out the way I did with the comments - so it would be easy for you get the separate feet/inches values if you still wanted to go that way. [edit] Look more closely at my code - you require two substring_index()s to extract the inches. The inner to get the string before the final " and the outer one to get the string after the ' SET feet = substring_index(@height, '\'', 1) * 12 , inches = substring_index(substring_index(@height, '"', 1), '\'', -1)
-
That said, here's a solution INPUT (roster.csv) uniform, firstname, lastname, position, height 101,Freda,Greaves,left out,6'4" 102,Jamie,Oliver,left behind,4'11" SQL LOAD DATA LOCAL INFILE 'c:/inetpub/wwwroot/test/roster.csv' INTO TABLE roster FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS (uniform, nameFirst, nameLast,position,@height) SET height = substring_index(@height, '\'', 1) * 12 -- feet + substring_index(substring_index(@height, '"', 1), '\'', -1) -- inches TABLE (roster) +-----------+---------+-----------+----------+-------------+--------+ | roster_id | uniform | nameFirst | nameLast | position | height | +-----------+---------+-----------+----------+-------------+--------+ | 1 | 101 | Freda | Greaves | left out | 76 | | 2 | 102 | Jamie | Oliver | left behind | 59 | +-----------+---------+-----------+----------+-------------+--------+
-
Does the height of your player depend on which roster they are in? If a player is the same height no matter what roster s/he is placed in then the height columns should be in the player table, not the roster. In answer to your question, it is often useful to load csv data into an intermediate table then move it to the correct table using PHP, where you can use functions like explode(). Why have 2 columns (feet/inches) and not just convert to inches?
-
If you had error reporting tuned on, you would see a message like this If the PHP processor comes across an unquoted string it assumes it is a defined constant. It then tries to find the value that was defined for that constant. If it cannot find one it then assumes it must be a string value 'green'. All this is a waste of time making your code less efficient, so always quote string literals.
-
Convert Multiple Queries Into One Query Using Conditions
Barand replied to mongoose00318's topic in MySQL Help
As an alterntive for $price = $res->fetchColumn(); return $price ? $price : 'Not set'; I could have used return $res->fetch()['price'] ?? 'Not set'; which might be better (if you have freebies and 0.00 is a valid price). -
Convert Multiple Queries Into One Query Using Conditions
Barand replied to mongoose00318's topic in MySQL Help
Correct - the ternary expression reads "If price is true, return price otherwise return 'Not set'. In the line above $price = $res->fetchColumn(); price will be "false" if no price was found. -
You will if you try it. Experiment. Planes won't fall out of the sky if get it wrong.
-
calculate total absents and presents based on user login data
Barand replied to ajoo's topic in MySQL Help
I've given you a couple of alternative methods - the main change is to to use the previous log in instead of the log out time so it shouldn't be to difficult for you to alter the queries to your requirements. The tricky bit is your "edge condition", but give it a go. (my 503 was because I forgot to subtract the 1 to get the days inbetween when getting the last record's time difference, so I had 97) I still don't understand why, when you are calculating days between logins, you decide to add in the duration of the login for the last record (15 apples + 1 orange). If anything I would have the thought the last record's time would be from final login to the current date. Just my 0.02 worth -
Convert Multiple Queries Into One Query Using Conditions
Barand replied to mongoose00318's topic in MySQL Help
Try "Plan A" function determine_multi_price_kit( $order_id, $kit_id, $pdo ) { $res = $pdo->prepare("SELECT l.price , l.enterprise FROM production_data p JOIN leds_prices l ON l.enterprise IN (p.enterprise, 'Unbranded') AND kit_id = :kit_id WHERE p.id = :order_id ORDER BY l.enterprise = 'Unbranded' "); $res->execute( [ 'kit_id' => $kit_id, 'order_id' => $order_id ] ); $price = $res->fetchColumn(); return $price ? $price : 'Not set'; } $kit = 48; $orders = [ 5017, 7094, 7179, 7185, 1234 ] ; foreach ($orders as $oid) { echo "$oid : ". determine_multi_price_kit( $oid, $kit, $pdo ) . '<br>'; } -
Convert Multiple Queries Into One Query Using Conditions
Barand replied to mongoose00318's topic in MySQL Help
Question: Your function specifies (order_id, kit_id, link) as parameters but your sample function call in last post has ( 7185, 119, $pdo ) . According to your data, 119 is a product_id and not a kit_id. Which is it? -
Convert Multiple Queries Into One Query Using Conditions
Barand replied to mongoose00318's topic in MySQL Help
Excellent - I'll get back to you. -
Convert Multiple Queries Into One Query Using Conditions
Barand replied to mongoose00318's topic in MySQL Help
Any chance of some test data to work with?