Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Your deleteList() function (as it says on the tin) deletes the whole list. You need to to delete just the list item that is doubleclicked Here's my version <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#addNew").click(function () { let txt = $("#addItem").val() // get the item text let li = $("<li>", {"text":txt, "class":"sItem"}) // create an <li> item $(li).dblclick(function() { // add dblclick event handler $(this).remove() $("#addItem").focus() }) $("#sList").append(li); // append li to ol item $("#addItem").val("").focus() // clear input field and ready for next input }) $("#addItem").focus() }) </script> [edit] PS Yours always deletes the first item in the list; mine deletes the one that is doubleclicked.
  2. Paying attention to the spelling would give it more of a chance. $email= new PHPMailer(); ^ echo "4"; $mail->setFrom('darth@empire.com', 'Darth Vader'); ^
  3. Try this for size. (Note that my item_price table uses DATE types and not DATETIME. If you are using DATETIME, use NOW() where I have used CURDATE() and use "- INTERVAL 1 SECOND" where I have used "- INTERVAL 1 DAY" so the previous price ends just before the new one comes into effect) Starting with the current item_price table item_price +---------------+-------------+-----------+------------+-------------+---------+ | item_price_id | sales_price | our_price | valid_from | valid_until | item_id | +---------------+-------------+-----------+------------+-------------+---------+ | 30 | 175.00 | 0.00 | 2021-08-08 | 2021-08-07 | 30 | | 79 | 175.00 | 0.00 | 2021-08-08 | NULL | 30 | | 31 | 100.00 | 0.00 | 2021-08-08 | NULL | 31 | | 86 | 105.00 | 0.00 | 2022-04-09 | NULL | 31 | | 32 | 65.00 | 0.00 | 2021-08-08 | NULL | 32 | | 33 | 65.00 | 0.00 | 2021-08-08 | NULL | 33 | | 34 | 75.00 | 0.00 | 2021-08-08 | NULL | 34 | | 35 | 85.00 | 0.00 | 2021-08-08 | NULL | 35 | +---------------+-------------+-----------+------------+-------------+---------+ Write you POST data to a temporary table (new_price) instead of to an array new_price +---------+-------------+-----------+ | item_id | sales_price | our_price | +---------+-------------+-----------+ | 30 | 180.00 | 0.00 | changed | 34 | 75.00 | 0.00 | no change | 40 | 90.00 | 80.00 | new | 41 | 80.00 | 70.00 | new | 42 | 110.00 | 0.00 | new | 43 | 70.00 | 60.00 | new +---------+-------------+-----------+ If the new prices are the same as those that are still current then they can be ignored, so we can remove those, creating a separate temporary table (changed_price) which will be used to update the item_price table CREATE TEMPORARY TABLE changed_price SELECT n.item_id , n.sales_price , n.our_price , CURDATE() as valid_from FROM new_price n LEFT JOIN item_price p ON n.item_id = p.item_id AND CURDATE() BETWEEN p.valid_from AND coalesce(p.valid_until, '9999=12-31') AND n.sales_price = p.sales_price AND n.our_price = p.our_price WHERE p.item_id IS NULL; changed_price +---------+-------------+-----------+------------+ | item_id | sales_price | our_price | valid_from | +---------+-------------+-----------+------------+ | 30 | 180.00 | 0.00 | 2022-04-27 | | 40 | 90.00 | 80.00 | 2022-04-27 | | 41 | 80.00 | 70.00 | 2022-04-27 | | 42 | 110.00 | 0.00 | 2022-04-27 | | 43 | 70.00 | 60.00 | 2022-04-27 | +---------+-------------+-----------+------------+ Update the item price table setting the valid until to just before our valid from for the new price UPDATE item_price p JOIN changed_price c ON p.item_id = c.item_id AND c.valid_from BETWEEN p.valid_from AND coalesce(p.valid_until, '9999-12-31') SET p.valid_until = c.valid_from - INTERVAL 1 DAY; then add the new prices to the item price table INSERT INTO item_price(item_id, sales_price, our_price, valid_from) SELECT * FROM changed_price; Our resulting ite price table is now +---------------+-------------+-----------+------------+-------------+---------+ | item_price_id | sales_price | our_price | valid_from | valid_until | item_id | +---------------+-------------+-----------+------------+-------------+---------+ | 30 | 175.00 | 0.00 | 2021-08-08 | 2021-08-07 | 30 | | 79 | 175.00 | 0.00 | 2021-08-08 | 2022-04-25 | 30 | ** updated | 120 | 180.00 | 0.00 | 2022-04-26 | NULL | 30 | ** new | 31 | 100.00 | 0.00 | 2021-08-08 | NULL | 31 | | 86 | 105.00 | 0.00 | 2022-04-09 | NULL | 31 | | 32 | 65.00 | 0.00 | 2021-08-08 | NULL | 32 | | 33 | 65.00 | 0.00 | 2021-08-08 | NULL | 33 | | 34 | 75.00 | 0.00 | 2021-08-08 | NULL | 34 | | 35 | 85.00 | 0.00 | 2021-08-08 | NULL | 35 | | 121 | 90.00 | 80.00 | 2022-04-26 | NULL | 40 | ** new | 122 | 80.00 | 70.00 | 2022-04-26 | NULL | 41 | ** new | 123 | 110.00 | 0.00 | 2022-04-26 | NULL | 42 | ** new | 124 | 70.00 | 60.00 | 2022-04-26 | NULL | 43 | ** new +---------------+-------------+-----------+------------+-------------+---------+ This should execute far faster than creating and manipulating arrays then looping through those arrays to run queries.
  4. Always put FPDF code in a script by itself. Link to that script to get the pdf file <a href='create_pdf.php'>Create PDF</a> The TCPDF library has a "WriteHTML()" function. Don't expect much by way of documentation for this library. Apart from the source code, I haven't found any yet.
  5. Here's one method $bidders = ['bobby', 'kevin', 'bill', 'kevin', 'brian', 'bobby', 'bobby' ]; $uniq = array_unique($bidders); for ($i=1; $i<=count($uniq); $i++) { $anon[] = "Bidder$i"; } $trans = array_combine($uniq, $anon); $anonBidders = array_map( function($v) use ($trans) { return $trans[$v]; }, $bidders ); to test echo '<pre>'; vprintf("Bidders | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s <br>", $bidders); vprintf("anonBidders | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s <br>", $anonBidders); giving Bidders | bobby | kevin | bill | kevin | brian | bobby | bobby anonBidders | Bidder1 | Bidder2 | Bidder3 | Bidder2 | Bidder4 | Bidder1 | Bidder1
  6. I'll attempt to describe this process as I see it. There will be some assumptions here but this is my best guess. You have a table of prices which you are querying and creating an array. Similarly, you have new prices which you are loading into a second array. You then compare these two arrays and split the contents into 2 new arrays (updates & inserts) You then loop through these new arrays updating and inserting records into the prices table. Have you considered the simple approach, namely SQL. price (before) new_price +-----+-----------------+-----------------+ +-----+-----------------+-----------------+ | id | sales_price | our_price | | id | sales_price | our_price | +-----+-----------------+-----------------+ +-----+-----------------+-----------------+ | 41 | 80.00 | 0.00 | | 40 | 90.00 | 80.00 | | 42 | 100.00 | 0.00 | | 41 | 80.00 | 70.00 | +-----+-----------------+-----------------+ | 42 | 110.00 | 0.00 | | 43 | 70.00 | 60.00 | +-----+-----------------+-----------------+ INSERT INTO price(id, sales_price, our_price) SELECT id, sales_price, our_price FROM new_price ON DUPLICATE KEY UPDATE sales_price = VALUES(sales_price), our_price = VALUES(our_price); price (after) +----+-------------+-----------+ | id | sales_price | our_price | +----+-------------+-----------+ | 40 | 90.00 | 80.00 | | 41 | 80.00 | 70.00 | | 42 | 110.00 | 0.00 | | 43 | 70.00 | 60.00 | +----+-------------+-----------+
  7. Curly braces should do the trick $d_row['FormId'] = '98765'; echo "<td><a href='delete.php?user_id={$d_row['FormId']}' onclick=\"return confirm('Are you sure?')\">Delete</a></td>"; giving <td><a href="delete.php?user_id=98765" onclick="return confirm('Are you sure?')">Delete</a></td>
  8. What if the first line of code in my previous post were $jsonstr = json_encode([$json]);
  9. My 0.02 worth... Don't mix your JS and PHP code like that. I f you need to send php data to JS (and you're not using AJAX) put the data into a hidden input field. <?php $jsonstr = json_encode($json); echo "<input type='hidden', id='jsondata' value='$jsonstr'>"; ?> <script> $(".gantt").gantt({ navigate: "buttons", scale: "days", source: $("#jsondata").val() }); </script>
  10. The one that works puts the json stuff inside [...]. The one that doesn't doesn't.
  11. Why build the json data manually??? $json_data = json_encode($result); AS for the rest of the code, without knowing your db structure and gantt api input requirements there is no way I can suggest a better route.
  12. Your first priority is to get your query right. There are four matching transaction rows with a total of 1,690 (not 1,240) which I have highlighted below. Your total bonus, therefore, should be 84.50. Your use of GROUP BY without any aggregation gives only one row for each userid. However there are 2 for for user #5 so one is ignored. SELECT t.userid , r.referalid , SUM(t.amount_deposited) as deposited , SUM(t.amount_deposited) * 0.05 as bonus FROM transaction_tbl t JOIN referal_tbl r USING (userid) GROUP BY userid; +--------+-----------+-----------+-------+ | userid | referalid | deposited | bonus | +--------+-----------+-----------+-------+ | 5 | 1 | 750 | 37.50 | | 18 | 1 | 340 | 17.00 | | 24 | 1 | 600 | 30.00 | +--------+-----------+-----------+-------+ (1690 84.50)
  13. I would create an array of holiday dates. When checking for day of week, also check if the date is in the holiday array.
  14. In each iteration of your foreach() loop you are calculating 5% of that row's bonus. You are not accumulating any totals. You need to accumulate the total bonus in the loop then, after the loop, calculate 5% of the total.. Or you could do all the calculation in the query. I'd show you how but pictures are totallt useless for recreating test data.
  15. Was "working" or was it just that the error wasn't being reported (because error reporting wasn't ON) and no one noticed the error?
  16. There was no such thing as computing or software courses when I left school for uni. Back in those days IBM were predicting that, one day, every country would own a computer.
  17. Hardly, just common sense. But, as the old saying goes, "There's nought so rare as common sense".
  18. BTW Your HTML markup "<td width='10'>" was obsolete over a decade ago. You should be using CSS. (Did you get that from the same book?)
  19. You should've changed your initial query, adding in the age calculation (simple as that, just like the examples I've shown you!) $query = "SELECT Form.FormId, Form.FirstName, Form.LastName, Form.Email, TIMESTAMPDIFF(YEAR, Form.Birthdate, CURDATE()) as age2, Form.Birthdate, Form.FavLanguage, GROUP_CONCAT(Vehicle.VehSelection SEPARATOR ', ') AS VehSelection FROM Vehicle RIGHT JOIN Form ON Form.FormId = Vehicle.FormId GROUP BY Form.FormId"; Now you have $d_row['age2']
  20. Why not? I thought you told us you were using timestampdiff(year, Birthdate, curdate()) as age2 in your query.
  21. Answer: By thinking about what you are doing instead of throwing code around hoping it'll stick. You are attampting to output {$row['age2']} but your variable in that loop is $d_row Replace <td><?php echo $diff->format("%y years"); ?></td> with <td><?php echo $d_row['age2'] ?></td>
  22. A query returns those rows for which the WHERE clause evaluates to TRUE. For example mysql> select fname -> , timestampdiff(year, dob, curdate()) as age -> from employee -> where fname = 'Peter'; +-------+------+ | fname | age | +-------+------+ | Peter | 37 | +-------+------+ 1 row in set (0.00 sec) mysql> select fname -> , timestampdiff(year, dob, curdate()) as age -> from employee -> where timestampdiff(year, dob, curdate()) > 45; +-------+------+ | fname | age | +-------+------+ | Paul | 48 | | Jane | 51 | +-------+------+ 2 rows in set (0.01 sec) If there is no WHERE clause, all rows are returned.
  23. When you do a query to select only 1 record, why would you expect output for more than one record?
×
×
  • 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.