Returning array
-
I’ve been banging my head on the desk for a while over this. Seems like it should be pretty simple but… not working. Trying to simply return an array from a function.
I have a grid showing database results. If any are in error, there should be notifications as such above the grid.
-I call a function which checks the results for errors, and counts them. Within this function, another function is called to display the notification indicating the quantity of errors. This works properly.-These notification also provides a link to view the database record in question – primary key ‘id’ is returned in an array from the function. However, the array that I am returning that should contain the ‘id’ of the erroneous record comes out of the function empty. If I var_dump within the function, the array contains the records I would expect it to.
The code for the main page flows like this (code is abbreviated):
include 'header.php' include 'error_check.php' (file containing the error check function) include 'notices.php' (file containing the function to display notices) check_for_errors($qdate, $salesman_id); if($_REQUEST['view'] = 'nomatch' { include 'nomatch_grid.php' } if($_REQUEST['view'] = 'unknown' { include 'unknown_grid.php' } if(!$_REQUEST['view'] { include 'main_grid.php' }
Here is the code for the check_for_errors() function:
function check_for_errors($cfe_date, $salesman_id) { global $wpdb; $d_year = date('Y', $cfe_date); $d_month = date('m', $cfe_date); $spiffs = $wpdb->get_results("SELECT * FROM spiff_records LEFT JOIN spiff_items ON spiff_records.item = spiff_items.item_number WHERE YEAR(inv_date) = $d_year AND MONTH(inv_date) = $d_month AND salesman_id = $salesman_id ORDER BY inv_date ASC"); /** * RUN THROUGH ALL SPIFFS RETURNED IN MAIN QUERY * ASSIGN COUNT OF HOW MANY RECORDS DONT MATCH AN ITEM * CREATE ARRAY CONTAINING <code>SPIFF_ID</code> WITH ITEMS THAT DON'T MATCH **/ $nomatchct = 0; $nmarray = array(); foreach($spiffs as $nomatch) { if(!$nomatch->item_number && $nomatch->unknown != 1) { $nomatchct = $nomatchct + 1; $nmarray[] = (string)$nomatch->spiff_id; } } /** * RUN THROUGH ALL SPIFFS RETURNED IN MAIN QUERY * ASSIGN COUNT OF OUT HOW MANY ARE MARKED AS UNKNOWN BY ADMIN * CREATE ARRAY CONTAINING <code>SPIFF_ID</code> WITH RECORDS MARKED AS UNKNOWN **/ $unknownct = 0; foreach($spiffs as $unknown) { if($unknown->unknown == 1) { $unknownct = $unknownct + 1; $ukarray[] = (string)$unknown->spiff_id; } } display_notices($unknownct, $nomatchct); return $ukarray; }
Finally, here is the link for the display_notices() function:
function display_notices($unknownct, $nomatchct) { if($unknownct > 0) { echo '<div class="notice red">'; echo '<p><strong>NOTICE:</strong> You have ' .$unknownct. ' records with unknown item numbers. <a href="/view/?view=unknown">Click here to review these entries.</a></p>'; echo '</div>'; } if($nomatchct > 0) { echo '<div class="notice green">'; echo '<p><strong>NOTICE:</strong> You have ' .$nomatchct. ' records waiting to be reviewed. <a href="/view/?view=nomatch">Click here to review these entries.</a></p>'; echo '</div>'; } }
Since I am using includes, the check_for_errors() is run no matter which grid we are looking at, so if I return the $ukarray or the $nmarray, I should be able to use those arrays in order to display the records who’s ID is in that array. Or So I thought.
If anyone has time to check this out and let me know what they think I’d greatly appreciate it!!
- The topic ‘Returning array’ is closed to new replies.