Jump to content

Recommended Posts

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']".

Link to comment
https://forums.phpfreaks.com/topic/138845-solved-variables/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726027
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726033
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726045
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726049
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726648
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138845-solved-variables/#findComment-726801
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.