4 Oct, 2011
The Google Maps API is heavily biased to xml but what if you’d rather get your markers in json from an ajax call? Since we will be using jquery for the call you will need to include the library into your page.
First set up the code that will serve the json. We will put this at /get_markers and will be using Kohana 3 but any code will work aslong as it returns json.
//get markers from database
$lastpoints=ORM::factory('markers')->where('timestamp','>',$this->request->post('date'))->order_by('timestamp','DESC')->find_all();
if($lastpoints->count() > 0)
{
echo json_encode( array_map( create_function( '$obj', 'return $obj->as_array();'), $lastpoints->as_array() ) );
}
Now we can grab that data and use it.
$.ajax({
type: "POST",
url: '/get_markers',
dataType: 'json',
data: 'date=' + dateText,
success: function(data){
locations.length = 0;
for (p = 0; p < data.length; p++) {
locations.push(Array(data[p].latitude,data[p].longitude,data[p].timestamp));
}
initialize();
}
});
In the example above we would be reloading only the new markers onto the map. If you would like the markers to just be added to the markers that are already on the map, you will need to remove the loop and the locations length reset in the success function and just have the array push. The initialize function is the code that sets up the map and is found in most examples and tutorials on the API.
Of course you will need to set the ajax call to an event.
2 Oct, 2011
The Microsoft technology Clickonce is technology that enables clicking a link on a website and installing software. It has been a component of the .NET framework since version 2.0. ClickOnce is similar to Java WebStart and GNU/Linux ZeroConfig.
ClickOnce is supported natively by Internet Explorer and Firefox via a Firefox plugin.
There is also a provision in the ClickOnce architecture to automatically update applications. Applications can be configured to check for updates automatically at startup or after startup. ClickOnce also provides API’s for further controlling the update process.
ClickOnce is enabled in the Visual Studio project settings area.
30 Sep, 2011
I needed to order a mysql result but in a somewhat custom way.
Say there is a field “id” and rows of values of 8, 4, 2, and 19.
SELECT `id` FROM `table_name` ORDER BY `id`;
This would obviously return the order: 2,4,8,19.
But what if you needed the 4 to be first and then order by integers?
SELECT IF(`id` = '4','0','1') AS `custom_order` FROM `table_name` ORDER BY `custom_order`;
This would return 4,2,8,19.
Simple and can be really helpful.
21 Sep, 2011
I was recently doing some javascript testing and needed to make some asynchronous (sequential) ajax requests for authentication, creating of records, etc. As with all good things jQuery, the solution to this problem is to install the plugin. The ajaxq plugin, that is.
Installation is as simple as including it in your code.
[script language="javascript" src="/js/jquery-1.6.2.min.js"][/script]
[script language="javascript" src="/js/jquery.ajaxq.js"][/script]
Then calling the $.ajaxq method, which takes the queue name (use different names to run multiple asynchronous queues synchronously) and the settings object with the same parameters as jQuery’s ajax function.
$(function() {
//Login, then logout
$.ajaxq("queue", {
url: '/api/user/login',
type: "POST",
data: {
username: "username",
password: "password"
},
success: function(data) {
document.write("Logged in...<br />");
}
});
$.ajaxq("queue", {
url: '/api/user/logout',
type: "POST",
success: function(data) {
document.write("Logged out...<br />");
}
});
});
And finally, an example which runs two queues simultaneously:
$(function() {
//Login, then logout
$.ajaxq("one", {
url: '/api/requestone',
type: "POST",
success: function(data) {
document.write("Request one queue 'one'...<br />");
}
});
$.ajaxq("two", {
url: '/api/user/logout',
type: "POST",
success: function(data) {
document.write("Request 1 queue 'two'...<br />");
}
});
$.ajaxq("two", {
url: '/api/user/logout',
type: "POST",
success: function(data) {
document.write("Request 2 queue 'two'...<br />");
}
});
});
Output could either be
Request 1 queue 'two'...
Request 2 queue 'two'...
Request 1 queue 'one'...
or
Request 1 queue 'one'...
Request 1 queue 'two'...
Request 2 queue 'two'...
20 Sep, 2011
Unfortunately Kohana 3.2 dropped the pagination module. Usually updates just extend the current features so not sure why they removed the functionality. It may have had pure MVC concepts but it was a nice feature.
However, getting the results in orm for a pagination is still pretty simple. First add the route that will pass the page number from the friendly URL.
Route::set('pagi_page', 'welcome/index(/)')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
'page' => '1'
));
Next set up the limits in your controller action:
public function action_index()
{
$limit = 30; //change this to the number of results you want.
$page_number = $this->request->param('page');
$offset = $limit * ($page_number-1);
}
And then just use those variables to manipulate the ORM to get the expected results.
public function action_index()
{
$limit = 30; //change this to the number of results you want.
$page_number = $this->request->param('page');
$offset = $limit * ($page_number-1);
$results = ORM::factory('items')->limit($limit)->offset($offset)->find_all();
}
16 Sep, 2011
Adding a function that dynamically generates a robots.txt to Kohana 3 is actually very easy.
First we need to add a route to bootstrap.php that will route the yourdomain.com/robots.txt url to the function.
Route::set('robots', 'robots.txt')
->defaults(array(
'controller' => 'home',
'action' => 'robots',
));
Now we create the action “action_robots” in the home controller and output the exclusions we are looking for.
public function action_robots()
{
header('Content-type: text/plain');
echo "User-agent: *\r\n";
echo "disallow: /";
exit();
}
The content-type here is very important, as search engines will not read it as a php file.
NOTE: This will not work if your application if the index_file configuration in bootstrap.php is set to TRUE. This is because if it is set to TRUE then the url would turn out to be /index.php/robots.txt which is obviously not where the search engines will be looking for the restrictions.
11 Sep, 2011
I find that the best way to make a log-in for a flex application is to make it via states. I originally made my application have a log-in that then forwarded to a new view, but the transfer of data and the overall efficiency proved to be a lot easier by using states between the “LogIn” and the “HomeView”.
The data object saved by the phone while the application is running can keep a user log-in in when the user closes out of the application for a little while. If a username variable in the data object is available, then you can make ‘currentState = “HomeView”‘ otherwise have the ‘currentState = “LogIn”‘.
Again, it’s possible to do it completely with views and data forwarding. I just ran into problems with the back button returning you to the “LogIn” view even though they were already signed in. (since data is only easily pushed to new views. Views needs to be set up to get data from popped views, only to find they need to forward to the next view because they were already signed in.) Although I think there is a way to pop the “LogIn” view from the view stack, but overall I found this method less complex for application authentication.
5 Sep, 2011
I recently needed to add a datetimepicker to an API project I’m working on. I love the jQquery-ui datepicker, but it doesn’t have the option for time. I found a great plugin to the jQuery-ui datepicker called the Timpicker Addon.
The addon supports localization, has sliders to pick the time and is fully configurable. I was able to make it do exactly what I needed, pick to the exact PHP date format I needed (Y-m-d H:i:s).
31 Aug, 2011
I’m working on an Kohana based API where I needed to return some data from an ORM result as an array. Sure, I can loop through the ORMIterator and assign the results to an array, but there must be a nicer way of doing this. I ran across this thread which uses and anonymous function to do the as_array() call. See:
echo json_encode( array_map( create_function( '$obj', 'return $obj->as_array();', ORM::factory('image')->find_all()->as_array() ) );
29 Aug, 2011
I manage a few Joomla websites on a underpowered virtual servers and they occasionally crash under load. Some of the time when they crash, the jos_session table becomes corrupt, yielding this error:
jtablesession::Store Failed
DB function failed with error number 1146
Table 'dbname.jos_session' doesn't exist SQL=INSERT INTO `jos_session` ( `session_id`,`time`,`username`,`gid`,`guest`,`client_id` ) VALUES ( 'rAxRWo70lP8aQOwtokou4IRsMZ','2444128272','','0','1','0' )
This is because jos_session is used to manage user sessions, so it is written to frequently. The most common cause of a corrupt table is a failed write. When the server runs out of resources, it is usually in the middle of a write, thus the corrupt jos_session table. Here’s a way to fix it in Joomla 1.5:
DROP TABLE IF EXISTS `jos_session`;
CREATE TABLE IF NOT EXISTS `jos_session` (
`username` varchar(150) default '',
`time` varchar(14) default '',
`session_id` varchar(200) NOT NULL default '0',
`guest` tinyint(4) default '1',
`userid` int(11) default '0',
`usertype` varchar(50) default '',
`gid` tinyint(3) unsigned NOT NULL default '0',
`client_id` tinyint(3) unsigned NOT NULL default '0',
`data` longtext,
PRIMARY KEY (`session_id`(64)),
KEY `whosonline` (`guest`,`usertype`),
KEY `userid` (`userid`),
KEY `time` (`time`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Because jos_session is storing temporary data, dropping it and recreating the structure is a simple fix. Had the table had data that we needed, we’d need to go more in depth for recovery.