-
Posts
24,605 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
date not displaying on populated edit form
Barand replied to webdeveloper123's topic in PHP Coding Help
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 -
Is there a better alternative way to reduce the below code?
Barand replied to beginnerForPHP's topic in PHP Coding Help
<?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 -
PHP calc price with different time range cost
Barand replied to kit123321's topic in PHP Coding Help
What have you tried to get the total? -
date not displaying on populated edit form
Barand replied to webdeveloper123's topic in PHP Coding Help
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) -
date not displaying on populated edit form
Barand replied to webdeveloper123's topic in PHP Coding Help
I commented on your d-m-y date formats in your last topic -
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.
-
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>
-
just want some little help more from you seniors
Barand replied to Ehsan_collboy's topic in PHP Coding Help
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. -
just want some little help more from you seniors
Barand replied to Ehsan_collboy's topic in PHP Coding Help
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. -
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>
-
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.
-
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 | +---------------------+------+----------------+
-
What is your table's definition? Does each record have a unique id?
-
How to pre fill an array with missings keys?
Barand replied to beginnerForPHP's topic in PHP Coding Help
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) -
I'm guessing the implicit join is screwing with the aliases when it tries to join to the subquery
-
Let it be a lesson always to use explicit join syntax and not FROM A, B
-
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 | +-------------+------+-----------+-----------+-----------+------------+
-
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
-
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);
-
How to pre fill an array with missings keys?
Barand replied to beginnerForPHP's topic in PHP Coding Help
$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 ) ) -
How wide are the spacings beween the labels?