Jump to content

Barand

Moderators
  • Posts

    24,605
  • Joined

  • Last visited

  • Days Won

    831

Everything posted by Barand

  1. The purpose of that is to change a date string (eg '19-04-2022') which is in %d-%m-%Y format to a correct DATE format. RTFM - https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
  2. <?php $test['0000'] = ['address' =>'tes0']; $test['1111'] = ['id'=>'tes1', 'name'=>'tes11', 'address' =>'tes111']; $test['2222'] = ['id'=>'tes2', 'name'=>'tes22', 'address' =>'tes222']; $test['3333'] = ['id'=>'tes3', 'name'=>'tes33']; $nil = ['id'=>'NIL', 'name'=>'NIL', 'address' =>'NIL']; $test = array_map( function($v) use ($nil) { return array_replace($nil, $v); } , $test); PS Definite feeling of deja vu here
  3. What have you tried to get the total?
  4. You can change the format of your d-m-Y dates to the correct Y-m-d format with this query UPDATE date_test SET birthday = str_to_date(birthday, '%d-%m-%Y') WHERE locate('-', birthday) = 3; For example mysql> CREATE TABLE `date_test` ( -> `date_test_id` int(11) NOT NULL AUTO_INCREMENT, -> `the_date` varchar(15) DEFAULT NULL, -> PRIMARY KEY (`date_test_id`) -> ) ; Query OK, 0 rows affected (0.28 sec) mysql> INSERT INTO `josen2`.`date_test` (`the_date`) VALUES ('2022-01-01'), ('01-01-2022'); Query OK, 2 rows affected (0.14 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM josen2.date_test; +--------------+------------+ | date_test_id | the_date | +--------------+------------+ | 1 | 2022-01-01 | | 2 | 01-01-2022 | +--------------+------------+ 2 rows in set (0.00 sec) mysql> UPDATE date_test -> SET the_date = str_to_date(the_date, '%d-%m-%Y') -> WHERE locate('-', the_date) = 3; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM josen2.date_test; +--------------+------------+ | date_test_id | the_date | +--------------+------------+ | 1 | 2022-01-01 | | 2 | 2022-01-01 | +--------------+------------+ 2 rows in set (0.00 sec) Once that has been done and all dates in the correct format you can change the column from varchar to date. (The ALTER query will fail if any dates have an incorrect format) mysql> ALTER TABLE `josen2`.`date_test` -> CHANGE COLUMN `the_date` `the_date` DATE NULL DEFAULT NULL; Query OK, 2 rows affected (0.87 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM josen2.date_test; +--------------+------------+ | date_test_id | the_date | +--------------+------------+ | 1 | 2022-01-01 | | 2 | 2022-01-01 | +--------------+------------+ 2 rows in set (0.01 sec)
  5. I commented on your d-m-y date formats in your last topic
  6. From the manual (https://www.php.net/manual/en/pdostatement.rowcount.php) You could use this to see if a row was returned... $stmt->execute(); return $stmt->fetch(); However a better way would be to put a UNIQUE constraint on the username column. You then just insert the new username but check for a "duplicate key" error. This saves you from having check every insert and you only need to take action if the insert failed.
  7. The PHP DateTime::diff() method provides a very convenient way of getting the days, hours, minutes and seconds components of a time difference so this script uses an AJAX request on loading to get the time remaining. From then on, it calls a javascript function every second to reduce the time displayed by one second. This greatly reduces network traffic and gives a consistent update performance. Repeatedly using AJAX could sometimes result in delays preventing a regular countdown interval. <?php ################################################################################################################## # # # THIS SECTION HANDLES THE AJAX REQUEST AND EXITS TO SEND RESPONSE (Days,hrs, mins, secs remaining) # # # if (isset($_GET['ajax'])) { if ($_GET['ajax'] == 'countdown') { $remain = ['days' => 0, 'hrs' => 0, 'mins' => 0, 'secs' => 0]; $dt1 = new DateTime( $_GET['target'] ); $dt2 = new DateTime('now'); if ($dt1 > $dt2) { $diff = $dt1->diff($dt2); $remain['days'] = $diff->days; $remain['hrs'] = $diff->h; $remain['mins'] = $diff->i; $remain['secs'] = $diff->s; } exit(json_encode($remain)); } } # # ################################################################################################################### $target = '2022-04-30 23:59:59'; // SET OR GET TARGET TIME HERE $targ = new DateTime($target); $target_time = $targ->format('g:ia'); $target_date = $targ->format('F jS Y'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Countdown</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> var inter $().ready( function() { get_time_remaining() // call AJAX request to get remaining time inter = setInterval(countdown, 1000) // set timer to call "countdown()" function every second }) function countdown() { let s = parseInt($("#secs").html()) // get current time remaining let m = parseInt($("#mins").html()) let h = parseInt($("#hrs").html()) let d = parseInt($("#days").html()) if (d==0 && h==0 && m==0 && s==0) { // exit when target time is reached clearInterval(inter) $(".remain").css("background-color", "red") return } s--; // reduce display by 1 second if (s < 0) { s = 59; m-- } if (m < 0) { m = 59 h-- } if (h < 0) { h = 23 d-- } if (d < 0) { d = 0 } $("#days").html(d) // redisplay new values $("#hrs").html(h) $("#mins").html(m) $("#secs").html(s) } function get_time_remaining() { $.get( // make AJAX request "", {"ajax":"countdown", "target":$("#target").val()}, function(resp) { // put response values in display fields $("#days").html( resp.days ) $("#hrs").html( resp.hrs ) $("#mins").html( resp.mins ) $("#secs").html( resp.secs ) }, "JSON" ) } </script> <style type='text/css'> body { font-family: verdana, sans-serif; font-size: 11pt; } header { padding: 8px; text-align: center; width: 600px; margin: 20px auto; background-color: #F0F0F0; } .target { color: #006EFC; font-size: 16pt; } table { border-collapse: collapse; width: 400px; margin: 0 auto; } td, th { padding: 8px; text-align: center; width: 25%; } .remain { font-size: 24pt; color: white; background-color: black; border: 1px solid white; } </style> </head> <body> <header> <p>Countdown to</p> <p class='target'><?=$target_time?> on <?=$target_date?> </p> <!-- make target time available to javascript --> <input type='hidden' id='target' value='<?=$target?>' > <table border='0'> <tr><th>Days</th><th>Hours</th><th>Mins</th><th>Secs</th></tr> <tr> <td class='remain' id='days'>0</td> <td class='remain' id='hrs'>0</td> <td class='remain' id='mins'>0</td> <td class='remain' id='secs'>0</td> </tr> </table> </header> </body> </html>
  8. It's one of your next steps. The first thing you need to do is understand why that code I last posted works and yours didn't.
  9. I'd define it something like this CREATE TABLE `comments` ( `post_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `email` varchar(50) DEFAULT NULL, `comments` text, `approvedby` varchar(50) DEFAULT NULL, `status` tinyint(4) DEFAULT NULL, PRIMARY KEY (`post_id`) ) then, as stated earlier, datetime and post_id are generated automatically and can be excluded from the INSERT. As @ginerjm said, don't use varchar for dates. They should be DATE, DATETIME or TIMESTAMP and the format should be yyyy-mm-dd. Store them for functionality, not prettiness. Format them as required on output or in the query.
  10. Looks to me like you have 5 placeholders and 4 bound values for them, hence the error message (They usually tell you what the problem is - you just have to read them) What does your table structure look like? If datetime is the current timestamp and post_id is an auto_incremented primary key then they don't need to be in the insert statement.
  11. Let's shoot this horse and put it out of its misery. There's still work to do but it illustrates another solution to your main problem. $vehicles = [ 'Plane', 'SuperCar', 'Yacht' ]; $res = $db->query("SELECT f.formid , f.firstname , f.lastname , v.vehselection FROM form f LEFT JOIN vehicle v USING (formid) ORDER BY lastname "); $data = []; foreach ($res as $r) { // process results // for each person create an array element // that contains an array of the vehicles owned // by that person if (!isset($data[$r['formid']])) { $data[$r['formid']] = [ 'name' => $r['firstname'] . ' ' . $r['lastname'], 'vehTypes' => [] ]; } $data[$r['formid']]['vehTypes'][] = $r['vehselection']; } // the data array looks like this // // $data = Array ( // // [193] => Array // ( // [name] => John Atkins // [vehTypes] => Array // ( // [0] => SuperCar // [1] => Plane // ) // // ) // // [192] => Array // ( // [name] => Frank Lampard // [vehTypes] => Array // ( // [0] => Yacht // [1] => Plane // ) // // ) // // ... // ) ?> <style type='text/css'> table { border-collapse: collapse; width: 600px; margin: 20px auto; } td, th { padding: 8px; } </style> <table border='1'> <tr> <th>Name</th> <th>Vehicles</th> </tr> <?php foreach ($data as $fid => $person) { echo "<tr><td>{$person['name']}</td><td>"; // now loop through the $vehicles array (top of script) // outputting a checkbox for each whic is checked // if the person owns one of that type foreach ($vehicles as $vtype) { $chk = in_array($vtype, $person['vehTypes']) ? 'checked' : ''; echo "<label> <input type='checkbox' name='vehicle[$fid][]' value='$vtype' $chk> $vtype </label> <br>"; } echo "</td></tr>\n"; } ?> </table>
  12. Is that because they want or need to, or because there is nothing to stop them them doing it accidentally? You could add a UNIQUE constrint on (code, datetime) columns to prevent accidental duplicates. BTW, as you will be testing deletions (not undoable) I recommend you back up before testing on any live data.
  13. As all records in a relational DB should have a primary key I'll assume yours do. BEFORE +---------------------+------+----------------+ | 1 | hide | 20211123073000 | | 2 | hide | 20211123073000 | | 3 | hide | 20211123074500 | | 4 | hide | 20211123074500 | | 5 | | 20211123080000 | | 6 | hide | 20211123080000 | | 7 | | 20211123081500 | | 8 | hide | 20211123081500 | | 9 | | 20211123083000 | | 10 | hide | 20211123083000 | +---------------------+------+----------------+ QUERY DELETE teacher_schedule FROM teacher_schedule JOIN ( SELECT code , sched_time , MAX(teacher_schedule_id) as teacher_schedule_id FROM teacher_schedule WHERE code = 'hide' GROUP BY code, sched_time HAVING count(*) > 1 ) dupes USING (teacher_schedule_id); AFTER +---------------------+------+----------------+ | teacher_schedule_id | code | sched_time | +---------------------+------+----------------+ | 1 | hide | 20211123073000 | | 3 | hide | 20211123074500 | | 5 | | 20211123080000 | | 6 | hide | 20211123080000 | | 7 | | 20211123081500 | | 8 | hide | 20211123081500 | | 9 | | 20211123083000 | | 10 | hide | 20211123083000 | +---------------------+------+----------------+
  14. What is your table's definition? Does each record have a unique id?
  15. Use a DatePeriod() object to generate the NIL array. I'd show the code but I am having difficulty processing that image of your data in my test code. (Hint: in future, use var_export() with arrays)
  16. I'm guessing the implicit join is screwing with the aliases when it tries to join to the subquery
  17. Let it be a lesson always to use explicit join syntax and not FROM A, B
  18. Use an explicit join ( trk_races JOIN drivers) instead of trk_races,drivers SELECT a.race_winner, w.wins, SUBSTRING_INDEX(a.race_winner, ' ', -1) AS last_name, a.race_name, DATE_FORMAT(a.race_date, '%m/%d') AS race_date, d.driver_num FROM trk_races a JOIN drivers d ON a.season = d.driver_season AND a.race_winner = d.driver_name LEFT OUTER JOIN ( SELECT race_winner, COUNT(race_date) AS wins FROM trk_races WHERE race_winner > '' AND Season=2021 GROUP BY race_winner ) w ON w.race_winner = a.race_winner WHERE a.race_winner > '' AND a.Season = 2021 +-------------+------+-----------+-----------+-----------+------------+ | race_winner | wins | last_name | race_name | race_date | driver_num | +-------------+------+-----------+-----------+-----------+------------+ | G Harrison | 2 | Harrison | Race 1 | 01/01 | 38 | | G Harrison | 2 | Harrison | Race 2 | 01/08 | 38 | | J Lennon | 1 | Lennon | Race 3 | 01/15 | 37 | | R Starr | 1 | Starr | Race 4 | 01/22 | 39 | | P McCartney | 1 | McCartney | Race 5 | 01/29 | 36 | +-------------+------+-----------+-----------+-----------+------------+
  19. Easiest way is to use a link <a>..</a> which takes you to the required page, passing the id of the article in the query string. Style the link to look like a button. For example <head> <title>Example</title> <style type='text/css'> body { font-family: verdana, sans-serif; font-size: 11pt; } .link-button { display: inline-block; width: 50px; padding: 5px; margin-left: 16px; background-color: blue; color: white; border: 1px solid black; text-align: center; text-decoration: none; border-radius: 8px; } .link-button:hover { background-color: gray; } </style> </head> <body> <p> Storm news <br> <a href='read_article.php?id=1' class='link-button'>Read</a> </p> <p> Flood news <br> <a href='read_article.php?id=2' class='link-button'>Read</a> </p> </body> giving
  20. Use this
  21. try $data = []; $fp = fopen('test.csv', 'r'); while ($line = fgetcsv($fp, 1024, ';')) { $data[ $line[0]][] = $line[2]; } fclose($fp); $fp = fopen('test.gz', 'w'); foreach ($data as $date => $prices) { fwrite($fp, $date . ';' . join(';', $prices) . "\n"); } fclose($fp);
  22. $test = [ '0000' => [2 =>'tes000'], '1111' => [0=>'tes1', 1=>'tes11', 2 =>'tes111'], '2222' => [0=>'tes2', 2 =>'tes333'] ] ; $test = array_map( function($v) { return array_replace(array_fill_keys(range(0,2), 'NIL'), $v); } , $test); echo '<pre>' . print_r($test, 1) . '</pre>'; giving Array ( [0000] => Array ( [0] => NIL [1] => NIL [2] => tes000 ) [1111] => Array ( [0] => tes1 [1] => tes11 [2] => tes111 ) [2222] => Array ( [0] => tes2 [1] => NIL [2] => tes333 ) )
  23. According to your post, your table doesn't have a "Firstname" column (it's "Name"). Also make sure the character cases in your indexes match what is returned in the results. (EG "email" or "Email"?) (Personally, I follow the convention of having all table and column names lower case.)
  24. I know, hence my "hot air balloon" example. If no-one has one, it won't show.
  25. How wide are the spacings beween the labels?
×
×
  • 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.