Sorry I'm having a hard time explaining.
I have a form in which I can select the fields in my database. (I will send the data to an Excel doc.)
<form action="db_all.php" method="post" name="getall">
<select name="all_data[]" size="10" multiple="multiple">
<option value="MemberID" selected="selected">MemberID</option>
<option value="FirstName" selected="selected">FirstName</option>
<option value="LastName" selected="selected">LastName</option>
<option value="Company" selected="selected">Company</option>
<option value="Address" selected="selected">Address</option>
<option value="City" selected="selected">City</option>
<option value="State" selected="selected">State</option>
<option value="PostalCode" selected="selected">PostalCode</option>
<option value="EmailAddress" selected="selected">EmailAddress</option>
<option value="PhoneNumber" selected="selected">PhoneNumber</option>
<option value="MemberInterest" selected="selected">MemberInterest</option>
<option value="SockSize" selected="selected">SockSize</option>
<option value="MemberTypeID" selected="selected">MemberTypeID</option>
<option value="JoinDate" selected="selected">JoinDate</option>
<option value="RenewalDate" selected="selected">RenewalDate</option>
<option value="LastPayment" selected="selected">LastPayment</option>
<option value="ModifyDate" selected="selected">ModifyDate</option>
</select>
<input type="submit" name="button4" id="button4" value="Submit" />
</form>
I select which fields I want and then build a SELECT statement
$field = $_POST['all_data']; // get the fields from form
foreach ($field as $value) {
$query_string.= $value.",";// build a string to insert into SELECT statement
So I end up with the fields I'm going to select from the database - for example my $query_string might be FirstName, LastName, PhoneNumber,
and I strip off the last comma.
$str = substr($query_string, 0, -1); // remove the last comma from query_string
and then plug $query_string into my SELECT statement and pull the info from the database
mysql_select_db($database_Membership, $Membership);
$query_members = "SELECT ".$str." FROM members";
$members = mysql_query($query_members, $Membership) or die(mysql_error());
$row_members = mysql_fetch_assoc($members);
$totalRows_members = mysql_num_rows($members);
So now I have the variables (for each person in the database).
$row_members['FirstName']
$row_members['LastName']
$row_members['PhoneNumber']
I'm ready to send it off to my Excel Doc. So I create the Excel doc
# file name for download
$filename = "mailing_" . date('Ymd') . ".xls";
//headers for Excel file
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
and now need to add the data to the Excel doc - in this case FirstName, LastName, PhoneNumber.
This code would work (and add the proper tabs and line breaks);
do {
echo $row_members['FirstName']."\t".$row_members['LastName']."\t".$row_members['PhoneNumber']."\n";
}
while ($row_members = mysql_fetch_assoc($members));
Of course I need the above do while loop to contain the variables that I choose in the original form.
So my (not so bright and useful) idea was I would just build this echo command the same time I build the SELECT statement with my convoluted code of
$q = "'";
$excel_string.= '$row_members['.$q.$value.$q.']."\t".'; //build a string to use for excel document
It does. The variable $excel_string in this example would be equal to
$row_members['FirstName']."\t".$row_members['LastName']."\t".$row_members['PhoneNumber']."\n";
But when I plug $excel_string into my do while loop
do {
echo $excel_string;
}
while ($row_members = mysql_fetch_assoc($members));
I don't get the data, instead I get
$row_members['FirstName']."\t".$row_members['LastName']."\t".$row_members['PhoneNumber']."\n";
I admit my code may suck, but instead of rewriting it all I was hoping there would be a command that would simple convet this $excel_string so it would actual insert the data, instead of inserting the variable names (figuring I'd done something funky to the string variable).