Jump to content

how to insert a functions result into a database


JKG

Recommended Posts

hello,

 

thanks for taking the time to read this.

 

just wondering if you could help me with this evident gap in my knowledge!!

 

i want to insert the following functions results into the database, but when it comes to executing the function, it just prints the data onto the page, and not within the mysql statement.

 

<?php 
function make_js($date_raw){
if(isset($date_raw)){
	$i = 1;
	foreach($date_raw as $val){
		$date = $val . '';
		$iplus = $i++;
		$jsdate=date("Y,G,j", mktime(substr($date, 5,2)-1,1,0, substr($date, 5, 2), substr($date, -2), substr($date, 0, 4)));
		print 'var date'.$iplus.' = new Date('. $jsdate . ')
		date'.$iplus.'.canSelect = "true";
		date'.$iplus.'.selected = "false";
		date'.$iplus.'.type = "normal";
		';
	}

		print 'var myArray = new Array(';
		$i2 = 1;
		while($i2<=$iplus){
			print "date".$i2++;
				if($i2 <= $iplus){
					print ',';
  					}
		}	
print ');
ms_cal.addDates(myArray);';
}
}
function make_dates($data_array){
$data = array($data_array);
foreach($data as $d){
  foreach($d as $v){
    print $v."; ";
  }
}
}
if(isset($_POST['d'])){
mysql_query("UPDATE `users` SET 
            `unavailable_dates_js` = '".make_js($_POST['d'])."',
            `unavailable_dates_array` = '".make_dates($_POST['d'])."' WHERE `Id` = '".$_SESSION['user_id']."'") or die("SQL Error: " . mysql_error());
            echo('inserted');
}
?>

 

any help would be appreciated.

 

thank you, Joe.

 

EDIT: just so you know, im making some js code from some dates, and an array for another function. the functions in themselves arnt important, its the inserting their output that im interested in. thanks.

Link to comment
Share on other sites

actually....

 

this only returns the first bit of the string....

how can i make consecutive returns?

 

thanks again.

 

new code:

 

<?php 
function make_js($date_raw){
if(isset($date_raw)){
	$i = 1;
	foreach($date_raw as $val){
		$date = $val . '';
		$iplus = $i++;
		$jsdate=date("Y,G,j", mktime(substr($date, 5,2)-1,1,0, substr($date, 5, 2), substr($date, -2), substr($date, 0, 4)));
		return 'var date'.$iplus.' = new Date('. $jsdate . ')
		date'.$iplus.'.canSelect = "true";
		date'.$iplus.'.selected = "false";
		date'.$iplus.'.type = "normal";
		';
	}

		return 'var myArray = new Array(';
		$i2 = 1;
		while($i2<=$iplus){
			return "date".$i2++;
				if($i2 <= $iplus){
					return ',';
  					}
		}	
return ');
ms_cal.addDates(myArray);';
}
}
function make_dates($data_array){
$data = array($data_array);
foreach($data as $d){
  foreach($d as $v){
    return $v."; ";
  }
}
}
if(isset($_POST['d'])){
mysql_query("UPDATE `users` SET 
            `unavailable_dates_js` = '".make_js($_POST['d'])."',
            `unavailable_dates_array` = '".make_dates($_POST['d'])."' WHERE `Id` = '".$_SESSION['user_id']."'") or die("SQL Error: " . mysql_error());
            echo('inserted');
}
?>

Link to comment
Share on other sites

return exits the function where it is. You'll need to build a string (saving it into a variable) then return that once your have all the data.

 

Looking at your code, I have to ask.... why the hell are you storing JavaScript in your database?

Link to comment
Share on other sites

yeah im starting to see what you mean...

just pull the dates and pass them through the loop on the fly...

 

how would i make function make_dates($data_array) into a string as there is only one return anyway?

 

its only storing the first date...

Link to comment
Share on other sites

ok, so i found out how to do... kind of!

 

foreach($data as $d){
  foreach($d as $v){
    $dates_php .= $v."; ";
  }}

 

however, this throws up this notice, do you know why?

 

Notice: Undefined variable: dates_php in /root/path/unavailable-dates.php on line 35

 

thanks for you help!!

Link to comment
Share on other sites

i kind of figured that now too:

 

$data = array($_POST['d']);
foreach($data as $d){
$dates_php = "";
  foreach($d as $v){
    $dates_php .= $v."; ";
  }}

 

last question i promise!!

 

how can i grab all the dates (look something like this: 2011-07-06; 2011-07-13; 2011-07-20; 2011-07-21; 2011-07-28;)

and turn it into an array of dates?

i presume i would use str_replace, or explode() or something, can i just have a pointer please??

 

thanks so much.

 

 

Link to comment
Share on other sites

did this. so no js in db!! thanks thorpe...

<?php 
function make_js($date_raw){
if(isset($date_raw)){
	$datesarray = explode(";", $date_raw);
	$i = 1;
	foreach($datesarray as $val){
		$date = $val . '';
		$iplus = $i++;
		$jsdate=date("Y,G,j", mktime(substr($date, 5,2)-1,1,0, substr($date, 5, 2), substr($date, -2), substr($date, 0, 4)));
		echo 'var date'.$iplus.' = new Date('. $jsdate . ')
		date'.$iplus.'.canSelect = "true";
		date'.$iplus.'.selected = "false";
		date'.$iplus.'.type = "normal";
		';
	}

		echo 'var myArray = new Array(';
		$i2 = 1;
		while($i2<=$iplus){
			echo "date".$i2++;
				if($i2 <= $iplus){
					echo ',';
  					}
		}	
echo ');
ms_cal.addDates(myArray);';
}
}
if(isset($_POST['d'])){

$data = array($_POST['d']);
foreach($data as $d){
$dates_php = "";
  foreach($d as $v){
    $dates_php .= $v.";";
  }}
mysql_query("UPDATE `users` SET `unavailable_dates_array` = '{$dates_php}' WHERE `Id` = '".$_SESSION['user_id']."'") or die("SQL Error: " . mysql_error());
echo $dates_php;
}
?>

 

<script type="text/javascript">
/*<![CDATA[*/
var	ms_cal;      
window.onload = function () {
ms_cal  = new Epoch('epoch_multi','flat',document.getElementById('multi_container'),true);
<?php if(unavailable_dates_array ( $_SESSION['user_id'] ) != ''){make_js(unavailable_dates_array( $_SESSION['user_id'] ));}; ?>
};

/*]]>*/

</script>

Link to comment
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.