Jump to content

Barand

Moderators
  • Posts

    24,324
  • Joined

  • Last visited

  • Days Won

    794

Community Answers

  1. Barand's post in Access single array element was marked as the answer   
    For the "[0]" to be needed then the json data would need to be
    [{ "id": 1, "item_id": 19, "min_qty": 1, "max_qty": 499, "GBP": 500, "USD": 750, "CAD": 875 }] with the outer [..] to make it an array.
  2. Barand's post in How to make visible of unvisible error while inserting data into mariadb database? was marked as the answer   
    One way is not to turn off the error reporting...
     
     
  3. Barand's post in Validating password characters was marked as the answer   
    Do your database , table and db connection all share the same utf8 encoding?
  4. Barand's post in Ajax log on handler wrapping redirected to page in response content was marked as the answer   
    When you send an ajax request to a page, all output then goes into the response that is returned. (ie what you would see if you called that page in your browser is the reponse (which you can use to your advantage to test ajax responders).
    Remove the location header from the responder code and do the redirect in the calling page when you receive a succesful response.
  5. Barand's post in Current password hashing standard was marked as the answer   
    password_hash() and password_verify()
  6. Barand's post in Duplicating Mysql Table rows but also updating some content was marked as the answer   
    Start with
    SELECT q.id , q.version FROM quote q JOIN ( SELECT client_id , max(version) as version FROM quote WHERE client_id = 15 ) latest USING (client_id, version); +----+---------+ | id | version | +----+---------+ | 71 | 6 | +----+---------+ Now you know that for client #15 the latest quote has id = 71 and its version is 6.
    Duplicate quote #71, giving the dupe a verion of 7, and duplicate the items for #71 with the quote_id of the new duped quote.
  7. Barand's post in Sql statement join question was marked as the answer   
    Something like this? ...
     
    SELECT j.id as job_id , j.name , count(q.id) as quotes FROM job j LEFT JOIN quote q ON j.id = q.job_id GROUP BY j.id  
  8. Barand's post in [Suggestions] - Ordering Items Dynamically was marked as the answer   
    -- first UPDATE thetable SET display_order = display_order + 1 WHERE display_order >= 2; -- then INSERT INTO thetable (item_name, display_order) VALUES ('Item 4', 2);  
  9. Barand's post in Build multidimentional array from select was marked as the answer   
    Oops!
    FETCH_GROUP should be FETCH_ASSOC.
    You also need to change the order of your selected columns so that the first two are the ones you are using as the keys, then the array_slice takes the rest..
  10. Barand's post in For each loop not getting all data was marked as the answer   
    Shouldn't that be
    if(!isset($data[$item['sectionName']])){ ^ What is the relevance of the data you posted to the code?
  11. Barand's post in More efficient rounding calculator was marked as the answer   
    The code you posted was using PHP variable names. ($days, $weeks)
    intdiv (x, y) == floor(x/y)
  12. Barand's post in On duplicate key update always inserting new row was marked as the answer   
    You should be getting a mysql error - you have 7 placeholders but only passing 6 values in the execute. The way you have defined it you need to pass the $_POST['quotename'] twice.
    A better way is
    ... ON DUPLICATE KEY UPDATE name=VALUES(name) telling it to use the same value as in the VALUES clause. The you only need provide the name once.
    As for your problem, what has to be duplicated to throw a duplicate key error? Your id will be auto_incremented so that won't be duplicated.
  13. Barand's post in Insert Ignore was marked as the answer   
    Just alter the table to add the unique constraint. Run this query...
    ALTER TABLE `tablename` ADD UNIQUE INDEX `unq_user_date` (`user_id` ASC, `timestamp` ASC); Checking if it exists first is the worst way to do it.
  14. Barand's post in String help was marked as the answer   
    One way...
    $str = "1 x MAKE: Behringer, 1 x MODEL: X-32, 1 x Problem with unit: Not powering on"; $a = explode(': ', $str); unset ($a[0]); foreach ($a as $b) { $results[] = explode(',', $b)[0]; } echo '<pre>results = ' . print_r($results, 1) . '</pre>'; output...
    results = Array ( [0] => Behringer [1] => X-32 [2] => Not powering on )  
  15. 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  
  16. 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),  
  17. 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.
  18. 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.
  19. 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 }  
  20. 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'
  21. 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  
  22. 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 | +----+---------------------+----------+---------------------+  
  23. 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  
  24. 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>'; }  
  25. 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>  
×
×
  • 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.