I have this array:
$crops[] = array(
'Type' => $type_name,
'Method' => $method_name,
'Crop' => $crop_name,
'SowDirect' => $direct_sow_date,
'SowTransplant' => $transplant_sow_date,
'Transplant' => $transplant_date,
'BeginHarvest' => $begin_harvest_date,
'EndHarvest' => $end_harvest_date
);
What I want is to add the key/value pairs one by one within some logic code:
$crops[] = array();
if($var1 == something) {
$crops['Type'] = $value1;
}
if($var2 == something) {
$crops['Method'] = $value2;
}else{
$crops['Crop'] = $value3;
}
Using the first way, I get this, which is what I want: http://gardencalc.drivingpeace.com/grid_data.php
Using the second way, I get this, which is NOT what I want: http://gardencalc.drivingpeace.com/grid_data1.php
How do I make example 2 work the same as example 1?
Update 4/18/14
The answer below did the trick. The main problem I was having (and the one I hoped your suggestion would fix) is that I'm manipulating a bunch of dates within my logic decisions, and I'm using date_add() & date_sub().
If I just print the dates within the code, it works fine and I get the changes I want.
However, if I manipulate dates, add them to the array as in your method, then print the array via JSON, none of the dates are changing.
Here's your modified array code where the dates still aren't changing:
<?php
$loc_id = 174;
$prob_id = 10;
#Include the connect.php file
include('connect.php');
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die('Could not connect: ' . mysql_error());
//select database
mysql_select_db($database, $connect);
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
print "can't find $database";
}
$tempSQL = "SELECT * FROM prob_28 WHERE prob_id=$loc_id";
$temp = mysql_query($tempSQL) or die(mysql_error());
while($row = mysql_fetch_array( $temp )) {
$prob1_90 = $row['prob1_90'];
$prob1_50 = $row['prob1_50'];
$prob1_10 = $row['prob1_10'];
$prob2_10 = $row['prob2_10'];
$prob2_50 = $row['prob2_50'];
$prob2_90 = $row['prob2_90'];
}
//build the dates
if ($prob_id == "10"){
$first_frost_date = date_create(date("Y").'-'.substr($prob1_10,0,2).'-'.substr($prob1_10,2,2));
$last_frost_date = date_create(date("Y").'-'.substr($prob2_10,0,2).'-'.substr($prob2_10,2,2));
}elseif ($prob_id == "50"){
$first_frost_date = date_create(date("Y").'-'.substr($prob1_50,0,2).'-'.substr($prob1_50,2,2));
$last_frost_date = date_create(date("Y").'-'.substr($prob2_50,0,2).'-'.substr($prob2_50,2,2));
}else{
$first_frost_date = date_create(date("Y").'-'.substr($prob1_90,0,2).'-'.substr($prob1_90,2,2));
$last_frost_date = date_create(date("Y").'-'.substr($prob2_90,0,2).'-'.substr($prob2_90,2,2));
}
// get data and store in a json array
$query = "SELECT * FROM data ORDER BY data_id";
$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$curcrop = array();
$data_id = $row['data_id'];
$days_to_harvest = $row['days_2_harvest'];
$resolution = $row['resolution'];
//Get type name
$type_id = $row['type_id'];
$type = mysql_query("SELECT * FROM type WHERE type_id=$type_id") or die(mysql_error());
while($typerow = mysql_fetch_array( $type )) {
$curcrop['Type'] = $typerow['type_name'];
}
//Get planting method name
$method_id = $row['method_id'];
$method = mysql_query("SELECT * FROM method WHERE method_id=$method_id") or die(mysql_error());
while($methodrow = mysql_fetch_array( $method )) {
$curcrop['Method'] = $methodrow['method_name'];
}
//Get crop name
$crop_id = $row['crop_id'];
$crop = mysql_query("SELECT * FROM crop WHERE crop_id=$crop_id") or die(mysql_error());
while($croprow = mysql_fetch_array( $crop )) {
$curcrop['Crop'] = $croprow['crop_name'];
}
//calculate the direct sow date
//Only build real date if the planting method is direct sow
if ($method_id == 1){
//check if it's a fall or spring crop type
//spring date
if ($type_id == 1){
$direct_sow_date = $first_frost_date;
$date_int = $row['wks_b4_frost_2_sow'] * 7;
date_sub($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
//fall date
}else{
$direct_sow_date = $last_frost_date;
$date_int = $row['wks_b4_first_frost'] * 7;
date_sub($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
}
}else{
$direct_sow_date = null;
}
$curcrop['SowDirect'] = $direct_sow_date;
//calculate transplant date
if ($method_id == 2){
//check if it's a fall or spring crop type
//spring date
if ($type_id == 1){
$transplant_date = $first_frost_date;
$date_int = $row['wks_b4_last_frost_2_trans'] * 7;
date_sub($transplant_date,date_interval_create_from_date_string("$date_int days"));
//fall date
}else{
$transplant_date = $last_frost_date;
$date_int = $row['wks_b4_first_frost'] * 7;
date_sub($transplant_date,date_interval_create_from_date_string("$date_int days"));
}
}else{
$transplant_date = null;
}
//transplant sow date
if ($transplant_date != null){
$date_int_trans = $row['wks_2_grow_trans'] * 7;
date_sub($transplant_date,date_interval_create_from_date_string("$date_int_trans days"));
$curcrop['SowTransplant'] = $transplant_date;
date_add($transplant_date,date_interval_create_from_date_string("$date_int_trans days"));
}else{
$curcrop['SowTransplant'] = null;
}
$curcrop['Transplant'] = $transplant_date;
//begin harvest date
if ($method_id == 1 && $direct_sow_date != null){
date_add($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['BeginHarvest'] = $direct_sow_date;
date_sub($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
}
if ($method_id == 2 && $transplant_date != null){
date_add($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($transplant_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['BeginHarvest'] = $transplant_date;
date_sub($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($transplant_date,date_interval_create_from_date_string("$resolution days"));
}
//end harvest date
if ($method_id == 1 && $direct_sow_date != null){
date_add($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['EndHarvest'] = $direct_sow_date;
date_sub($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
date_add($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
}
if ($method_id == 2 && $transplant_date != null){
date_add($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_add($transplant_date,date_interval_create_from_date_string("$resolution days"));
$curcrop['EndHarvest'] = $transplant_date;
date_sub($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
date_sub($transplant_date,date_interval_create_from_date_string("$resolution days"));
date_add($transplant_date,date_interval_create_from_date_string("$date_int days"));
}
$crops[] = $curcrop;
}
echo json_encode($crops);
?>
In the first example, you're appending an array of information to the $crops
array, but in the second, you append an empty array to the $crops
array, and then set a few keys in that same $crops
array.
What you need to do is build your "current crop" array with the second method and then append that array to the $crops
list, as before:
$curcrop = array();
if($var1 == something) {
$curcrop['Type'] = $value1;
}
if($var2 == something) {
$curcrop['Method'] = $value2;
}else{
$curcrop['Crop'] = $value3;
}
$crops[] = $curcrop;