Jump to content

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.

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

the php builds javascript that tells a calendar which dates are available or not.

everytime a user adds another date, or takes one away it updates the javascript which adds or takes away an added date.

 

using the http://jkgo.co.uk/epoch calendar.

 

will have a go at the string in a sec.

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...

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!!

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.

 

 

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>

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.