RyanMinor Posted January 20, 2011 Share Posted January 20, 2011 I am trying to allow users of a web page the ability to create custom Excel exports. I have some code that I use elsewhere that works to make Excel exports. When I moved the code over to my custom report page, the Excel chart contains an error saying that $csv_output is undefined and so is $data (both variables toward the end of the page). I tried declaring the variables ($csv_output = ''; and $data = '' above the Excel code, but that didn't work. Can anybody help me out? Code below... <?php //////////////////// INITIATE SESSION //////////////////// if (!isset($_SESSION)) { session_start(); } //////////////////// ALLOW ACCESS ONLY TO ADMINISTRATORS //////////////////// if ($_SESSION['person_priveleges'] == 0) { header("location:index.php"); } //////////////////// CONNECT AND SELECT DATABASE //////////////////// require_once('connect.php'); mysql_select_db($database, $connect); //////////////////// APPLY VARIABLES //////////////////// $state_search = ""; $payment_status = ""; $method_search = ""; //////////////////// GET USER SEARCH RESULTS //////////////////// if (array_key_exists('search', $_GET)) { // FILTER USER INPUT // // HERE // /////////////////////// // INITIALIZE ERROR ARRAY // $error = array(); // IF ALL INPUT FIELDS ARE LEFT BLANK THROW AN ERROR // if ((empty($_GET['state_search'])) && (empty($_GET['payment_status'])) && (empty($_GET['payment_method']))) { $error['search'] = "Please enter at least one search parameter."; // IF NO ERRORS START THE QUERY // } else { $search = "SELECT * FROM person"; // SET WHERE PART OF QUERY TO FALS SINCE WE DO NOT HAVE ANY WHERE PARAMETERS // $where = false; // IF THE PERSON SELECTS A STATE ASSIGN TO A VARIABLE AND ADD TO QUERY // if (isset($_GET['state_search']) && !empty($_GET['state_search'])) { $state_search = $_GET['state_search']; // FOR EACH STATE THAT WAS SELECTED // foreach ($state_search as $state) { // IF THERE ALREADY IS A WHERE CLAUSE ADD THE FOLLOWING CODE TO THE QUERY // if ($where) { $search .= " AND person_state = '$state'"; // IF THERE IS NO WHERE CLAUSE ADD THE FOLLOWING CODE TO THE QUERY AND SET WHERE TO TRUE SO FUTURE ADDITIONS WILL BE CORRECT // } else { $search .= " WHERE person_state = '$state'"; $where = true; } } } // IF THE PERSON SELECTS A PAYMENT STATUS ASSIGN TO A VARIABLE AND ADD TO QUERY // if (isset($_GET['payment_status']) && !empty($_GET['payment_status'])) { $payment_status = $_GET['payment_status']; foreach ($payment_status as $status) { if ($where) { $search .= " AND person_paid = '$status' "; } else { $search .= " WHERE person_paid = '$status' "; $where = true; } } } // IF THE PERSON SELECTS A PAYMENT METHOD ASSIGN TO A VARIABLE AND ADD TO QUERY // if (isset($_GET['payment_method']) && !empty($_GET['payment_method'])) { $payment_method = $_GET['payment_method']; foreach ($payment_method as $method) { if ($where) { $search .= " AND person_payment_method = '$method' "; } else { $search .= " WHERE person_payment_method = '$method' "; $where = true; } } } $search .= " AND person_priveleges = 0 ORDER BY person_last_name"; } $query_search = mysql_query($search, $connect) or die(mysql_error()); // CODE TO GENERATE EXCEL OUTPUT // // GET THE NUMBER OF FIELDS // $fields = mysql_num_fields($query_search); // FOR EACH RECORD IN THE DATABASE PRODUCE SOME OUTPUT // for ($i = 0; $i < $fields; $i++) { $csv_output .= mysql_field_name($query_search, $i) . "\t"; } while($row = mysql_fetch_row($query_search)) { foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r","",$data); header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=Registration Report for IDUES 2011 Project Directors Meeting.xls"); header("Pragma: no-cache"); header("Expires: 0"); print $csv_output."\n".$data; exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225073-undefined-variables/ Share on other sites More sharing options...
BlueSkyIS Posted January 20, 2011 Share Posted January 20, 2011 post the complete error messages. Quote Link to comment https://forums.phpfreaks.com/topic/225073-undefined-variables/#findComment-1162509 Share on other sites More sharing options...
Maq Posted January 20, 2011 Share Posted January 20, 2011 Your wording suggests that the errors are located in the Excel sheet, is this true? (and yes, please post the exact error messages) Quote Link to comment https://forums.phpfreaks.com/topic/225073-undefined-variables/#findComment-1162533 Share on other sites More sharing options...
AbraCadaver Posted January 20, 2011 Share Posted January 20, 2011 The code is trying to use the variables via concatenation ( .= ) before they have been defined: Before this: for ($i = 0; $i < $fields; $i++) { Add: $csv_output = ''; Before this: while($row = mysql_fetch_row($query_search)) { Add: $line = ''; $data = ''; Quote Link to comment https://forums.phpfreaks.com/topic/225073-undefined-variables/#findComment-1162546 Share on other sites More sharing options...
RyanMinor Posted January 20, 2011 Author Share Posted January 20, 2011 Sorry for not posting the whole error message. When I declared the variables above the loops and then changed the "AND" to an "OR" in the queries it worked except for... 1) The data is displayed in Excel side by side. For example...Let's say there are 3 records returned in the Excel output. The first record and it's data is displayed three times vertically. Then, the next record is displayed two times vertically, but is displayed right next to the first record. The third record is displayed once vertically, but right next to the second record. It's like instead of listing the data vertically, it is listing it horizontally. I hope I explained this correctly. Anybody have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/225073-undefined-variables/#findComment-1162629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.