Jump to content

Recommended Posts

See code below.

 

I'm attempting to take the field names passed into the script and use them as variable names and assign a variable value to them on the fly.  If I can get this to work I can eliminate fetching every field from a MySQL table and only fetch the ones I want.  $field_data stores the variables and their associated values as a string so when I use it in the echo statement at the bottom of the script, I get the string and not what the variable should be doing, which is fetching the field values.

 

Any help would be very much appreciated.  I haven't been able to identify any string function that would help me out. 

 

Thanks!

 

<?php

$begin_date = $_GET['begin_date'];

$end_date = $_GET['end_date'];

$sites_list = $_GET['sites'];

$fields_list = $_GET['fields'];

$sites = explode(",", $sites_list);
$fields = explode(",", $fields_list);

$field_clause = "".$fields[0]."";
$field_data = "$".$fields[0]." = \$row['".$fields[0]."'].','.";
for($i = 1; $i < count($fields); $i++) {
$field_clause = $field_clause . ",".$fields[$i]."";
$field_data = $field_data . "$".$fields[$i]." = \$row['".$fields[$i]."']";
}

//print $field_data;

$site_where_clause = " WHERE (site_id = $sites[0]";
for($i = 1; $i < count($sites); $i++) {
$site_where_clause = $site_where_clause . " OR site_id = ".$sites[$i]."";
}

$conn = mysql_connect('localhost','root','13cmws13') or trigger_error("SQL", E_USER_ERROR);

mysql_select_db('volunteer_monitoring', $conn);

$selectQuery = "SELECT id,lab_sample_id,site_id,collection_date,collection_time,$field_clause 
FROM data_mi
$site_where_clause)
AND (collection_date >= '$begin_date'
AND collection_date <= '$end_date')";

$result  = mysql_query($selectQuery,$conn);

if ($result  != 0) {
header( "Content-Type: application/save-as" );
header ('Content-Disposition: attachment; filename="data_mi.csv"'); 

echo "id,lab_sample_id,site_id,collection_date,collection_time,".$field_clause."\n";
 $num_results = mysql_num_rows($result );
  for ($i=0;$i<$num_results;$i++) {
	$row = mysql_fetch_array($result );
//		$id = $row['id'];
//		$lab_sample_id = $row['lab_sample_id'];
//		$site_id = $row['site_id'];
//		$collection_date = $row['collection_date'];
//		$collection_time = $row['collection_time'];
//		$conductivity_pS_cm = $row['conductivity_pS_cm'];
//		$tds_mg_per_l = $row['tds_mg_per_l'];
//		$DO_mg_per_l = $row['DO_mg_per_l'];
//		$DO_percentage = $row['DO_percentage'];
//		$temp_do = $row['temp_do'];
//		$turbidity_ntu = $row['turbidity_ntu'];
//		$nitrate = $row['nitrate'];
//		$nitrite = $row['nitrite'];
//		$ammonia = $row['ammonia'];
//		$nitrate_nitrite = $row['nitrate_nitrite'];
//		$nitrate_nitrite_ammonia = $row['nitrate_nitrite_ammonia'];
//		$salinity_ppt = $row['salinity_ppt'];
//		$pH = $row['pH'];
//		$e_coli = $row['e_coli'];
//		$total_coliform = $row['total_coliform'];
//		$chlorophyll = $row['chlorophyll'];
//		$chloroDIVpheo = $row['chloroDIVpheo'];
//		$alkalinity = $row['alkalinity'];
//		$color = $row['color'];
//		echo "$id,$lab_sample_id,$site_id,$collection_date,$collection_time,$conductivity_pS_cm,$tds_mg_per_l,$DO_mg_per_l,$DO_percentage,$temp_do,$turbidity_ntu,$nitrate,$nitrite,$ammonia,$nitrate_nitrite,$nitrate_nitrite_ammonia,$salinity_ppt,$pH,$e_coli,$total_coliform/,$chlorophyll,$chloroDIVpheo,$alkalinity,$color\n";
	//echo $field_clause."\n";
	//echo "$id,$lab_sample_id,$site_id,$collection_date,$collection_time,".$field_data."\n";

	echo $id = $row['id'].",".$lab_sample_id = $row['lab_sample_id'].",".$site_id = $row['site_id'].",".$collection_date = $row['collection_date'].",".$collection_time = $row['collection_time'].",".$field_data."\n";

  }
}
mysql_close($conn);
?>

I reviewed your suggestion and looked at some online examples.  All of the examples start by storing a string but my first $field_data variable is defined by string parts and existing variable value parts (i.e $field_data = "$".$fields[0]." = \$row['".$fields[0]."'].','.";)).  I then append this to the remaining field name from within the for loop which is also part string and part variable value.  The entire value stored in this variable seems to be a string.

 

 

If what you are suggesting is the solution to my problem, today I am not able to understand how it will help me.

OK I'll try this again with more explanation in the specific lines of code that have me stumped.

 

See this:

$field_data = "$".$fields[0]." = \$row['".$fields[0]."']";
for($i = 1; $i < count($fields); $i++) {
$field_data = $field_data . "$".$fields[$i]." = \$row['".$fields[$i]."']";
}

 

The code above outputs this as an example: "$tds_mg_per_l = $row['tds_mg_per_l'],$DO_mg_per_l = $row['DO_mg_per_l']"

 

I would like to use that output in an echo statement as such:

echo $id = $row['id'].",".$lab_sample_id = $row['lab_sample_id'].",".$site_id = $row['site_id'].",".$collection_date = $row['collection_date'].",".$collection_time = $row['collection_time'].",".$field_data."\n";

 

This doesn't work.  Instead I just get the string(i.e.$tds_mg_per_l = $row['tds_mg_per_l'],$DO_mg_per_l = $row['DO_mg_per_l']), not the data which is contained in the database fields I am calling.

 

Any help would be very much appreciated.  Please, if i have not clearly stated my problem or provided enough information, please ask me for the information that you need to help me.

If you're doing what I think you're doing, you just need to look at where you're escaping dollar signs, and jumping in and out of the quoted string.

 

This for example;

 

<?php
echo '$id = $row[\'id\'], $lab_sample_id = $row[\'lab_sample_id\'], $site_id = $row[\'site_id\'], $collection_date = $row[\'collection_date\'], $collection_time = $row[\'collection_time\'], $field_data'."\n";

 

will print

 

$id = $row['id'], $lab_sample_id = $row['lab_sample_id'], $site_id = $row['site_id'], $collection_date = $row['collection_date'], $collection_time = $row['collection_time'], $field_data

Lets try this...

 

Instead of using the code commented out below,

 

$result  = mysql_query($selectQuery,$conn);

if ($result  != 0) {
 $num_results = mysql_num_rows($result );
  for ($i=0;$i<$num_results;$i++) {
	$row = mysql_fetch_array($result );
               //$tds_mg_per_l = $row['tds_mg_per_l'];
               //$DO_mg_per_l = $row['DO_mg_per_l'];

 

I "create" these variables on the fly. I.e.

 

$field_data = "$".$fields[0]." = \$row['".$fields[0]."']";
for($i = 1; $i < count($fields); $i++) {
   $field_data = $field_data . "$".$fields[$i]." = \$row['".$fields[$i]."']";
}

 

The suggestion you provided turned everything into a string.  My problem is that the variables that I am creating on the fly are seen as string, not like the lines of code that are commented out above.

I'm sorry, I have no idea what you want to do, that makes no sense whatsoever to me.

 

<?php
$field_data = "$".$fields[0]." = \$row['".$fields[0]."']";
for($i = 1; $i < count($fields); $i++) {
   $field_data = $field_data . "$".$fields[$i]." = \$row['".$fields[$i]."']";
}

 

maybe you should explain what each step is 'meant' to be doing.

 

$field_data = "$".$fields[0]." = \$row['".$fields[0]."']";

$field_data is going to be a string, which is a mix of array data, and other characters.

 

In the for loop you're writting over $field_data everytime, firstly with the original occurance of $field_data, and then itself (again).

The end goal is to provide a way for users to download a subset of data from a database.

 

OK, I have a php application where users can select a site or sites and water quality parameters that they can display for the site(s) they choose.  This is being passed into the code below i.e. $sites_list and $fields_list.

 

$field_data is creating the variables which I am hoping can replace the variables after the third for loop (commented out in code) to keep the application more dynamic and to only give the user what they want in a CSV file.  This group of code (the first for loop) is giving me a correctly structured output.  For example, if a user selects tds_mg_per_l and DO_mg_per_l in the application, the code will output the following: $tds_mg_per_l = $row['tds_mg_per_l'],$DO_mg_per_l = $row['DO_mg_per_l'].

 

The idea is to then use this output in the echo statement following the third for loop.

 

<?php
$sites_list = $_GET['sites'];

$fields_list = $_GET['fields'];

$sites = explode(",", $sites_list);
$fields = explode(",", $fields_list);

$field_data = "$".$fields[0]." = \$row['".$fields[0]."']".",";
for($i = 1; $i < count($fields); $i++) {
$field_data = $field_data . "$".$fields[$i]." = \$row['".$fields[$i]."']";
}

$site_where_clause = " WHERE (site_id = $sites[0]";
for($i = 1; $i < count($sites); $i++) {
$site_where_clause = $site_where_clause . " OR site_id = ".$sites[$i]."";
}

$conn = mysql_connect('localhost','root','13cmws13') or trigger_error("SQL", E_USER_ERROR);

mysql_select_db('volunteer_monitoring', $conn);

$selectQuery = "SELECT id,lab_sample_id,site_id,collection_date,collection_time,$field_clause 
FROM data_mi
$site_where_clause)
AND (collection_date >= '$begin_date'
AND collection_date <= '$end_date')";

$result  = mysql_query($selectQuery,$conn);

if ($result  != 0) {
//header( "Content-Type: application/save-as" );
//header ('Content-Disposition: attachment; filename="data_mi.csv"'); 

//echo "id,lab_sample_id,site_id,collection_date,collection_time,".$field_clause."\n";
 $num_results = mysql_num_rows($result );
  for ($i=0;$i<$num_results;$i++) {
	$row = mysql_fetch_array($result );
//		$id = $row['id'];
//		$lab_sample_id = $row['lab_sample_id'];
//		$site_id = $row['site_id'];
//		$collection_date = $row['collection_date'];
//		$collection_time = $row['collection_time'];
//		$conductivity_pS_cm = $row['conductivity_pS_cm'];
//		$tds_mg_per_l = $row['tds_mg_per_l'];
//		$DO_mg_per_l = $row['DO_mg_per_l'];
//		$DO_percentage = $row['DO_percentage'];
//		$temp_do = $row['temp_do'];
//		$turbidity_ntu = $row['turbidity_ntu'];
//		$nitrate = $row['nitrate'];
//		$nitrite = $row['nitrite'];
//		$ammonia = $row['ammonia'];
//		$nitrate_nitrite = $row['nitrate_nitrite'];
//		$nitrate_nitrite_ammonia = $row['nitrate_nitrite_ammonia'];
//		$salinity_ppt = $row['salinity_ppt'];
//		$pH = $row['pH'];
//		$e_coli = $row['e_coli'];
//		$total_coliform = $row['total_coliform'];
//		$chlorophyll = $row['chlorophyll'];
//		$chloroDIVpheo = $row['chloroDIVpheo'];
//		$alkalinity = $row['alkalinity'];
//		$color = $row['color'];


	echo $id = $row['id'].",".$lab_sample_id = $row['lab_sample_id'].",".$site_id = $row['site_id'].",".$collection_date = $row['collection_date'].",".$collection_time = $row['collection_time'].",".$field_data"\n";

  }
}
mysql_close($conn);
?>

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.