简体   繁体   中英

Saving Variable for later use

<?php
    require_once("class/goldendata.php");

    $res = json_encode($GCaller->select("calls", "*"));
    $oldVal = $res;
    $res;

    echo $res . " - " . $oldVal;

    /* What I want to happen */
    if ($res != $oldVal) {
         //Code Here
    }
?>

So I have current PHP code. Problem is I don't get the old value.

WHAT DO YOU THINK IS HAPPENING?
Well it's fairly obvious that when this is called $oldData is being overwritten by the new value and so it will never be different from each other.


WHAT DO YOU WANT TO HAPPEN?
I want to store the old value returned by $GCaller->select("calls", "*") . But I can't think of any way of doing this. Even If I do it with sessions I would just be overwriting the $_SESSION just like with the variable.

Extra Information
The script is executed in the javascript every 5 second.

setInterval(function(){
                $.ajax({
                    url: "assets/lifespan.php",
                    success: function(data) {
                        if (data) {
                            $("#pendingCall" + data.split(":")[1] + " button[data-uid='" + data.split(":")[0] + "']").remove();
                        }
                    }
                });

                /*$.ajax({
                    url: "assets/refresh.php",
                    dataType: 'json',
                    success: function(json) {
                       console.log(JSON.stringify(json));
                       $.each(json, function(idx, topic){
                         $("#nav").html('<a href="' + json[0] + '">' + topic.link_text + "</a>");
                       });
                    }
                });*/

                $.getJSON("assets/refresh.php", function(data){
                    for(var i = 0; i < 30; i++) {
                        $("#pendingCall" + i).empty();
                    }

                    $.each(data, function (index, info) {
                        $("#pendingCall" + info.callNum).append("<button onclick=\"loadPlayer('" + info.playerTag+ "', "+ info.callNum +")\"class='btn btn-primary'>" + info.username + "</button>");
                    });
                });
            }, 5000);

If I can also do this in here. Compare new value with old value and then execute a certain code that will work as well. :)

SOLUTIONS YOU THOUGHT OF?
Put data in a file where I can read the 'PREVIOUS LINE' and that will be my old data. But I would prefer something more efficient.

Confused?
Yes, I am sure most of you are confused because I can't really explain too well. But I will try and clear up as much as possible ff you comment down below what exactly you are confused about.

Even If I do it with sessions I would just be overwriting the $_SESSION just like with the variable

You have to do it the right way around of course – compare the new value with the old one from the session, before you put the new one into the session.

Something like this:

<?php
session_start();

if(isset($_SESSION['foo'])) {
  $old_value = $_SESSION['foo'];
}
else {
  $old_value = null;
}

$new_value = …

if($old_value != $new_value) {
  do_something();
}

$_SESSION['foo'] = $new_value;

Of course you don't need the $old_value variable, you can as well use the session value – if set – in the comparison directly,

if(isset($_SESSION['foo']) && $_SESSION['foo'] != $new_value) { …

You can use a local storage, with web applications can store data locally within the user's browser.

Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.

// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");

For more about it, please check this page: http://www.w3schools.com/html/html5_webstorage.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM