Thursday, 11 March 2010

PHP: Insert into the middle of an array

Ever needed to insert something into the middle or at specific position in a php array?

Well this function may help;




CODE
<?php

$fruits = array("apple", "pear", "orange", "plum");
$insert = array("grape", "peach");

var_dump($fruits);
var_dump($insert);


array_insert($fruits, $insert, 2);

var_dump($fruits);



function array_insert(&$array, $insert, $position) {
settype($array, "array");
settype($insert, "array");
settype($position, "int");

#if pos is start, just merge them
if($position==0) {
$array = array_merge($insert, $array);
} else {


#if pos is end just merge them
if($position >= (count($array)-1)) {
$array = array_merge($array, $insert);
} else {
#split into head and tail, then merge head+inserted bit+tail
$head = array_slice($array, 0, $position);
$tail = array_slice($array, $position);
$array = array_merge($head, $insert, $tail);
}
}
}

?>


OUTPUT

array
0 => string 'apple' (length=5)
1 => string 'pear' (length=4)
2 => string 'orange' (length=6)
3 => string 'plum' (length=4)

array
0 => string 'grape' (length=5)
1 => string 'peach' (length=5)

array
0 => string 'apple' (length=5)
1 => string 'pear' (length=4)
2 => string 'grape' (length=5)
3 => string 'peach' (length=5)
4 => string 'orange' (length=6)
5 => string 'plum' (length=4)

Thursday, 4 February 2010

Using PHP sessions in a clustered HA apache setup

So you have a cluster of two or more apache servers and want your php sessions to be shared between the different apache servers?

One great solution is to use memcache. It's a Free & open source, high-performance, distributed memory object caching system and can support the mirroring of sessions to multiple memcache servers (which could be on each apache server). Also your sessions are stored in RAM so they are nice and quick too!

This blog post has a nice setup guide. php session clustering with memcache

Wednesday, 6 January 2010

SpamAssassin 2010 issue!

So it turns out SpamAssassin doesn't like the year 2010!

If your running SpamAssassin, then make sure you do this;


Thursday, 10 December 2009

movingmole.co.uk launched

A great site called movingmole launched today. It provides a service to find and compare removal companies with a free, quick and easy search.

Wednesday, 14 October 2009

v1.0.3 of JQuery lockSubmit released

Version 1.0.3 of our JQuery lockSubmit plugin has been released. It adjusts the method used to disable the submit. Improves cross browser compatibility and effectiveness

Friday, 9 October 2009

jQuery plugin : imageTickBox - Change any input checkbox to an image version

Image Tick Box

This is a small unobtrusive JQuery plugin function which allows you to change any input checkbox to an image version while retaining full functionality! It even pre-loads the images to keep it snappy.


Demo
Example 1
Example 2
Example 3
Example 4


Download
jquery.imageTickBox.js.zip


Options
"tickedImage" - Image to be used when ticked
"unTickedImage" - Image to be used when unticked
"imageClass" - CSS Class to apply to the images.


Usage
1 - Download/Extract JQuery
2 - Download/Extract our plugin

3 - Include jquery and the plugin in your page with;
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.imageTickBox.js">
4 - Include a jQuery selector function call. E.g. to apply this to all tickboxes on the page use;

jQuery(document).ready(function(){

jQuery("input:[type=checkbox]").imageTickBox({
tickedImage: "/images/checkbox_tick.png",
unTickedImage: "/images/checkbox_notick.png",
imageClass: "tickbox"
});

});



License
LGPL

Change Log
v1.0 - First Version

jQuery Plugins Project Page
http://plugins.jquery.com/project/ImageTickBox

Donate
Here is a paypal link for if you find this useful and would like to buy me a drink!

Saturday, 5 September 2009

v1.0.2 of JQuery textinput.focus.colorise plugin released

Version 1.0.2 of our JQuery textinput.focus.colorise plugin has been released. It adjusts the method of changing the colour to a CSS based solution.

Find out more