JKG Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 Instead of printing the string within your function just return it. Quote Link to comment Share on other sites More sharing options...
JKG Posted July 28, 2011 Author Share Posted July 28, 2011 fantastic! thank you so much. Quote Link to comment Share on other sites More sharing options...
JKG Posted July 28, 2011 Author Share Posted July 28, 2011 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'); } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 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? Quote Link to comment Share on other sites More sharing options...
JKG Posted July 28, 2011 Author Share Posted July 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 I still don't think it's necessary to have the entire Javascript stored in a database. You could just as easily store just the data. Quote Link to comment Share on other sites More sharing options...
JKG Posted July 28, 2011 Author Share Posted July 28, 2011 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... Quote Link to comment Share on other sites More sharing options...
JKG Posted July 28, 2011 Author Share Posted July 28, 2011 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!! Quote Link to comment Share on other sites More sharing options...
JKG Posted July 28, 2011 Author Share Posted July 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2011 Share Posted July 28, 2011 If you want them in an array just put them in an array in the first place. Quote Link to comment Share on other sites More sharing options...
JKG Posted July 28, 2011 Author Share Posted July 28, 2011 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.