Jump to content

Barand

Moderators
  • Posts

    24,450
  • Joined

  • Last visited

  • Days Won

    809

Community Answers

  1. Barand's post in Auto generate year session was marked as the answer   
    Try this. You don't need date arithmetic for years.
    $start = 2020; $end = 2029; for ($y=$start; $y<=$end; $y++) { $session = sprintf('%d-%d', $y, $y+1); echo $session . '<br>'; } output
    2020-2021 2021-2022 2022-2023 2023-2024 2024-2025 2025-2026 2026-2027 2027-2028 2028-2029 2029-2030  
  2. Barand's post in passing POST value to CURLOPT_POSTFIELDS was marked as the answer   
    Perhaps create an array then json_encode it
    EG
    $user_id = 1234; $data = [ 'user_id' => $user_id , 'name' => 'John Doe' , 'card' => 0 ]; CURLOPT_POSTFIELDS => json_encode($data),  
  3. Barand's post in mysqli_stmt::bind_param error message was marked as the answer   
    You are executing a query passing two bound parameter values, but your query has no placeholders for those parameters.
  4. Barand's post in php array format from the mysqli_fetch_assoc rows was marked as the answer   
    You have a situation where a job can have many categories and a category can contain many jobs; a many-to-many relationship. These a resolved by an intermediate table. Your data
    model should like this ...
    +-------------+ +-----------------+ | job | | category | +-------------+ +--------------------+ +-----------------+ | id |---+ | job_category | +-----| id | | job_title | | +--------------------+ | | cat_description | +-------------+ | | id | | +-----------------+ +---<| job_id | | | category_id |>---+ +--------------------+ so that where you now the equivalent of this for your job table,
    +--------+--------------+ | job_id | category_id | +--------+--------------+ | 3 | 2, 4, 5 | | 4 | 1, 2 | +--------+--------------+ you would have a job_category table like this
    +----+--------+--------------+ | id | job_id | category_id | +----+--------+--------------+ | 1 | 3 | 2 | | 2 | 3 | 4 | | 3 | 3 | 5 | | 4 | 4 | 1 | | 5 | 4 | 2 | +----+--------+--------------+ Now it's a simple to find which jobs are in a particular category as it is to find which categories a job belongs to.
  5. Barand's post in When query is empty was marked as the answer   
    The first fetch() will return false if no records were found.
    if ($result->fetch_assoc()) { // ticket found } else { // ticket not found }  
  6. Barand's post in Trying to access array of offset with if statement was marked as the answer   
    Plus, if you are accessing $_SESSION, then you need session_start() at the top of the script.
    Also errors in the query
    You are trying to match the value in column myid with the string value 'myid' You are trying to order the results by the string value 'id'
  7. Barand's post in Still some problems was marked as the answer   
    Then include that condition in your WHERE clause
    WHERE (sales.sales_date BETWEEN ? AND ?) AND products.product_id BETWEEN 50 AND 56  
  8. Barand's post in Php and javascript with timestamp and countdown was marked as the answer   
    Keep it simple for the user with datetime and time input fields
    <form method='POST'> <label for='start'>Auction Start</label> <input type='datetime-local' name='start' id='start'> <br> <label for='duration'>Duration</label> <input type='time' name='duration' id='duration'> <br><br> <input type='submit'> </form>
    Store these values in your auction table
    CREATE TABLE `auction` ( +----+---------------------+----------+ `id` int(11) NOT NULL AUTO_INCREMENT, | id | start_time | duration | `start_time` datetime DEFAULT NULL, +----+---------------------+----------+ `duration` time DEFAULT NULL, | 1 | 2023-07-17 15:00:00 | 02:30:00 | PRIMARY KEY (`id`) | 2 | 2023-07-18 12:00:00 | 03:00:00 | ) ENGINE=InnoDB;</body> +----+---------------------+----------+ When you want the auction end time for your countdown, query the database and use sql's addtime() function...
    SELECT id , start_time as start , duration , addtime(start_time, duration) as finish FROM auction; +----+---------------------+----------+---------------------+ | id | start | duration | finish | +----+---------------------+----------+---------------------+ | 1 | 2023-07-17 15:00:00 | 02:30:00 | 2023-07-17 17:30:00 | | 2 | 2023-07-18 12:00:00 | 03:00:00 | 2023-07-18 15:00:00 | +----+---------------------+----------+---------------------+  
  9. Barand's post in two different problems stmt and $result was marked as the answer   
    if (expression) return true else return false is equivalent to
    return expression  
  10. 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>'; }  
  11. 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>  
  12. 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);  
  13. 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>  
  14. 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 | +---------------+------------+--------------+  
  15. 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.
  16. 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 <<<<<<<  
  17. 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.
  18. 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.
  19. 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
  20. 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.
  21. 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.
  22. 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)  
  23. 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) ...
     
  24. 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  
  25. 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();  
×
×
  • 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.