Jump to content

Undefined variables


RyanMinor

Recommended Posts

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;
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.