Jump to content

Barand

Moderators
  • Posts

    24,551
  • Joined

  • Last visited

  • Days Won

    821

Community Answers

  1. Barand's post in Help with decoding json was marked as the answer   
    Try something like
    $j = '{"androidID":"6e5d819af1afb92d","cardAmount":7.149999999999999,"cashAmount":10.0, "list":[{"barcode":"1111117971111 5010018003165","cat_id":23,"cost":1.45,"id":0,"name":"1.45 CAN","payment_id":"","payment_type":"","product_id":2695,"product_type":"","qty":1,"staff_name":"","total":1.45}, {"barcode":"5054267000704 5054267000681 5449000125019 5449000124999 5038512005041 1111111141159 50271511 5449000107077 5038512000756 40822938 54492493 54493957","cat_id":23,"cost":1.85,"id":0, "name":"1.85 BOTTLE","payment_id":"","payment_type":"","product_id":2694,"product_type":"","qty":1,"staff_name":"","total":1.85}, {"barcode":"54491496 5000112628739","cat_id":23,"cost":1.95,"id":0,"name":"500ml Diet Coke","payment_id":"","payment_type":"","product_id":1758,"product_type":"","qty":1,"staff_name":"","total":1.95}, {"barcode":"1211111111111","cat_id":6,"cost":6.7,"id":0,"name":"WAFFLE 3 SCOOP","payment_id":"","payment_type":"","product_id":1254,"product_type":"","qty":1,"staff_name":"","total":6.7}, {"barcode":"1111111111160","cat_id":6,"cost":5.2,"id":0,"name":"WAFFLE 2 SCOOP","payment_id":"","payment_type":"","product_id":1252,"product_type":"","qty":1,"staff_name":"","total":5.2}], "paymentID":8646434759308,"paymentType":"Split"}'; $data = json_decode($j, 1); // put json data into an array $required = ['androidID', 'cardAmount', 'cashAmount']; foreach ($required as $k) { echo "<b>$k : </b>{$data[$k]}<br>"; } echo '<hr>'; foreach ($data['list'] as $prod) { foreach ($prod as $k => $v) { echo "<b>$k : </b>$v<br>"; } echo '<hr>'; }  
  2. Barand's post in Php and Javascript resize text area was marked as the answer   
    try
    $res = $pdo->query("SELECT user_review as rvw FROM review "); foreach ($res as $r) { echo "<textarea class='textarea-container'>{$r['rvw']}</textarea><br>"; } ?> <script type='text/javascript'> $(function() { $(".textarea-container").each(function(k,v) { let scrht = v.scrollHeight $(v).height(scrht) }) }) </script>  
  3. Barand's post in Php Mysql count and percentage display was marked as the answer   
    $totalgoal2 = number_format(min($num_rows['count'], 49)/49*100);  
  4. Barand's post in How do I get the results to show as a table was marked as the answer   
    Output the results into an HTML table
    Put each result row in a new table <tr> row. Put each column into a table <td> cell.
     
    EG
    <?php // CONNECT TO DB HERE $sql = "SELECT animal_type , animal_breed , colour , owner_name , address , telephone , mobile , email , offence , offence_date , offence_location , case_status , case_ref , action_required , action_taken , microchipped , microchip_number , aggressive , dangerous , lost , date_lost , location_lost , stolen , date_stolen , location_stolen , found, date_found , location_found , other_information FROM `animals` -- WHERE 1 (utterly useless, just leave it out) "; //------------------------------------------------------------------------------ // I find this function useful for testing // queries when I haven't got MySql Workbench open function query2HTML($db, $sql) { $output = "<table border='1'>\n"; // Query the database $result = $db->query($sql); if ($result->num_rows == 0) return "No matching records"; // get the first row and display headings $row = $result->fetch_assoc(); $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n"; // display the data do { $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } while ($row = $result->fetch_assoc()); $output .= "</table>\n"; return $output; } ?> <!DOCTYPE html> <html lang='en'> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 19.5 (Build 19523, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="04/09/2022"> <style type='text/css'> table { border-collapse: collapse; margin: 20px auto; width: 95%; } td, th { padding: 4px 8px; } th { background-color: #ccc; } </style> </head> <body> <?= query2html($conn, $sql) ?> </body> </html>  
  5. Barand's post in Reflecting Clients name on Projects everywhenre if the client name gets updated was marked as the answer   
    Example data
    TABLE: client TABLE: project +----+-----------+----------+ +----+---------------+-----------+------------+ | id | firstname | lastname | | id | project_name | client_id | start_date | +----+-----------+----------+ +----+---------------+-----------+------------+ | 1 | Scott | Chegg | | 1 | Project Alpha | 4 | 2022-12-01 | | 2 | Laura | Norder | | 2 | Proect Beta | 2 | 2023-01-15 | | 3 | Tom | DiCanari | | 3 | Project Gamma | 4 | 2023-03-01 | | 4 | S | Tonin | | 4 | Project Delta | 1 | 2023-03-20 | +----+-----------+----------+ +----+---------------+-----------+------------+ Query
    SELECT project_name , start_date , CONCAT(c.firstname, ' ', c.lastname) as client FROM project p JOIN client c ON p.client_id = c.id ORDER BY client, start_date; +---------------+------------+--------------+ | project_name | start_date | client | +---------------+------------+--------------+ | Proect Beta | 2023-01-15 | Laura Norder | | Project Alpha | 2022-12-01 | S Tonin | | Project Gamma | 2023-03-01 | S Tonin | | Project Delta | 2023-03-20 | Scott Chegg | +---------------+------------+--------------+ Now change the first name of client 4 and re-query
    UPDATE client SET firstname = 'Sarah' WHERE id = 4; SELECT project_name , start_date , CONCAT(c.firstname, ' ', c.lastname) as client FROM project p JOIN client c ON p.client_id = c.id ORDER BY client, start_date; +---------------+------------+--------------+ | project_name | start_date | client | +---------------+------------+--------------+ | Proect Beta | 2023-01-15 | Laura Norder | | Project Alpha | 2022-12-01 | Sarah Tonin | | Project Gamma | 2023-03-01 | Sarah Tonin | | Project Delta | 2023-03-20 | Scott Chegg | +---------------+------------+--------------+  
  6. Barand's post in not sure what to do with action equals index.php was marked as the answer   
    It looks like it expects that code to be in "index.php". The posted data will be sent to that file (itself)
    If you remove action="index.php" you can call your php file anything and, without the action, it will call itself.
  7. Barand's post in How much updating makes sense? was marked as the answer   
    MySql does not make unnecessary changes
    mysql> select * from user; +----+-----------+----------+-----------+----------+ | id | firstname | lastname | user_name | password | +----+-----------+----------+-----------+----------+ | 1 | Sarah | Tonin | saraht | NULL | | 2 | Tom | DiCanari | tomd | NULL | | 3 | Laura | Norder | lauran | NULL | | 4 | Anna | Robik | annar | NULL | | 5 | Peter | Dowt | peted | NULL | +----+-----------+----------+-----------+----------+ 5 rows in set (0.04 sec) mysql> update user set user_name = 'tomd' where id=2; Query OK, 0 rows affected (0.71 sec) <<<<<<< Rows matched: 1 Changed: 0 Warnings: 0 <<<<<<<  
  8. Barand's post in MYSQLI Object orientated & procedural tutorials was marked as the answer   
    Your query failed, so $result contains false and not a result object.
    Put this code before your call to nysql_connect...
    mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); ... it won't stop it failing but it will tell you why.
  9. Barand's post in Hi, have a qustion please was marked as the answer   
    I didn't notice before (because you didn't use a code block) but you have used backticks around `%search_data_value%`. Those will force SQL to treat it as a column name. Use single quotes around strings.
  10. Barand's post in Help with Syntax was marked as the answer   
    Ticks are only used in SQL queries around identifiers.
    When using quotes (single or double) they should be in matching opening and closing pairs.
    do_check('<a href="\emwin\adm\cwfmob.txt' title="Coastal Water Forecast" target="_self">CWFMOB</a>','\emwin\adm\cwfmob.txt',60*60*60,'\emwin ...'); ^..............................^ ^......................^ ^.....^ ^.^ ^..........^ As you can see, you href does not have matching opening and closing quotes, throwinng other pairs out of synch.
    try
    do_check('<a href="\emwin\adm\cwfmob.txt" title="Coastal Water Forecast" target="_self">CWFMOB</a>','\emwin\adm\cwfmob.txt',60*60*60,'\emwin ...'); ^.....................^ ^......................^ ^.....^ ^........................................................................................^ ^.....................^ ^..........^  
    See https://www.php.net/manual/en/language.types.string.php
  11. Barand's post in Hello guys, I have a message error on my PHP code : was marked as the answer   
    In the above, "=" should be "==".
    If you use "=" you assign "" to the variable clearing any value it had.
  12. Barand's post in Query Multiple Tables using 1 common field was marked as the answer   
    The names do not have to be the same, although it does make relationships clearer if they are. What matters is the value in the columns as that is what is used to match a record in a table to one or more related records records in another table.
    Before using a database, the tables in it should be normalized. Looking at your tables, that term is something new to you and your whole design is bad need of repair. The correct design of your data makes life a lot easier further down the road.
    There is a link to an SQL tutorial in my signature that may help you.
  13. Barand's post in How to select option if value is 'exactly' the string? was marked as the answer   
    let $option = $("option[value='America']") $option.attr('disabled',true)  
  14. Barand's post in mySQL Structure for Encrypted Data was marked as the answer   
    VARCHAR columns aren't limited to 255 characters
    From manual (https://dev.mysql.com/doc/refman/5.7/en/char.html) ...
     
  15. Barand's post in SQl creation problem was marked as the answer   
    Try
    SELECT rep_id , rep_name , sales_id , sales_ticketnr FROM reps r JOIN sales s ON r.rep_id = sales_rep_id WHERE rep_touroperatorid = 5  
  16. Barand's post in Trying to add a where clause to my pdo select was marked as the answer   
    Sorry, I forgot to take the "->fetchAll()" from the end. Let's start again
    $stmt = $db->prepare("SELECT * FROM posts WHERE username = ? LIMIT $paginationStart, $limit"); $stmt->execute([$row2['username']]); $authors = $stmt->fetchAll();  
  17. Barand's post in Why I Get This Unnecssary FATAL Error ? was marked as the answer   
    Since you found that this...
    $query->bind_param($CharTypes, ...$SearchValues); works, why are you using
    without the "..." to expand the array into a comma separated list?
  18. Barand's post in how to Run a line of code after 10 minutes of running another line of code in PHP was marked as the answer   
    Here's one way.
    When you send a payment confirmation write a record (forename, surname, email) to "confirmation" table
    CREATE TABLE `confirmation` ( `confirm_id` int(11) NOT NULL AUTO_INCREMENT, `forename` varchar(45) DEFAULT NULL, `surname` varchar(45) DEFAULT NULL, `email` varchar(45) DEFAULT NULL, `time_confirmed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `tips_sent` datetime DEFAULT NULL, PRIMARY KEY (`confirm_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Set up a cron job to run every 5 minutes. This would
    SELECT confirm_id, forename, surname, email FROM confirmation WHERE tips_sent IS NULL AND NOW() - INTERVAL 10 MINUTE > time_confirmed; foreach record returned
    Send welcome tips email UPDATE confirmation SET tips_sent = NOW() WHERE confirm_id = ?
  19. Barand's post in simple string operation not working.. was marked as the answer   
    Looking up strpos() in the manual would have explained exactly what your problem is, and the first example gives you the cure.

  20. Barand's post in there seems to be no difference between CHAR and VARCHAR was marked as the answer   
    Use varchar for columns which can have values of variable lengths (names, address lines, descriptions, comments etc)
    Use char when you have values of fixed length (EG US state abbreviations could be CHAR(2), or if you have a product table that has a 4-character product code then CHAR(4)).
    If in doubt use varchar, but don't go mad and make everything VARCHAR(255) as some do. Keep them as sensible size for the expected content. You don't want people entering a phone number with 255 digits just because your DB allows it.
  21. Barand's post in PHP and wildlife was marked as the answer   
    or
    $ark = array( '3 gerbils', '7 fish', '3 gerbils', '1 cat' ); $received = array( '3 gerbils', '7 fish' ); foreach($ark as $j => $expected) { echo $expected; foreach($received as $k => $line) { if($expected == $line) { echo " ARRIVED"; unset ($received[$k], $ark[$j]); // cross them off the lists } } echo "<hr>"; } giving...

     
  22. Barand's post in numbers and math driven websites was marked as the answer   
    What about the Dewey Decimal System used by libraries?
  23. Barand's post in CRUD read. An overview of all the members was marked as the answer   
    If you are happy to display the multiple numbers and emails as, say, comma-separated lists, then you can use GROUP_CONCAT on those columns and GROUP BY Lidnummer.
    SELECT lid.Lidnummer , lid.Naam , lid.Voornaam , lid.Huisnummer , lid.Postcode , postcode.Adres , postcode.Woonplaats , GROUP_CONCAT(telefoonnummers.Telefoonnummer SEPARATOR ', ') as Telefoonnummer , GROUP_CONCAT(email.Emailadres SEPARATOR ', ') as Emailadres FROM lid INNER JOIN postcode ON lid.Postcode = postcode.Postcode INNER JOIN telefoonnummers lid.Lidnummer = telefoonnummers.Lidnummer INNER JOIN email ON lid.Lidnummer = email.Lidnummer GROUP BY lid.Lidnummer; Use explicit joins and not "FROM A, B, C WHERE..."
    When posting code in the forum, use the <> button to create a code block and specify the code type.
  24. Barand's post in Syntax issues, still a newbie but getting there! was marked as the answer   
    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é'; } ?>  
  25. Barand's post in I can't push my record to mysql database was marked as the answer   
    Remove the single quotes from around the tablename and column name identifiers. Quotes are for string literals.
    $sql="INSERT INTO 'users' ('name','email','phone','bgroup') VALUES ('$name','$email','$phone','$bgroup')"; ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ REMOVE Also, you should use a prepared statement instead of putting user-provided variables into your query.
×
×
  • 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.