mancorn Posted December 30, 2008 Share Posted December 30, 2008 Sorry for such a basic question but I can't get this to work $value = FirstName; $q = "'"; $excel_string = '$row_members['.$q.$value.$q.']'; $row_members['FirstName'] = Sam; echo "$excel_string"; // dipslays as $row_members['FirstName'] echo "<br />"; echo $row_members['FirstName']; // displays as Sam I want $excel_string to display as "Sam" not "$row_members['FirstName']". Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/ Share on other sites More sharing options...
daveoffy Posted December 30, 2008 Share Posted December 30, 2008 Try this $value = FirstName; $q = "'"; $excel_string = '$row_members['.$q.$value.$q.']'; $members = $row_members['FirstName']; $row_members['FirstName'] = Sam echo "$excel_string"; // dipslays as $row_members['FirstName'] echo "<br />"; echo $members; // displays as Sam Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726027 Share on other sites More sharing options...
mancorn Posted December 30, 2008 Author Share Posted December 30, 2008 This simply assigns a variable ($members) the value of $row_members['FirstName'] and then dipslays that variable. I need to convert the $excel_string variable. Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726030 Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 try this bro instead of $excel_string = '$row_members['.$q.$value.$q.']'; try $excel_string = $row_members["$q$value$q"]; Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726032 Share on other sites More sharing options...
RussellReal Posted December 30, 2008 Share Posted December 30, 2008 I'm sorry dude.. I messed up try this $value = "FirstName"; $q = "'"; $row_members['FirstName'] = "Sam"; $excel_string = $row_members[$value]; echo $excel_string; // dipslays as $row_members['FirstName'] echo "<br />"; echo $row_members['FirstName']; // displays as Sam please note the quotations.. single quotes DO NOT evaluate variables and special characters.. doubel quotes EVALUATE variables and special characters Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726033 Share on other sites More sharing options...
mancorn Posted December 30, 2008 Author Share Posted December 30, 2008 Once again this is just assigning the variable $row_members['FirstName'] the value of Sam and then displays the value (echo $row_members['FirstName']. What I'm trying to do is build the php code before I know the variables. I'm getting data from a database with a SELECT statement build from form variables and then pass the data into an Excel doc. // find all the fields we want to select from database $field = $_POST['all_data']; // get the data from dp_mailing.php "ALL" form foreach ($field as $value) { $query_string.= $value.",";// build a string to insert into SELECT statement $q = "'"; $excel_string.= '$row_members['.$q.$value.$q.']."\t".'; //build a string to use for excel document //$excel_string.= '$row_members["$q$value$q"]."\t".'; } $str = substr($query_string, 0, -1); // remove the last comma from query_string $excelstr = substr($excel_string, 0, -4); // remove the TAB characters from excel string "\t" $excelstr = $excelstr.'\n."'; // add line break at end of excel string "\n" 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); # 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"); do { $excelstr ; } while ($row_members = mysql_fetch_assoc($members)); echo "end"; So I wanted the $excelstr variable to be a chunk of PHP code. It might look like $row_members['FirstName']."\t".$row_members['LastName']."\t".$row_members['Company']."\t" But I won't know the exact variables until I get the data out of the database. So I was looking for a way to convert the $excelstr variable. If I use do { echo $row_members['MemberID']."\t".$row_members['FirstName']."\t".$row_members['LastName']."\t".$row_members['Company']."\t".$row_members['Address']."\t".$row_members['City']."\t".$row_members['State']."\t".$row_members['PostalCode']."\t".$row_members['EmailAddress']."\t".$row_members['PhoneNumber']."\n"; } while ($row_members = mysql_fetch_assoc($members)); echo "end"; It works OK. But I want to be able to have this last bit of data be the same as $excelstr instead of hard coding. Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726045 Share on other sites More sharing options...
mancorn Posted December 30, 2008 Author Share Posted December 30, 2008 Sorry Posted again with proper formating so it's easier to read (newbie). <?php // find all the fields we want to select from database $field = $_POST['all_data']; // get the data from dp_mailing.php "ALL" form foreach ($field as $value) { $query_string.= $value.",";// build a string to insert into SELECT statement $q = "'"; $excel_string.= '$row_members['.$q.$value.$q.']."\t".'; //build a string to use for excel document } $str = substr($query_string, 0, -1); // remove the last comma from query_string $excelstr = substr($excel_string, 0, -4); // remove the TAB characters from excel string "\t" $excelstr = $excelstr.'\n."'; // add line break at end of excel string "\n" 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); # 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"); do { $excelstr ; } while ($row_members = mysql_fetch_assoc($members)); ?> So I wanted the $excelstr variable to be a chunk of PHP code. It might look like <?php $row_members['FirstName']."\t".$row_members['LastName']."\t".$row_members['Company']."\t" ?> But I won't know the exact variables until I get the data out of the database. So I was looking for a way to convert the $excelstr variable. <?php do { echo $row_members['MemberID']."\t".$row_members['FirstName']."\t".$row_members['LastName']."\t".$row_members['Company']."\t".$row_members['Address']."\t".$row_members['City']."\t".$row_members['State']."\t".$row_members['PostalCode']."\t".$row_members['EmailAddress']."\t".$row_members['PhoneNumber']."\n"; } while ($row_members = mysql_fetch_assoc($members)); ?> It works OK. But I want to be able to have this last bit of data be the same as $excelstr instead of hard coding. Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726049 Share on other sites More sharing options...
mancorn Posted December 30, 2008 Author Share Posted December 30, 2008 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726585 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 uhh yeah.. I gave it to you.. $value = "FirstName"; $row_members['FirstName'] = "Sam"; $excel_string = $row_members[$value]; echo $excel_string; // dipslays as $row_members['FirstName'] echo "<br />"; echo $row_members['FirstName']; // displays as Sam Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726648 Share on other sites More sharing options...
mancorn Posted December 31, 2008 Author Share Posted December 31, 2008 I finally figured it out and used the following code to output the data. <?php // find all the fields we want to select from database $field = $_POST['all_data']; // get the data from dp_mailing.php "ALL" form foreach ($field as $value) { $query_string.= $value.",";// build a string to insert into SELECT statement } $str = substr($query_string, 0, -1); // remove the last comma from query_string 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); # 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"); //Populate Excel document with data do { foreach ($row_members as $value) { echo $value."\t" ; } echo "\n"; } while ($row_members = mysql_fetch_assoc($members)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726801 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.