mongoose00318
Members-
Posts
253 -
Joined
-
Last visited
Everything posted by mongoose00318
-
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
Yes I saw that documentation. Sorry I guess I misunderstand. I thought that line refers to the columns in the CSV file..so if I didn't want the last column wofc, I would do... (@dummy, @dummy, enterprise, part_num, desc, qty, line_item, job_num, work_order, psm, date_change_flag, scheduled_ship_date, on_hold, on_hold_reason, total_hours, @dummy) Or if the table column names were different I would just do: (@dummy, @dummy, diff_name_enterprise, diff_name_part_num, diff_name_desc, qty, line_item, job_num, work_order, psm, date_change_flag, scheduled_ship_date, on_hold, on_hold_reason, total_hours, worfc) -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
@Barand Sorry for my delay. I've been diligently working on this and also trying to spell out some of the requirements I need to keep in mind. I will have an update for you later today. One question I have real quick about your query here: ## ## load the csv data into the table ## $db->exec("LOAD DATA LOCAL INFILE 'prod_data.csv' INTO TABLE production_csv FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES (@dummy, @dummy, enterprise, part_num, desc, qty, line_item, job_num, work_order, psm, date_change_flag, scheduled_ship_date, on_hold, on_hold_reason, total_hours, worfc) "); I've looked for a good example and haven't found one yet that shows how to specify which values go into what table columns if the columns in the CSV don't match the order of the table columns in MySQL. Do I list the fields after "FIELDS"? Also, I'm trying to learn more about triggers as you suggested instead of having the archive function. I put my script on the live server, made a .BAT file to call it in the console, and then setup a task in Windows Task Scheduler. When I tell it to "run" the task manually it works fine and as intended. But, when it fires the task automatically as scheduled it doesn't do anything at all. I have to look into this as well... 😑 But, it's getting there. -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
Hmm...not through an automated method (such as an OBDC connection). They won't allow me to do that. But, maybe an email to the right person and someone could manually change it...that's possible..my biggest concern is when it goes to update any changes to orders (daily) that it could overwrite important information that's necessary for the order.. https://imgur.com/Lo8a2uT In that example, I tell it to find orders with that job # and line item C and look for any difference between the fields, if there is a difference, backup the previous line item, and overwrite with new changes. But in this case, it would overwrite both rows 843 and 845 -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
Lol our AS400 team is one guy and they never do much to improve it...so I'm thinking that's not going happen. Its only a percentage of .92% of all the orders (granted that information is based off of only 1 day of orders).... Do you see a way that it could be accounted for somehow? Assuming the AS400 tech won't do anything about it that is... -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
Okay I get 12 entries out of 1293 rows Array ( [job_number] => 22149221 [enterprise] => SHRVIE [line_item] => E [total] => 2 ) Array ( [job_number] => 22992071 [enterprise] => SHRVIE [line_item] => E [total] => 2 ) Array ( [job_number] => 23518411 [enterprise] => LIDLUS [line_item] => C [total] => 2 ) Array ( [job_number] => 23518431 [enterprise] => LIDLUS [line_item] => C [total] => 2 ) Array ( [job_number] => 23565391 [enterprise] => LIDLUS [line_item] => B [total] => 2 ) Array ( [job_number] => 23586751 [enterprise] => SONIC [line_item] => [total] => 3 ) Array ( [job_number] => 23595111 [enterprise] => LIDLUS [line_item] => A [total] => 2 ) Array ( [job_number] => 23598551 [enterprise] => LIDLUS [line_item] => A [total] => 2 ) Array ( [job_number] => 23598551 [enterprise] => LIDLUS [line_item] => B [total] => 2 ) Array ( [job_number] => 23598561 [enterprise] => LIDLUS [line_item] => B [total] => 2 ) Array ( [job_number] => 23598561 [enterprise] => LIDLUS [line_item] => C [total] => 2 ) Array ( [job_number] => 23606661 [enterprise] => BURGER [line_item] => Q ) -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
I'm trying to figure out how common it is now. I just wrote this code but it's super slow. I'm trying to find out how many orders have more than one of the same line item: ## ## Figure out how many orders exist which have duplicate line items ## $records = $pdo->query("SELECT * FROM production_data"); $orders = []; foreach ($records as $r) { //is not in array so create it if ( !in_array( $r['job_number'], $orders ) ) $orders[ $r['job_number'] ] = []; foreach ( $orders as $key => $value ) { $get_lines = $pdo->query("SELECT line_item FROM production_data WHERE job_number = $key"); foreach ( $get_lines as $line ) { if ( !isset ( $orders[$key][$line['line_item']] ) ) $orders[$key][$line['line_item']] = 1; else $orders[$key][$line['line_item']] + 1; } } } echo '<pre>'; print_r($orders); exit(); -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
The same part number can be used in several orders and the part number can be revised on occasion if the order was entered with a bad part number...I know I'm not making this easy... Our system is ridiculous. -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
See the entry on the bottom though? It's two entries with the same job number and same line item but a different description and quantity all together. -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
Ugh...the way we store information in the AS400 is a nightmare. https://imgur.com/8gIpgg1 I have no real way to uniquely identify a job/line item...I'm trying to figure out a way to cross check the information in my database with what comes out of the AS400. I thought I could do that with the line item but see those orders in the screenshot? Order 23606661 has two line items of Q with completely different part numbers, qty, and description... Very cool tutorial. I look forward to doing it. Any ideas on how to cross check the info like I am trying to do? For example, today those orders could look like that but tomorrow a revision could have been done to one or more of those orders. I have other data tied to the order using it's id when I do the initial insert. I'm trying to post the revisions, archive the old info, and retain the ability to identify the order due to it's relationship with other data in the DB. -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
@Barand Wow that's awesome. I'm going give it a try shortly. I've been doing the same thing with SQL as I have been with arrays. It's obvious to me that there are a lot of things I used to do years ago in PHP which I could have been handling with SQL statements themselves. I recently looked at some books on SQL on Amazon but didn't order one. Maybe I need to...often times when I'm looking online I don't find comprehensive examples. I'm sure they are out there but I just haven't come across them. One question I have about it: Is it just doing a mass update of the records in production_data? Here is what I programmed by the end of Friday: //loop through new orders array foreach ( $orders as $order ) { foreach ($result as $row) { //job already exists in DB if ( $order['job_number'] == $row['job_number'] && $order['line_item'] == $row['line_item'] ) { //check description field if ( $order['description'] != $row['description'] ) { //echo $order['job_number'] . ' : description is different. New: '. $order['description'] .' | Old: '. $row['description'] .'<br>'; } else { //echo $order['job_number'] . ' : description is same<br>'; } //check AS400 ship date if ( $order['scheduled_ship_date'] != $row['as400_ship_date'] ) { echo $order['scheduled_ship_date'] . ' : ship date is different. New: '. $order['scheduled_ship_date'] .' | Old: '. $row['as400_ship_date'] .'<br>'; } else { echo $order['scheduled_ship_date'] . ' : ship date is same New: '. $order['scheduled_ship_date'] .' | Old: '. $row['as400_ship_date'] .'<br>'; } $silly_count++; //job does not exist } else { //echo $row['as400_ship_date'].'<br>'; //echo $order['job_number'] . ' LN: ' . $order['line_item'] . ' has not match.<br>'; $silly_count++; } } $silly_count++; } I'm checking for two things before I archive/update a record; is the description or date from AS400 different than what is stored in MySQL. Then I use the archives to show the user past changes made to the order in the AS400. Sometimes those changes can be very small; like a dimensional change (ie 32 1/4" goes to 32 3/4"). So I built this to help the user more easily see changes to an order: https://imgur.com/yC4sNce Sorry I was more specific in my requirements. I look forward to trying this out and getting back to you with the results. Do you have any reading suggestions when it comes to learning how to better harness the power of SQL? -
@requinix Here is that code I mentioned earlier... //fetch production history data if( isset ( $action ) && $action == 'get_production_history' ) { $order_id = $_GET['order_id']; $production_history = []; $last_recorded = 0; $dept_codes = [5,6,7,8,10,11,12]; foreach ( $dept_codes as $d ) { $production_history[$d] = []; } //echo '<pre>'; //loop through depts foreach ( $dept_codes as $d ) { //loop through returned db rows foreach ( get_production_history( $order_id, $pdo ) as $row ) { if( $row['dept_code'] == $d ) { //set start time if ( !in_array ( $row[ 'id' ], $production_history[ $d ]) && $row[ 'status_id' ] === 1 ) { $production_history[$d][ $row['id'] ] = array( 'submit_time' => $row[ 'submit_time' ], 'finish_time' => '', ); //record id $last_recorded = $row['id']; //set finished time } elseif ( $row[ 'status_id' ] === 3 ) { $production_history[$d][ $last_recorded ]['finish_time'] = $row[ 'submit_time' ]; } } } } //find records without a finish time and unset them foreach ( $production_history as $dept => $value ) foreach ($value as $k => $v) if (empty($v['finish_time'])) unset($production_history[$dept][$k]); //find departments without records and unset them foreach ( $production_history as $dept => $value ) if (empty($value)) unset($production_history[$dept]); $json = []; $dept_arr_count = 0; //sort by dept foreach ( $production_history as $dept => $value ) { //get length of dept array $dept_arr_size = count($production_history[$dept]); //if on first entry for dept print parent if ( $dept_arr_count == 0 ) { //generate parent JSON entry $json[] = array( 'pID' => $dept, 'pName' => get_dept_name( $dept, $pdo ), 'pStart' => '', 'pEnd' => '', 'pClass' => 'ggroupblack', 'pLink' => '', 'pMile' => 0, 'pGroup' => 2, 'pParent' => 0, //need to set this for those that are children 'pOpen' => 1, 'pDepend' => '', 'pCaption' => '', 'pNotes' => '', ); } $submit_time = (isset($production_history[$dept][$k]['submit_time'])) ? $production_history[$dept][$k]['submit_time'] : ''; $finish_time = (isset($production_history[$dept][$k]['finish_time'])) ? $production_history[$dept][$k]['finish_time'] : ''; //print children foreach ($value as $k => $v) { $json[] = array( 'pID' => $dept .''. $dept_arr_count+1, 'pName' => '', 'pStart' => $production_history[$dept][$k]['submit_time'], 'pEnd' => $production_history[$dept][$k]['finish_time'], 'pClass' => 'ggroupblack', 'pLink' => '', 'pMile' => 0, 'pGroup' => 0, 'pParent' => $dept, //need to set this for those that are children 'pOpen' => 1, 'pDepend' => '', 'pCaption' => '', 'pNotes' => '', ); $dept_arr_count++; } //reached end of dept array if ( $dept_arr_size == $dept_arr_count ) { $dept_arr_count = 0; } } header('Content-type: text/javascript'); print(json_encode($json, JSON_PRETTY_PRINT)); } I was pretty proud of it haha. Here is what it generates... https://imgur.com/JW9Npz2
-
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
Lol...just did a count on how many times that loop cycled through...I was off a bit from 40-50k.... It was more like 4,587,564.... Just a bit off... -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
foreach ($result as $row) { if ( $order['job_number'] == $row['job_number'] && $order['line_item'] == $row['line_item'] ) { echo 'Found match on ' . $row['id'] . '<br><br>'; } else { echo $order['job_number'] . ' LN: ' . $order['line_item'] . ' has not match.<br>'; } } That loop does essentially what I was trying to do. It's just slow...looping through all records in the table for each record in the CSV. Okay, here is my table structure: -- -- Table structure for table `production_data` -- DROP TABLE IF EXISTS `production_data`; CREATE TABLE IF NOT EXISTS `production_data` ( `id` int(11) NOT NULL AUTO_INCREMENT, `job_number` int(8) NOT NULL, `enterprise` tinytext NOT NULL, `part_number` text NOT NULL, `description` text NOT NULL, `qty` int(11) NOT NULL, `line_item` varchar(11) NOT NULL, `as400_ship_date` date DEFAULT NULL, `hold_status` tinyint(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; COMMIT; And here is a list of the columns I'm actually using from the CSV. It has other columns I'm not using though. //setup col vars for easier editing $enterprise = $column[2]; $part_num = $column[3]; $desc = $column[4]; $qty = $column[5]; $line_item = $column[6]; $job_num = $column[7]; $work_order = $column[8]; $psm = $column[9]; $date_change_flag = $column[10]; $scheduled_ship_date = $column[11]; $on_hold = $column[12]; $on_hold_reason = $column[13]; $total_hours = $column[14]; $worfc = $column[15]; After I find a match I'd like to compare certain fields in DB record against CSV row to see which ones have differences then make a backup of DB record and update the DB record with the new information from the CSV. -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
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. -
Store part of array when array_search is successful
mongoose00318 replied to mongoose00318's topic in PHP Coding Help
@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. -
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.
-
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.
-
Okay very cool. Thanks a lot @requinix
-
So $number is essentially being returned as an argument for the array_map() function?
-
Also, what does the &$value do vs just $value in the below example? array_walk( $fruits, function( &$value, $key ) { $value = "$key is ".strtoupper($value); });
-
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...
-
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; }
-
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
-
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.
-
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.