Jump to content

mongoose00318

Members
  • Posts

    242
  • Joined

  • Last visited

Everything posted by mongoose00318

  1. Exactly what I'm trying to do. I've been learning about arrays and how much faster it is to do large complicated checks using them instead of hitting the database with repeated queries...which is why I'm trying to do it this way. If I wrote this with a query, it would probably result in 40,000-50,000 queries before it finished running...at least how I know to write it lol. So I did get the key from the matched array and the documentation mentions how array_search returns the key of it's first match. So on some matchs I am getting the wrong keys returned....maybe this is the wrong way to go about this... Basically, if the DB already has a job number and a line item which matches the new data; I want to check each field of that DB row against the new data to see if anything is different. If there is a difference I want to update those records and backup the old records in a different table. I am comparing a large CSV file against a table with almost 5,000 records.
  2. @Barand It's just a lot of values to compare against. I was trying to store all the rows of the particular table in an array and use the array to do the comparisons on; then when certain criteria is met, make an update on the specific record a match was found on.
  3. Hello, I'm trying to store the match I get in $result to a variable so that I can easily compare its other values against other values. Here is my code.. //loop through new orders array foreach ( $orders as $order ) { //compare job number and line item against database information for a match if ( array_search ( $order[ 'job_number' ], array_column ( $result, 'job_number' ) ) !== false && array_search ( $order[ 'line_item' ], array_column ( $result, 'line_item' ) ) !== false ) { $db_match = key($result); echo '<br><br>'.$db_match.'<br><br>'; echo 'Job: ' . $order['job_number'] . '/Line Item: ' . $order['line_item'] . ' was found<br>'; //new order which doesn't exist in database } else { echo 'Job: ' . $order['job_number'] . '/Line Item: ' . $order['line_item'] . ' was not found<br>'; } } When I do $db_match = key($result); I get 0 each time and when I do: $db_match = array_search ( $order[ 'job_number' ], array_column ( $result, 'job_number' ) ) !== false && array_search ( $order[ 'line_item' ], array_column ( $result, 'line_item' ) ) !== false; I get 1 each time...which I assume is the number of matches which meet that criteria instead of the array key I found a match on.
  4. I'll post some code later that I wrote yesterday building an array which is then passed as JSON to generate a Gannt chart on the fly. I'm proud of it for now...until you guys review it and tell me how repetitive my code is haha. It's cool learning to be more efficient with my code and really cool learning how to better build and interact with arrays. Back when I was still developing full time before I stepped away from it for several years; arrays was always something I had trouble with. I feel like I'm learning a lot about them now and it's making my life a lot easier.
  5. Okay very cool. Thanks a lot @requinix
  6. So $number is essentially being returned as an argument for the array_map() function?
  7. Also, what does the &$value do vs just $value in the below example? array_walk( $fruits, function( &$value, $key ) { $value = "$key is ".strtoupper($value); });
  8. I've seen this recently when looking at some PHP code and it feels very familiar because it's done in Javascript a lot. But, I don't have a good understanding of it even then...also, I'm not sure "inline functions" is the actual names of these...here is a sample... $cities = ['Berlin', 'KYIV', 'Amsterdam', 'Riga']; $aliases = array_map('strtolower', $cities); print_r($aliases); // ['berlin', 'kyiv, 'amsterdam', 'riga'] $numbers = [1, -2, 3, -4, 5]; $squares = array_map(function($number) { return $number ** 2; }, $numbers); print_r($squares); // [1, 4, 9, 16, 25] So where it says (array_map(function($number) {.... I'm confused about this part...normally when I write a function like... function($some_var) { return $some_var; } $some_var is acting as an input to the function...with the function sample above is $number acting as an output? $number has no value before the declaration of the function right? Hopefully I've worded this all correctly...
  9. I am checking the data coming in; here is my code for doing that and then setting the value's of the variables: //filters are set if(isset($_GET['filters'])){ //check field for manipulation $field = $_GET['filters'][0]['field']; if ( $field == 'Job Number' ) $field = 'job_number'; else if ( $field == 'Enterprise' ) $field = 'enterprise'; else exit(); //check type for manipulation $type = $_GET['filters'][0]['type']; if ( $type == '=' ) $type = '='; else if ( $type == 'like' ) $type = 'LIKE'; else exit(); //check value for job_number as numeric $value = $_GET['filters'][0]['value']; if ( $field == 'job_number' && trim( !is_numeric ( $value ) ) ) exit(); else if ( $type == 'LIKE' ) $value = "'%" . $value . "%'"; $filter_data = TRUE; }
  10. I'm trying to clean up all my functions with any queries which take dynamic parameters using PDO prepared statements. I originally thought I was using prepared statements and was told later I wasn't so it's been on my to-do list to go and clean them up. I have cleaned up a lot of them and tested them and they are working fine. This one is giving me a problem though.. /*fetch production data*/ if( isset( $field ) && isset( $type ) && isset( $value ) ) { $sql = 'SELECT id, job_number, enterprise, description, line_item, as400_ship_date FROM production_data WHERE :field :type :value ORDER BY enterprise, job_number, line_item LIMIT :offset, :records_per_page'; $stmt = $pdo->prepare($sql); $stmt->execute( [ 'field' => $field, 'type' => $type, 'value' => $value, 'offset' => $offset, 'records_per_page' => $records_per_page ] ); } else { $sql = 'SELECT id, job_number, enterprise, description, line_item, as400_ship_date FROM production_data ORDER BY enterprise, job_number, line_item LIMIT '. $offset . ', '. $records_per_page; } It takes values from some drop downs (I'm using tabulator to generate a table and this part is related to it's pagination functions)...here is the HTML for those elements. <div class="table-controls"> <div class="form-row"> <div class="col"> <label for="filter-field" class="col-form-label-sm">Field: </label> <select id="filter-field" class="form-control form-control-sm"> <option></option> <option value="Job Number">Job Number</option> <option value="Enterprise">Enterprise</option> </select> </div> <div class="col"> <label for="filter-type" class="col-form-label-sm">Type: </label> <select id="filter-type" class="form-control form-control-sm"> <option value="like" selected="selected">Like</option> <option value="=">Equal to</option> </select> </div> <div class="col"> <label for="filter-value" class="col-form-label-sm">Value: </label> <input id="filter-value" class="form-control form-control-sm" style="float: left;" type="text" placeholder="Value to filter..."> </div> <div class="col d-flex align-content-end flex-wrap"> <button id="filter-clear" class="btn btn-primary btn-sm rounded-0">Clear Filters & Sorting</button> </div> </div> </div> After attempting to put adjust the query to a prepared statement, I get the following error: Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '? ? ORDER BY enterprise, job_number, line_item LIMIT ?, ?' at line 3 in C:\wamp64\www\test\scripts\order_status.php on line 125
  11. What do you think about this? $today = new DateTime("now"); $expected_complete_time = new DateTime($stat[ 'expected_complete_time' ]); $eta = $today->diff($expected_complete_time); $str_eta = $eta->format('%r%d'); if($expected_complete_time->format('d/m/Y') === $today->format('d/m/Y')) { $eta = 'Today'; } else { $eta = ($str_eta+1 == 1) ? $str_eta+1 . ' day' : $str_eta+1 . ' days'; } https://imgur.com/HTiuvrF Seems to be doing what I need.
  12. Also, it must be rounding or something? If I set the date for tomorrow; the difference I get is 0. If I put the expected date 2 days out; I get back a difference of 1. Maybe it's using the time in combination with date and not getting a whole day for the difference because of that? The date in the database doesn't have a time. It's just the date.
  13. Oops..yea that fixed it. But. I'm getting - and + on 0 values...? Can I just tell it not to do anything for a 0? https://imgur.com/a/Vn9RmFl Also, on this date... Now: 01/05/2020: Expected: 04/05/2020 Diff: +2 Shouldn't that say 3 days?
  14. I was trying to get something like: Now: 01/05/2020: Expected: 22/04/2020 Diff: -9 Now: 01/05/2020: Expected: 01/05/2020 Diff: 0 Now: 01/05/2020: Expected: 01/05/2020 Diff: 0 Now: 01/05/2020: Expected: 22/04/2020 Diff: -9 Now: 01/05/2020: Expected: 23/04/2020 Diff: -8 Now: 01/05/2020: Expected: 04/05/2020 Diff: +2 (but shouldn't this actually be +3?) Basically user says the amount of days it takes to complete some work and if today's date is passed that day then it shows a negative value.
  15. No matter the method I use I get positive dates when they should be negative? Here's changes to my code and the results... $today = new DateTime("now"); $expected_complete_time = new DateTime($stat[ 'expected_complete_time' ]); $eta = $expected_complete_time->diff($today); $str_eta = $eta->format('%R%d'); echo ': Now: ' . $today->format('d/m/Y') . ': Expected: ' . $expected_complete_time->format('d/m/Y') . ' Diff: ' . $str_eta; Now: 01/05/2020: Expected: 22/04/2020 Diff: +9 Now: 01/05/2020: Expected: 01/05/2020 Diff: +0 Now: 01/05/2020: Expected: 01/05/2020 Diff: +0 Now: 01/05/2020: Expected: 22/04/2020 Diff: +9 Now: 01/05/2020: Expected: 23/04/2020 Diff: +8 Now: 01/05/2020: Expected: 04/05/2020 Diff: -2
  16. I'm trying to subtract get the total number of days between two dates. I have been trying the DateTime date_diff method and the strtotime methods. I have it working mostly; but I have a problem with if the value should be negative. I've tried the invert() on the value that comes back from the date_diff() function but get errors. To sum up what I'm trying to do is: if ( $diff === 0 ) echo "Today" elseif ($diff > 0) echo "+" . $diff . "Days"; else echo "LATE -" . $diff . "Days"; Something like that. Here is my code... //retrieve expected complete time if ( isset( $stat[ 'expected_complete_time' ] ) ) { $today = new DateTime("now"); $expected_complete_time = new DateTime($stat[ 'expected_complete_time' ]); $today->setTime(12, 0, 0); $expected_complete_time->setTime(12, 0, 0); //calculate difference $eta = date_diff($expected_complete_time, $today)->format("%d");; //future if ($today > $expected_complete_time) { $eta = '+' . $eta; //today } elseif ($eta === '0') { $eta = 'Today'; //overdue } else { $eta = 'LATE:-' . $eta; } } else $eta = '';
  17. Awesome. Thanks for the clarity & insight! I will look into it some more.
  18. Well sort of...I mean does it inherit the object assigned to $(this) from the outside code unless you redefine it later and can you redefine the object assigned to $(this)? Did I phrase that right?
  19. Can I still this $(this) if I remove the second argument?
  20. 😐 I figured it out. Sorry, it was a stupid mistake on my part. This fixed it. //compare changes buttons $('body').on('click', '.compare-change', function() { //$('.compare-change', this).click(function() { var buttonText = $(this).text(); if (buttonText == 'Compare') { $(this).text('Hide'); } else { $resultsRow = $(this).closest('tr').next(); if ($resultsRow.attr('class') == 'compare-results') { $resultsRow.remove(); $(this).text('Compare'); exit(); //alert('hellow'); } } var callerCellIndex = $(this).prevAll().length; //current cell index var callerCellRow = $(this).closest('tr'); //current row of caller cell var callerPrevRow = callerCellRow.prev(); //row before caller cell var resultsRowHTML = '<tr class="compare-results"><th scope="row"></th><td>test</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'; // create new row to display results in callerCellRow.after(resultsRowHTML); //loop through each cell in callerCellRow $(callerCellRow.children('td:not(:last)'), this).each(function () { }); });
  21. I accomplished what I needed. I switched to using this library: https://github.com/google/diff-match-patch Overall, it provided more of what I needed in a much easier to use way. It works great.
  22. I’m trying to remove a the row below the row which the function is called within; if the the next row has a class of “compare-results”. My code works fine except for the remove(). It alerts within the code block but won’t delete the row… //compare changes buttons $('.compare-change', this).click(function() { var buttonText = $(this).text(); if (buttonText == 'Compare') { $(this).text('Hide'); } else { $resultsRow = $(this).closest('tr').next(); if ($resultsRow.attr('class') == 'compare-results') { $resultsRow.remove(); alert('hellow'); } $(this).text('Compare'); } var callerCellIndex = $(this).prevAll().length; //current cell index var callerCellRow = $(this).closest('tr'); //current row of caller cell var callerPrevRow = callerCellRow.prev(); //row before caller cell var resultsRowHTML = '<tr class="compare-results"><th scope="row"></th><td>test</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'; // create new row to display results in callerCellRow.after(resultsRowHTML); //loop through each cell in callerCellRow $(callerCellRow.children('td:not(:last)'), this).each(function () { }); }); This is the problem area: if ($resultsRow.attr('class') == 'compare-results') { $resultsRow.remove(); alert('hellow'); }
  23. Hello, I'm using the JsDiff library and trying to compare each cell in a row to it's corresponding cell in the next row; then highlight the differences in the 2nd cell from the first cell. This is how far I've come... https://imgur.com/a/bCuEpUH The problem is I need it to highlight where the difference is; not just the entire cells contents. Also, when I set it up in jsFiddle it's not working as it does on my end. I think it's a different version of JsDiff but not sure. Here is my jsFiddle: https://jsfiddle.net/7fhunqde/6/
  24. Okay...I was on the right track. I thought it had to do with my event binding. I fixed it like so... $('body').on('click', '.btn_change_order_status_dialog', function() { //get data from button var from_order_id = $(this).data('order-id'); //get order id var from_order_number = $(this).data('order-number'); //get order number var from_order_enterprise = $(this).data('order-enterprise'); //get enterprise var from_dept_code = $(this).data('dept-code'); //get dept code //dialog box data containers var to_order_id = $('h5 span#order_id'); //get order id var to_order_number = $('h5 span#order_number'); //get order number var to_order_enterprise = $('h5 span#enterprise'); //get enterprise var to_dept_code = $('h5 span#dept_code'); //get dept code //update values in dialog box before showing user to_order_id.text(from_order_id); //set order id to_order_number.text(from_order_number); //set order number to_order_enterprise.text(from_order_enterprise); //set enterprise to_dept_code.text(from_dept_code); //set dept code //hide all containers $( '#datepicker-container' ).hide(); //hide datepicker-container $( '#delay-container' ).hide(); //hide delay-container $( '#finish-container' ).hide(); //hide finish-container //open dialog box $('#change_order_status_dialog').dialog('open'); }); If I were to reload the data in that table with ajax on an interval will my events become unbinden again? If so, how can I rebind them?
×
×
  • 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.