<!--?php require("isdk.php"); $myApp = new iSDK; $myApp--->cfgCon("connectionName"); // Get Contact ID from POST variable $conId = $_POST['Id']; // Set Video Last View Date to Today (_LastViewDate ) $infusionDate = date("YmdTH:i:s"); $data = array('_LastViewDate' => $infusionDate); $update = $myApp->dsUpdate("Contact", $conId, $data); // Increment Video Watched Counter By One (_VideoWatchedCounter ) // Load current value then add new value $returnFields = array('_VideoWatchedCounter'); $conDat = $myApp->dsLoad("Contact", $conId, $returnFields); $counter = $conDat['_VideoWatchedCounter']; //echo "Counter is ".$counter." "; $counter++; //echo "Now counter is ".$counter." "; $data = array('_VideoWatchedCounter' => $counter); $update = $myApp->dsUpdate("Contact", $conId, $data); //echo $update; ?>
Okay so lets break it down now. You know how to connect to Infusionsoft, so lets find out what we have to do to understand and get this script running.
$conId = $_POST['Id'];
Firstly we want to figure out what’s happening here to start with. When I read line 9, I’m seeing a variable (a container to store data) $conId, which has been created to hold the customer ID [‘Id’] number. The customer ID number is sent to the server by the $_POST variable. Now the $_POST variable is a global variable which has been pre defined by some of the geeky guys who originally wrote php. It. Always contains variables from a previous page , normally a form or something that contains user input and a button has been clicked.
So now that we have the data past through and then wrapped up in the container $conId we need to do something useful with it.
And that useful something will be to create a date of when the specific user watched a video … and then we’ll put that date into a custom field you’ve created in Infusionsoft.
Lets do this now.
$infusionDate = date("YmdTH:i:s"); $data = array('_LastViewDate' => $infusionDate); $update = $myApp->dsUpdate("Contact", $conId, $data);
We first have to take care of the date side of things, so we go ahead and make up a variable called $infusionDate and inside that variable we want to say “ Everytime you come across $infusionDate, we’d like it to create today’s date in this specific format. (Year, month, day)
Now the next thing we have to do is tell it which field we want to insert the date in. In this case we have created a custom field called LastViewDate. Now remember when you’re inserting data or specifying a custom field in a script, you have to precede it with an under score, like so: _LastViewDate.
So we create a variable called $data and assign it or in other words store the data inside of the two variables $infusionDate and LastViewDate to the container $data.
Did you get me on that one?
I’ll say it in other words if you didn’t. The $data variable will hold the information stored inside of $infusionDate and LastViewDate.
We now have to execute the three lots of data we’ve stored in our containers and put them in their rightful places.
$update = $myApp->dsUpdate("Contact", $conId, $data);
So looking at line 14, this is where it all happens. We want to put all of the data we have collected and created so far (The customer ID, the date and the custom field we want to put the date in.) and then house them inside of a variable called $update and when the parser reads this, it will execute the function like so:
1. Open the connection
2. Use the correct infusionsoft method – dsUpdate
3. Find the Contact table
4. Find this specific contact
5. Find the field inside of the variable $data (LastViewDate) and then find today’s date ($infusionDate ) and insert it in this format (y,m,d)
Also, while you’re in there, can you execute the following …
Firstly, find out how many times this video has been watched by this contact and secondly increment it by one, because it looks like they’ve just watched it again.
$returnFields = array('_VideoWatchedCounter'); $conDat = $myApp->dsLoad("Contact", $conId, $returnFields); $counter = $conDat['_VideoWatchedCounter']; //echo "Counter is ".$counter." "; $counter++; //echo "Now counter is ".$counter." "; $data = array('_VideoWatchedCounter' => $counter); $update = $myApp->dsUpdate("Contact", $conId, $data); //echo $update; ?>
So what we have to do here is a couple of things.
$returnFields = array('_VideoWatchedCounter'); $conDat = $myApp->dsLoad("Contact", $conId, $returnFields);
Firstly name a variable $returnFields and put the information from the custom field VideoWatchedCounter in it.
Next, we need to assign another variable $conDat to go to the Table named Contact and fined the customer fields who watched the video and bring back the data in the custom field VideoWatchedCounter.
$counter = $conDat['_VideoWatchedCounter']; //echo "Counter is ".$counter." "; $counter++; //echo "Now counter is ".$counter." ";
So now that we’ve returned the data in the custom field VideoWatchedCounter inside of the $conDat variable, line 22 says we now need to assign it to a new variable called $counter and then increment it by one, which is the ++ signs after the variable on line 25.
You’ll notice there is an echo sign behind the forward slashes, this is because we always use this to test the script and have it read back to us.
$data = array('_VideoWatchedCounter' => $counter); $update = $myApp->dsUpdate("Contact", $conId, $data); //echo $update; ?>
So now that we’ve the updated the count number for videos … and we did this by returning the existing number in the custom field (VideoWatchedCounter) and incremented it by one. We have to now put it back, so we assign this new data to the already created variable called $data. Note: Variables can be re-assigned within the same script. Line 28 says get the new number which has now been incremented by one and assign it to the custom field VideoWatchedCounter.
Now it hasn’t been executed yet and this is the last step in the script. Line 29 says makeup a variable and call it $update use the method dsUpdate, then look for the table Contact and find this specific contact, which is stored inside $conId, once you’ve done that find the custom field VideoWatchedCounter and put the data that is stored inside of the container $counter in there.
Again you’ll see the echo statement in green. You should always be testing. Just cancel out the two forward slashes, upload your script to the server and then type in the url to test if the script works. You should see the new figure telling you how many times the video has been watched.