Agile toolkit : how to automatically reload grid - php

Using Agile toolkit to generate a grid, I'm trying to get an automatic reload of a grid but I can't figure how :/
I dug into the js() function and saw the first parameter is $when but, how to set up an interval?
Reload on event like button click is ok, but I need to set up reload every x sec.

OK here is complete tested example how you should do.
First I have to admit, that ATK4 had no setInterval and clearInterval functions added in univ() library, but already had setTimeout. I added setInterval and clearInterval and made pull request in Github (https://github.com/atk4/atk4/pull/173). I hope Romans will accept that, but if not, then you can always add these functions in your own JS library.
So here is the code:
$g = $this->add('Grid');
$this->js(true)->univ()->setInterval(
$g->js()->reload()->_enclose()
,3000);
Or you can even execute multiple independent functions like this:
$this->js(true)->univ()->setInterval(
$g->js(null,array(
$g->js()->reload(),
$g->js()->univ()->successMessage('Reloaded...')
))->_enclose()
,3000);
Key part here is ->_enclose() method which will convert your JS chain to anonymous JS function.

Javascript code :
setInterval("func()",1000);
This will call the func() function every 1000 ms

Related

Codeception acceptance check if js function is called

i'm trying to create a php test to check if a specific ajax is executed, but i can't find anything that can help me.
I'm fine even if i can check if the method, whom triggers the ajax request, is called (but i think is not possibile to mock js functions with codeception).
A little example to explain better:
let's assume i have my online page: http://example.com with the following js
var log = {
execute: function(){
// AJAX CALL HERE
}
};
log.execute();
With the test i would like to know if log.execute() is executed, so in codeception i can do something like:
$I = new AcceptanceTester($scenario);
// Opening page
$I->amOnUrl('http://example.com');
// Checking if log is invoked
$I->wantTo('Check if log is invoked');
// Something to understand if a method is executed
$I->???('log.execute()');
Otherwise, i'm fine if there is a method to take the browser network activity and then regex it (do not suggest proxy, don't wanna go there).
I'm open to any kind of solutions, even to use a different testing system (with php)
Edit: i forgot to say that i'm using selenium with chromeDrivers
Thanks for your time

get_option, intval, datatype

My scripts run if I do:
$config['max'] = 20;
It doesn't run if I use the configuration-value of my plugin like
$config['max'] = get_option('plugin_max');
Also not like
$config['max'] = intval(get_option('plugin_max'));
In every case, if I print $config['max'], it prints
20
I think there must be some stupid datatype-mismatch, but I can't figure out what it could be.
Solution
ok, I have to clear this up and maybe it will help someone later on. The problem was that get_option was undefined because it was called directly in an AJAX-request. I now routed the ajax-requests properly over WPs admin-ajax.php function so that WP-functions are available to the plugin even when called from AJAX.
Strangely, while debugging, the function was available, but this might have been while initialising the plugin, which is of course no AJAX-request but entirely server-side and so inside the scope of WP by default.
Thanks for your support!

How can I stream live data and present it in a line chart using PHP, cURL, ajax

There is a piece of hardware that records data. I can call an API to get the stream of data via cURL. It records a new row of data every seconds. I would like to stream this data and present it in a line chart over time. With PHP and maybe AJAX. I would like to see a moving chart in the browser refreshing the browser in every seconds or using Ajax. What kind of tools do I need? What is the best way of doing this?
Thank you, Everybody these are really useful things.
At the moment I'm struggling to get the data from the hardware.
I can reach the data via an interface which is written like this:
192.168.150.130:2345/realtime
Then I can see in the browser this:
DM_NumLogChans=5
DM_NumDataModes=1
DM_LogicalChan=1
DM_ChanType=SEQUENTIAL
DM_NumDims=2
DM_DataMode=1
DM_DataModeType=TIMHIS
DM_AxisLabel.Dim1=Time
DM_AxisLabel.Dim2=Value
DM_AxisUnits.Dim1=secs
DM_AxisUnits.Dim2=microstrain
DM_SampleRate=1.000000
DM_TimeBase=0.0
DM_ChanName=bridge_1
DM_UserMin=-583.220764
DM_UserMax=940.916199
DM_Start=
-439.779 -391.875 -680.114 1001.37 0
-442.068 -396.62 -680.945 1001.37 0
-443.571 -399.705 -680.639 1001.37 0
-445.598 -404.848 -684.662 1001.37 0
These are recorded data I can't get it from the URL. I would like to save it or live stream it somehow.
I suggest you to use Google Charts's Line plot. See this jsFiddle example of how to add data automatically. This is simple as using the data.addRow([ DATA_INDEX, VALUE_1, VALUE_2, ... ]);
This use jQuery for plot initialization and "click" event. You'll have to make a loop in order to retrieve data and add it to the plot.
Any of the various JS chart libraries out there should be able to present a line chart for you via loading data up through ajax calls. Here are a few:
http://www.fusioncharts.com
http://www.chartjs.org
http://www.jscharts.com
http://www.amcharts.com
... and there are plenty more. Just find one that works best for you.
To run the ajax call that refreshes the charts every x-seconds, use something like this:
Call jQuery Ajax Request Each X Minutes
Also, take a look at this answer on the amCharts forum:
http://www.amcharts.com/forum/viewtopic.php?id=12625

setInterval doesn't seem to re-execute php script

what i'm trying to do is get a variable to update every 5 seconds doing this:
setInterval(document.getElementById("listeners").innerHTML =
"<?php include('../includes/shoutcaststuff.php');
echo $dnas_data['CURRENTLISTENERS']; ?>",5000);
but what happens is the inner html is set but doesn't update every 5 seconds like it should.
my guess is that the php only executes once, but i have no idea if that's the case or not.
and i'm aware i should make a function to do the stuff inside setInterval... i'll clean up the code once i figure out how to make it work.
thanks in advance.
ok... ajax was 'the best' answer since no more than 2 people would be logged in at a time here so server requests isn't such a big deal.
here's how i got it to work:
function lCount(){
$.get("../includes/shoutcaststuff.php",{Count: "TRUE"}, function(data){
document.getElementById('listeners').innerHTML = data;
});
}
setInterval(lCount,5000);
and added this to the end of the php:
if(isset($_GET["Count"])){
echo $dnas_data['CURRENTLISTENERS'];
}
now it works fine.
thanks for the suggestions guys :)
<?php include('../includes/shoutcaststuff.php');
echo $dnas_data['CURRENTLISTENERS']; ?>
This code only executes once when the page is built. For the rest of the times this javascript is called whatever is first echoed will be the value.
Instead of using a static value here, you are going to need to use an ajax request (or a websocket if you want to use html5). The request will then hit your server once every 5 seconds. Keep in mind that this can cause undue load on your server.
Ratchet is a commonly used PHP WebSocket implementation that allows for data to be sent to the client using push technology. This is probably more preferable than using your polling approach.
PHP code is run on the server generating the HTML/JS. Use ajax if you need to run php code once the page has loaded.
Take a look at this for example;
Using this:
setInterval(document.getElementById("listeners").innerHTML =
"<?php echo "1";?>",5000);
Will output this to the browser:
setInterval(document.getElementById("listeners").innerHTML =
"1",5000);

How to call specific PHP function using Ajax and result from JQuery

My first contact with Ajax is happening right now, and I'm kind a confused. I've read many of questions asked, but I'm not able to read the answer, that is most likely here somewhere.
Situation is, I'm using OOP PHP approach, and all I do go through index.php with parameters. So I do not call any other .php file in form posts, button clicks..
I've created an HTML listbox (which I'd like to remove vertical scrollbar, but that's just a bonus to resolve), which feeds my categories in it.
Now, by clicking each category I'd like to call certain function that would then generate output for the other div.
function swapContent(){
$("#myPresentDiv").html('<img src="../../imgs/ajax-loader-big.gif"/>').show();
var cat = $('#listbox').val();
$("#action").change(alert(cat));
var url = "&s=".cat;
$.post(url, {contentVar: cat} ,function(data) {
$("#myPresentDiv").html(data).show();
});
}
So, my JQuery script picks up correct Category, I alert it to alert dialog, so I'm sure that's fine, and then with code as it is at the moment, I reload my whole page so I get, page in page in page in page...
I'm trying to figure out how to write JQ ajax call, that would return only the results, not the whole page.
can I put URL "index.php&s="cat, and then somehow tell to ajax "go through index, call function displayresults ($cat); ?
Hope everything I wrote make sense to you :)
Tnx.
The url's your ajax function call, must return only the page parts and not the whole html document.
If you have
$.post('ajax.php',data,function(d){
$('#responsediv').html(d).show();
});
The file ajax.php must only return the page parts,like
<div>This is the new content</div>
so you will not have page inside page.
If you look at the frameworks or cms out there, they basically have routes that map calls to your index.php function to methods of the controller.
This is a complex argument, you could try to start out reading this article
Yeah, that makes sense. Your question is basically: when you get a result of an AJAX op and insert it into your page, it inserts the whole layout again, rather than the template.
OK, so the solution is to make a call to a PHP script that is "unstyled" i.e. has no template data. Your PHP script should therefore just output a short HTML snippet rather than a page (you might have a 'header' and 'footer' that can be removed for this page). What action you need to take depends what you're using on the server side - framework? CMS? Custom PHP app?
I did the exact thing for a internal app quite some time ago....What happened was i was passing the class name, function name and the function parameters via ajax variables and reading the same in php at the backend and then call the appropriate function in the class with those paraeters.
The PHP code:
$option = trim($_GET['v']);
switch ( $option ) {
case 1:
$sid = trim($_GET['q']);
$page = trim($_GET['p']);
$method = trim($_GET['m']);
$class = new $page( $link );
echo $class->$method( $sid );
break;
case 2:
$page = trim($_GET['p']);
$method = trim($_GET['m']);
$class = new $page( $link );
echo $class->$method();
break;
default:
echo '';
break;
}
But this was an internal app, so there was no injection attacks, xss,xsrf,session hijack issues....things might differ for you
Hope this helps.
I think you are searching for a GENERAL strategy to handle ajax requests its upto you
for example Server Side Ajax
unless you are using a specific framework (CI , yii etc)
You might want to look into some frameworks, as they can make this for you infinitely easier to implement:
http://demo.atk4.com/demo.html?t=20

Categories