-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
get new value of type='date' field in its onchange event
Barand replied to ginerjm's topic in Javascript Help
Change this line in my earlier code from $("#target").html(dobj.toLocaleString()) to $("#target").html(dobj.toDateString().substr(0,3)) -
get new value of type='date' field in its onchange event
Barand replied to ginerjm's topic in Javascript Help
Not my experience. Te value output in the "target" div is the new data value <!DOCTYPE html> <html lang="en"> <head> <title>sample</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#date1").change(function() { let d = $(this).val() let dobj = new Date(d) $("#target").html(dobj.toLocaleString()) }) }) </script> </head> <body> <h3>Date:</h3> <input type='date' id='date1' value='2023-02-24'> <h3>Changed date:</h3> <div id='target'> </div> </body> </html> -
If you must do stupid things such as storing numbers so that they can't be used as numbers, then you need to convert it back to a number $num_week = intval(ltrim($week, 'w0'));
-
The basic principle is very simple <?php $week = $_GET['wk'] ?? 1; // set week to $_GET['wk'], or 1 if no week is provided $prev = $week - 1; $next = $week + 1; if ($prev < 1) $prev = 1; if ($next > 53) $next = 53; ?> Then display your buttons <a class='button' href='?wk=<?=$prev?>'><</a> <input class='weekno' type='text' name='wk' value='<?=$week?>'> <a class='button' href='?wk=<?=$next?>'>></a> Actually links, using css to make them look like buttons EG <style type='text/css'> .button { display: inline-block; border: 1px solid black; width: 40px; padding: 4px; text-decoration: none; text-align: center; font-family: arial, sans-serif; background-color: #808080; color: #FFFFFF; } .button:hover { background-color: #ACACAC; } .weekno { width: 50px; text-align: center; }
-
When you have more than 1 year's data, how will you know which "week 1" you need to show?
-
You need an individual $db->query() for each of the 4 queries.
-
Adding a count to a basic player with mysql and php
Barand replied to PNewCode's topic in PHP Coding Help
I took an alternative approach, using two data attributes for each audio player element... data-id : as above contains the track id data-play : initially 0, set to 1 when play is clicked, reset to 0 when track has ended. The play count can only be incremented if play is clicked when data-play is 0. This prevents play...pause...play...pause from boosting the count. The AJAX processing receives the id, increments pcount for that track, retrieves the new pcount, returns that in the response <?php include 'db_inc.php'; // use your own $pdo = pdoConnect('db2'); // db connection code ################################################################################ # Handle ajax requests # ################################################################################ if (isset($_POST['ajax'])) { if ($_POST['ajax']=='addPlay') { // increment the count for the selected player file $stmt = $pdo->prepare("UPDATE audio SET pcount = pcount + 1 WHERE id = ? "); $stmt->execute([ $_POST['id'] ]); // get the new count $res = $pdo->prepare("SELECT pcount FROM audio WHERE id = ? "); $res->execute([ $_POST['id'] ]); $pc = $res->fetchColumn(); exit("$pc"); // send the new count as the reponse } } ################################################################################ # get available audio files and build page output # ################################################################################ $res = $pdo->query("SELECT id , audio , pcount FROM audio "); $players = ''; foreach ($res as $row) { $players .= "<tr> <td>" . substr(basename($row['audio']), 0, -4) . "</td> <td> <audio class='player' data-id='{$row['id']}' data-play='0' controls controlsList='nodownload' src='{$row['audio']}' preload> Your browser does not support the audio element. </audio> </td> <td data-id='{$row['id']}'>{$row['pcount']} </tr> "; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>sample</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $(".player").on("play", function() { let id = $(this).data("id") if ($(this).attr("data-play")=='0') { // only allow increment when data-play is 0 $.post( "", {"ajax":"addPlay", "id":id}, function(resp) { if (resp > '0') { // update the page with new play count $("td[data-id="+id+"]").html(resp) } }, "TEXT" ) $(this).attr("data-play", '1') // set data-play to 1 to prevent increments } }) $(".player").on("ended", function() { $(this).attr("data-play", '0') // now track has ended, reset data-play to 0 }) }) </script> </head> <body> <table border='1'> <tr><th>File name</th> <th>Player</th> <th>Plays to<br>date</th> </tr> <?= $players ?> </table> </body> </html> -
Is it possible to pass multiple variables through one method argument?
Barand replied to chesse18's topic in PHP Coding Help
Many recommend this one: https://phpdelusions.net/pdo -
Is it possible to pass multiple variables through one method argument?
Barand replied to chesse18's topic in PHP Coding Help
Another problem with mysqli is that it was obviously written by 2 teams of developers who never spoke to one other. Executing a query produces a result object. Executing a prepared statement produces a statement object. The methods these classes to use to process the outputs are completely different. With PDO, the class methods used are the same for both, making life much easier, -
FYI - ROLLUP is certainly available in version 5.7 mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.7.36-log | +------------+ mysql> SELECT classid as class -> , COUNT(*) as students -> FROM student_class -> WHERE semesterid = 12 -> GROUP BY classid WITH ROLLUP; +-------+----------+ | class | students | +-------+----------+ | 1 | 17 | | 2 | 18 | | 3 | 20 | | 4 | 23 | | 5 | 22 | | 6 | 27 | | 7 | 24 | | 8 | 20 | | 9 | 21 | | 10 | 27 | | 11 | 25 | | 13 | 5 | | 14 | 5 | | 16 | 3 | | 17 | 5 | | 19 | 17 | | NULL | 279 | <--- ROLLUP total +-------+----------+
-
Syntax issues, still a newbie but getting there!
Barand replied to Crazycabling's topic in PHP Coding Help
HTML code wil not be output when it is inside <?php .. ?> tags. It need to be output using echo command. <?php if ($ticket['status'] == 'Fermé') { echo "<div class=\"btns\"> <a href='viewtest.php?id={$_GET['id']}&status=Réouvert' class='btn blue' style='width:400px'>Réouvrir ce billet</a> </div> "; echo 'billet fermé'; } ?> The alternative is exit php mode, output the html then re-enter php mode, which gets very messy... <?php if ($ticket['status'] == 'Fermé') { echo "<div class=\"btns\">"; // I would need this part to echo the button correctly if above status is Fermé ?> <a href="viewtest.php?id=<?=$_GET['id']?>&status=Réouvert" class="btn blue" style="width:400px">Réouvrir ce billet</a> <?php //End of part echo "</div>"; echo 'billet fermé'; } ?> -
I am still scratching my head wondering how the hell that query produced those results. You don't write php in one long line so why do it with sql? Make it readable... $query=mysqli_query($conn, "SELECT created_at , count(*) AS total , number_click , LAST_DAY(created_at) + INTERVAL 1 DAY as first_date_of_month FROM `records` WHERE DATE_FORMAT(created_at, '%Y-%m-%d') BETWEEN 'first_date_of_month' AND NOW() GROUP BY created_at ORDER BY created_at ASC ") or die(mysqli_error()); Your first_date_of_month column will actually contain the first date of next month so the WHERE clause (BETWEEN '2023-03-01' AND NOW() ) would not produce anything until NOW() is on or after the first of March. (When using BETWEEN A AND B, then A has to be <= B) You are not allowed to use a column alias in a WHERE clause, so it shouldn't even run.
-
You were quoted in that reply - it was response to one of your statements (the last time that you were sounding off about deprecated italics tags).
-
I haven't used Smarty, but does this work? <li><span class="cmp_button_wire" ><i class="fa fa.eye"></i>{$article->getViews() + 1000}</span></li>
-
Strange, because I gave you an example about 3 weeks ago
-
$numbers = '1-2-3-4'; $k = 1; foreach (explode('-', $numbers) as $n) { echo "<input type='hidden' name='Field$k' value='$n'>\n"; ++$k; } Output
-
@ginerjmWhy have you suggested putting the $newcount in the middle of a Font-Awesome icon?
-
You use the "+" operator to add two numbers together.
-
-
Adding a count to a basic player with mysql and php
Barand replied to PNewCode's topic in PHP Coding Help
I had another look at the code. You have an <audio opening tag. "<source" shouldn't be there. Your ajax needs to use the player's "onplay" event. -
Adding a count to a basic player with mysql and php
Barand replied to PNewCode's topic in PHP Coding Help
Not directly related to your problem, but your opening and closing HTML tags should match The opening tag should be <audio ... An audio element has a "play" event which you could use to trigger updates to your count via AJAX. Your html markup is antiquated. You should use a learining resource that was written this century and not in the days of Netscape Navigator. -
In your original code you were using contacts.assignedto to locate the accounts record. Why, then, are you joining contacts to accounts using contacts.mentor column?
-
Your SQL query is incorrect. Your current code... $array_data = implode(", ", $hour_prices); $sql = "INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) VALUES('" . $array_data . "');"; ... builds a query like this, with all the values in single comma-delimited string ... INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) VALUES('2023-01-22T23:00:00, DK2, 1103.27002, 0.1397'); ^ ^ This means you have 4 columns into which you are trying to write 1 value. You need to insert 4 values - one for each column. These value should be 2 string values and 2 numeric values all separated by commas. It should look like this ... INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) VALUES( '2023-01-22T23:00:00', 'DK2', 1103.27, 0.14 ) ^ ^ ^ ^ Change those 2 lines to $array_data = vsprintf(" '%s', '%s', %0.2f, %0.2f ", $hour_prices); $sql = "INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) VALUES($array_data)"; You should, though, be using a prepared statement instead of putting values into the query. If you use PDO (instead of mysqli) it is particularly easy $stmt = $pdo->prepare("INSERT INTO elpriser (HourDK, PriceArea, SpotPriceDKK, GridPrice) VALUES(?, ?, ?, ?)"); // prepare the query statement for ($hour = 0; $hour < 24; $hour++) { $hour_prices = array( $dataset_1['records'][$hour]['HourDK'] // HourDK , $dataset_1['records'][$hour]['PriceArea'] // PriceArea , $dataset_1['records'][$hour]['SpotPriceDKK'] // SpotPriceDKK , $dataset_2['records'][0]['Price' . ($hour + 1)] // GridPrice ); $stmt->execute($hour_prices); // execute it with the new values }
-
FYI - pdo->query works fine in PHPv8 (provided your query is correct, of course).
-
Subracting a number from decimal value in mysql
Barand replied to PNewCode's topic in PHP Coding Help
Of course it could - int type columns can only store whole numbers (integers). Change the column type to FLOAT or DECIMAL.