Programming
Append value to multiselect with jquery
by admin on Aug.12, 2010, under Programming
This is a simple function to append values to a multiselect. In the comments you’ll find the explanation of each command.
function appendMulti(value,target){
//Get old values from multiselect (they are an array)
oldvalues= $(target).val();
//We need to check if the multiselect is empty
if(oldvalues){
//It's not empty
//Find the lenght of the array, the beauty of this is that since arrays starts from 0, this number will always be the next available slot
key = value.length;
} else {
//It's empty
//Initialize a new array and set the key to the first element
var oldvalues = [];
key = 0;
}
//Append new value to the array
oldvalues[key] = value;
//Set the new values
$(target).val(oldvalues);
return true;
}
You can call the function like this:
appendMulti('value','#multiselect');
Ajax calls vs MSSql
by admin on Jun.29, 2010, under Programming
Please note that this article can be applied to any non utf-8 encoding.
I recently needed to send data using ajax to an MSSql 2000 database (which requires windows-1252).
As you may know javascript sends data encoded in utf-8 (utf-16 to be more precise), this must be accepted as a fact thus making the use of a php function to convert the encoding before sending to the database. Here is the snippet I use:
function convertor($array){
if (is_array($array) && count($array) > 0){
foreach ($array as $k => $v){
$return[$k] = mb_convert_encoding($v, "iso-8859-1","UTF-8");
}
} elseif (!empty($array)) {
$return = mb_convert_encoding($array, "iso-8859-1","UTF-8");
} else {
return false;
}
return $return;
}
Basically what it does is to check if we have an array or a single value then proceed to convert it to the desired encoding (in my case iso-8859-1), after converting the values these can be safely stored into the database.
I have also to add that I’ve encountered some jquery plugins that messes with the encoding of the strings. To be on the safe side you may use this line:
unescape(encodeURIComponent(string))
jquery-ui dialog with ajax how to avoid a common error
by admin on May.31, 2010, under Programming
I’ve made this post because I got asked why this snippet gave problems:
$.ajax({
type: "POST", url: "somepage.php",
success: function(html){$("#someDiv").html(html).dialog();}
});
The problem with this code is that it creates a new instance of dialog each time, basically adding a new dialog div in the html for each call.
Aside from being unable to reopen the dialog after closing it, your are replicating your ajax code several times across the webpage creating all sort of problems with the jquery selectors.
The jquery-ui docs do not expalin well this issue, so to avoid headache here is the correct code:
$(function() {
$("#someDiv").dialog({ autoOpen: false });
});
then in the ajax call:
$.ajax({
type: "POST", url: "somepage.php",
success: function(html){$("#someDiv").html(html).dialog('open');}
});
European Countries array
by admin on Jun.03, 2009, under Programming
We recently had to create a select reserved only to contries from the european union, here’s the array with the standard countries codes:
$eu_countries = array( "AT" => "Austria", "BE" => "Belgium", "BG" => "Bulgaria", "CY" => "Cyprus", "CZ" => "Czech Republic", "DK" => "Denmark", "EE" => "Estonia", "FI" => "Finland", "FR" => "France", "DE" => "Germany", "GR" => "Greece", "HU" => "Hungary", "IE" => "Ireland", "IT" => "Italy", "LV" => "Latvia", "LT" => "Lithuania", "LU" => "Luxembourg", "MT" => "Malta", "NL" => "Netherlands", "PL" => "Poland", "PT" => "Portugal", "RO" => "Romania", "SK" => "Slovakia (Slovak Republic)", "SI" => "Slovenia", "ES" => "Spain", "SE" => "Sweden", "GB" => "United Kingdom" );
print_r output beautifier
by admin on May.13, 2009, under Programming
Today I wrote a script that needed to manipulate several multidimensional arrays, some of them pretty deep.
The standard php function print_r is an hellish mess, browsing through the marvellous php.net documentation I’ve found several examples to beautify the output of print_r (just to give credit: “Bob” and “Everett” were my inspiration) since no one actually did it the way I wanted I made a custom one myself:
function print_rr($array){
foreach($array as $key=>$value){
if(is_array($value)){
$id = md5(rand());
echo '[<a href="#" onclick="return expandParent(\''.$id.'\')">'.$key.'</a>]<br />';
echo '<div id="'.$id.'" style="display:none;margin:10px;border-left:1px solid; padding-left:5px;">';
print_rr($value, $count);
echo '</div>';
} else {
echo "<b>$key</b>: ".$value."<br />";
}
}
echo '<script language="Javascript">
function expandParent(id){toggle="block";if(document.getElementById(id).style.display=="block"){toggle="none"}document.getElementById(id).style.display=toggle};
</script>';
}
Feel free to use it anyway you like it.