/** * Writes $data to the csv file open in $filehandle. uses the array indices of $data for column headers * * @param string $filepath * @param array $data 2D array, first numerically-indexed, * and next-level-down preferably indexed by string * @param boolean $write_column_headers whether or not we should add the keys in the bottom-most array * as a row for headers in the CSV. * Eg, if $data looked like: * array( * 0=>array('EVT_ID'=>1,'EVT_name'=>'monkey'...), * 1=>array(...,...) * ) * * @return boolean if we successfully wrote to the CSV or not. If there's no $data, * we consider that a success (because we wrote everything there was...nothing) * @throws EE_Error */ public static function write_data_array_to_csv($filepath, $data, $write_column_headers = true) { $new_file_contents = ''; //determine if $data is actually a 2d array if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) { //make sure top level is numerically indexed, if (EEH_Array::is_associative_array($data)) { throw new EE_Error(sprintf(__("top-level array must be numerically indexed. Does these look like numbers to you? %s", "event_espresso"), implode(",", array_keys($data)))); } $item_in_top_level_array = EEH_Array::get_one_item_from_array($data); //now, is the last item in the top-level array of $data an associative or numeric array? if ($write_column_headers && EEH_Array::is_associative_array($item_in_top_level_array)) { //its associative, so we want to output its keys as column headers $keys = array_keys($item_in_top_level_array); $new_file_contents .= EEH_Export::get_csv_row($keys); } //start writing data foreach ($data as $data_row) { $new_file_contents .= EEH_Export::get_csv_row($data_row); } return EEH_File::write_to_file($filepath, EEH_File::get_file_contents($filepath) . $new_file_contents); } else { //no data TO write... so we can assume that's a success return true; } }
function ee_resurse_into_array($data) { if (is_object($data) || $data instanceof __PHP_Incomplete_Class) { //is_object($incomplete_class) actually returns false, hence why we check for it $data = (array) $data; } if (is_array($data)) { if (EEH_Array::is_associative_array($data)) { ?> <table class="widefat"> <tbody> <?php foreach ($data as $data_key => $data_values) { ?> <tr> <td> <?php echo $data_key; ?> </td> <td> <?php ee_resurse_into_array($data_values); ?> </td> </tr> <?php } ?> </tbody> </table> <?php } else { ?> <ul> <?php foreach ($data as $datum) { echo "<li>"; ee_resurse_into_array($datum); echo "</li>"; } ?> </ul> <?php } } else { //simple value echo $data; } }
/** * Writes $data to the csv file open in $filehandle. uses the array indices of $data for column headers * @param array $data 2D array, first numerically-indexed, and next-level-down preferably indexed by string * @param boolean $add_csv_column_names whether or not we should add the keys in the bottom-most array as a row for headers in the CSV. * Eg, if $data looked like array(0=>array('EVT_ID'=>1,'EVT_name'=>'monkey'...), 1=>array(...),...)) * then the first row we'd write to the CSV would be "EVT_ID,EVT_name,..." * @return boolean if we successfully wrote to the CSV or not. If there's no $data, we consider that a success (because we wrote everything there was...nothing) */ public function write_data_array_to_csv($filehandle, $data) { EE_Registry::instance()->load_helper('Array'); //determine if $data is actually a 2d array if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) { //make sure top level is numerically indexed, if (EEH_Array::is_associative_array($data)) { throw new EE_Error(sprintf(__("top-level array must be numerically indexed. Does these look like numbers to you? %s", "event_espresso"), implode(",", array_keys($data)))); } $item_in_top_level_array = EEH_Array::get_one_item_from_array($data); //now, is the last item in the top-level array of $data an associative or numeric array? if (EEH_Array::is_associative_array($item_in_top_level_array)) { //its associative, so we want to output its keys as column headers $keys = array_keys($item_in_top_level_array); echo $this->fputcsv2($filehandle, $keys); } //start writing data foreach ($data as $data_row) { echo $this->fputcsv2($filehandle, $data_row); } return true; } else { //no data TO write... so we can assume that's a success return true; } // //if 2nd level is indexed by strings, use those as csv column headers (ie, the first row) // // // $no_table = TRUE; // // // loop through data and add each row to the file/stream as csv // foreach ( $data as $model_name => $model_data ) { // // test first row to see if it is data or a model name // $model = EE_Registry::instance();->load_model($model_name); // //if the model really exists, // if ( $model ) { // // // we have a table name // $no_table = FALSE; // // // put the tablename into an array cuz that's how fputcsv rolls // $model_name_row = array( 'MODEL', $model_name ); // // // add table name to csv output // echo self::fputcsv2($filehandle, $model_name_row); // // // now get the rest of the data // foreach ( $model_data as $row ) { // // output the row // echo self::fputcsv2($filehandle, $row); // } // // } // // if ( $no_table ) { // // no table so just put the data // echo self::fputcsv2($filehandle, $model_data); // } // } // END OF foreach ( $data ) }
/** * Gets HTML for laying out a deeply-nested array (and objects) in a format * that's nice for presenting in the wp admin * @param mixed $data * @return string */ public static function layout_array_as_table($data) { if (is_object($data) || $data instanceof __PHP_Incomplete_Class) { $data = (array) $data; } EE_Registry::instance()->load_helper('Array'); ob_start(); if (is_array($data)) { if (EEH_Array::is_associative_array($data)) { ?> <table class="widefat"> <tbody> <?php foreach ($data as $data_key => $data_values) { ?> <tr> <td> <?php echo $data_key; ?> </td> <td> <?php echo self::layout_array_as_table($data_values); ?> </td> </tr> <?php } ?> </tbody> </table> <?php } else { ?> <ul> <?php foreach ($data as $datum) { echo "<li>"; echo self::layout_array_as_table($datum); echo "</li>"; } ?> </ul> <?php } } else { //simple value echo $data; } return ob_get_clean(); }