I am stuck, I can't find any reference to this problem. It shouldn't be happening. The global variable $assignmentInfo is passed into the function, manipulated, and returned as a different variable.
BUT, the local manipulations within the function are propagated to the global variable, so that the next time the function gets called, $assignmentInfo has been changed. I want to pass in the same, unchanging version of $assignmentInfo each time I call the function.
Any suggestions?
function studentScores($submissionInfo, $assignmentInfo){
//********************************** Add submission data to assignment data ****************************************************************************
foreach($assignmentInfo as $assignmentGroup){
foreach($assignmentGroup->assignments as $assignment){
foreach($submissionInfo as $submission){
if(isset($assignment->id) and isset($submission->assignment_id) and $assignment->id == $submission->assignment_id){
if (isset($submission->score)){$assignment->score = $submission->score;}
if (isset($submission->submitted_at)){$assignment->submitted_at = $submission->submitted_at;}
if (isset($submission->workflow_state)){$assignment->workflow_state = $submission->workflow_state;}
break;
}
}
}
}
$studentScores = $assignmentInfo;
return $studentScores;
}
$studentScores = studentScores($submissionInfo->submissions, $assignmentInfo);
Related
I'd like to store a datetime variable into different tables by using two functions. I use constraint in CI but still have no luck.
This is the constraint:
$date_now = date("ymdhis");
define('TODAY_DATE',$date_now);
These are the functions:
public function save_activity_m(){
foreach($details as $rows){
$stock_in = $rows['product']."_".TODAY_DATE;
$data['STOCK_IN'] = ($rows['product'] == "") ? NULL : $stock_in;
$this->MProduct->ins_product_m($data);
}
echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>";
}
public function save_notifikasi(){
$lampiran = $this->input->post('lamp');
$data['note_date'] = $lampiran."_".TODAY_DATE;
$data['note'] = $this->input->post('isi');
$this->MProduct->ins_notif($data);
echo "<script type='text/javascript'>alert('You have a notification');</script>";
}
How to make the datetime is the same for $data['STOCK_IN'] and $data['note_date']?
Since the web is stateless, no data in a PHP variable will be held from one page (or load) to another; essentially you're booting the application from scratch each time.
The only way around this is to use some sort of semi-persistent storage such as a cookie or session variable (or persistent storage like the database) - setting a constant, e.g. define('TODAY_DATE',$date_now); will only make that data constant for the current execution of the script(s).
This is a basic example using session storage ($_SESSION):
<?php
// crank up the session
// you may well have one running already,
// in which case ignore this
session_start();
// store the execution time for this script
// as a session variable if it's NOT already set
// i.e. don't overwrite it
if(empty($_SESSION['time_now'])) $_SESSION['time_now'] = date("ymdhis");
public function save_activity_m() {
foreach($details as $rows) {
$stock_in = $rows['product'] . "_" . $_SESSION['time_now'];
$data['STOCK_IN'] = ($rows['product'] == "") ? NULL : $stock_in;
$this->MProduct->ins_product_m($data);
}
echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>";
}
/**
* Assuming this is the last thing you want to
* do with 'time_now' you should unset it here
*/
public function save_notifikasi() {
$lampiran = $this->input->post('lamp');
$data['note_date'] = $lampiran . "_" . $_SESSION['time_now'];
$data['note'] = $this->input->post('isi');
$this->MProduct->ins_notif($data);
// since we're done with the 'time_now' session
// variable we need to unset it...
unset($_SESSION['time_now']);
echo "<script type='text/javascript'>alert('You have a notification');</script>";
}
// just to be on the safe side unset the 'time_now' session var
// if it's older than 1 minute - otherwise future calls to this
// script, by the same user, during the same session will use
// the stored value from $_SESSION['time_now']
if(isset($_SESSION['time_now'])) {
$sessionTime = DateTime::createFromFormat('ymdhis', $_SESSION['time_now']);
$oneMinuteAgoTime = new DateTime('-1 minute');
if($sessionTime < $oneMinuteAgoTime) {
unset($_SESSION['time_now']);
}
}
The caveat is that because you've stored the time in a session variable, unless you update or unset it, it will always be there (for the current session) - so if the user runs the script again it'll just use the stored time from the session.
I've put in a couple of unset() calls to try and work around this.
See PHP: define. It's a constant and it should have the same value if the two functions executed in the same time the script is running.
I am subscribing to data from a MQTT broker with phpMQTT. I have successfully set up a pub / sub routine based on their basic implementation. I can echo the information just fine inside the procmsg() function.
However, I need to take the data I receive and use it for running a few database operations and such. I can't seem to get access to the topic or msg received outside of the procmsg() function. Using return as below seems to yield nothing.
<?php
function procmsg($topic, $msg){
$value = $msg * 10;
return $value;
}
echo procmsg($topic, $msg);
echo $value;
?>
Obviously I am doing something wrong - but how do I get at the values so I can use them outside the procmsg()? Thanks a lot.
I dont know about that lib, but in that code
https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php ,
its possible see how works.
in :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"procmsg");
you are telling it that topic "edafdff398fb22847a2f98a15ca3186e/#" will have Quality of Service (qos) = 0, and an "event" called 'procmsg'.
That's why you later wrote this
function procmsg($topic,$msg){ ... }
so in the while($mqtt->proc()) this function will check everytime if has a new message (line 332 calls a message function and then that make a call to procmsg of Source Code)
thats are the reason why you cannot call in your code to procmsg
in other words maybe inside the procmsg you can call the functions to process message ej :
function procmsg($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Note that you can change the name of the function simply ej :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"onMessage");
and then :
function onMessage($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Sorry for my english, hope this help !
I was using this function to clear stored results after calling a procedure etc.
function clearStoredResults($mysqli_link){
#------------------------------------------
while($mysqli_link->next_result()){
if($l_result = $mysqli_link->store_result()){
$l_result->free();
}
}
}
But I keep getting the following message/error
mysqli::next_result(): There is no next result set. Please, call
mysqli_more_results()/mysqli::more_results() to check whether to call
this function/method in
If I change next_result() to more_results() the page keeps loading for a while and I get a time out error:
Maximum execution time of 30 seconds exceeded
any ideas on how to fix this?
You need to use both methods:
function clearStoredResults($mysqli_link) {
#------------------------------------------
while($mysqli_link->more_results()) {
$mysqli_link->next_result();
if($l_result = $mysqli_link->store_result()) {
$l_result->free();
}
}
}
Use more_results to control the loop, and next_result to actually move the cursor.
**
After the first answer: 5 year old project that needs to be updated
My problem is that at the moment one single product-download page has at least 14 functions and over 1200 lines of code. I basically just want to make it more simple to make changes and decrease the file size and code e.g. add all vars at the top of the page and not having to search thru the entire code to find where they have to be added and cut out as many dupicate functions as possible ...
**
Some of the vars (all with extra functions) for the same object:
$p1_files = "'p-1.zip', 'p-2.zip', 'p-3.zip', 'p-4.zip', 'p-5.zip'";
$p2_files = "'p2-1.zip', 'p2-2.zip', 'p2-3.zip', 'p2-4.zip', 'p2-5.zip'";
$p3_files = "'p3-1.zip', 'p3-download-2.zip', 'p3-download-3.zip', 'p3-4.zip', 'p3-5.zip'";
the "very shortend" function:
function p_files_function(){
$p1_files = "'p-1.zip', 'p-2.zip', 'p-3.zip', 'p-4.zip', 'p-5.zip'";
return $p1_files;
}
the "p_files_function()" returns
'p-1.zip', 'p-2.zip', 'p-3.zip', 'p-4.zip', 'p-5.zip'
which is perfect and I can use to create the listings but it only works with 1 var.
Now, what I would like to do is to add all vars to one function and only return the needed one
Something like
// List all vars at top of page
$p1_files = "'p-1.zip', 'p-2.zip', 'p-3.zip', 'p-4.zip', 'p-5.zip'";
$p2_files = "'p2-1.zip', 'p2-2.zip', 'p2-3.zip', 'p2-4.zip', 'p2-5.zip'";
$p3_files = "'p3-1.zip', 'p3-download-2.zip', 'p3-download-3.zip', 'p3-4.zip', 'p3-5.zip'";
// one function to get the needed vars
function p_files_function($args){
if Value1 {
$needed_files = $p1_files
}
if Value2 {
$needed_files = $p2_files
}
if Value3 {
$needed_files = $p3_files
}
return $needed_files;
}
// easy way to get the needed vars
p_files_function(Value2) //should retuns $p2_files
any ideas?
This is very shortend there are also images, documents and so on, I managed to cut everything else down to minimum but with this I am lost, all I need is a starting point.
Thanks
Personally I think adding the suffix _function to your function name is a little redundant. That said:
function p_files($type) {
switch ($type) {
case 'p1':
return "'p-1.zip', 'p-2.zip', 'p-3.zip', 'p-4.zip', 'p-5.zip'";
case 'p2':
return "'p2-1.zip', 'p2-2.zip', 'p2-3.zip', 'p2-4.zip', 'p2-5.zip'";
case 'p3':
return "'p3-1.zip', 'p3-download-2.zip', 'p3-download-3.zip', 'p3-4.zip', 'p3-5.zip'";
default:
return ''; // or trigger error or whatever
}
}
$needed_files = p_files('p1'); // etc
Though after reading explanations about setting cookie and not working for first time i find it difficult to resolve the below problem as am new to php and cookies.
I have a webpage with for (e.g) cp.php, login.php, header.php, maindata.php , bottom.php. Whenever i login to the webpage cp.php will be processed from there 1.header.php will be called first 2.maindata.php will be called and 3.bottom.php will be called.
So am setting my cookie at maindata.php and the code is like,
<?php
$cid = $_GET["id"];
$XmlPath = $_GET["path"];
$numpath = $_GET["numpath"];
$finepath =$_GET["finepath"];
$Tech =$_GET["tech"];
$read_str="";
function read($Path)
{
$temp="";
if(file_exists($Path))
{
$library = new SimpleXMLElement($Path,null,true);
foreach($library->children("SAS") as $info){
foreach($info->children("SAS") as $attributes){
$nameVal = $attributes->Name."=".$attributes->Value;
$str_temp .=$nameVal."#";
}
}
}else
{
$str_temp ="NA";
}
return $str_temp;
}
$arrpath =explode(",",$XmlPath);
/*Reading and storing arrpath[0] has the path of xml to be parsed*/
$strG=read($arrpath[0]);
$strC=read($arrpath[1]);
$strB =read($arrpath[2]);
setcookie($cid.'strE',$strG);
setcookie($cid.'comstr',$strC);
setcookie($cid.'basstr',$strB);
(....)
in the same file am reading the cookie using the below code,
$read_str =$_COOKIE[$cid.'strE'].$_COOKIE[$cid.'comstr'].$_COOKIE[$cid.'basstr'];
after this process is done bottom.php will be called and for the first time loading is completed.As i said for the first time am not getting any value in $read_str, but if i refresh the page and do all the process again i am getting the value.
As SETCOOKIE will return TRUE incase of successfully setting cookie i tried putting it in an if-loop and it returned false even for the first time.
kindly assist me in finding where the problem exists!
Make use of isset to check if a cookie exists and then try setting one.
Something like this.
if(!isset($_COOKIE['yourcookie'])) {
setcookie('yourcookie', 'Some data !');
$_COOKIE['yourcookie'] = 'Some data !';
}
echo $_COOKIE['yourcookie'];
I arrived here looking for an answer as well. Here's the deal.
When you set a cookie it can only be accessed on the next page load, that is why you can't access it after you set it. If you really need to work with the cookie data right away, you could set the value directly in global cookie such as:
$_COOKIE['my_cookie'] = 'i am a cookie';
Use setcookie()just the same so you can set expiration, domain, etc..