Posts Tagged ‘php’

PHP fix length random string

If you need a simple function to generate a fixed length string with random characters, use this:

function genRandomString($length) {
$characters = ’0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$string = ”;
for ($p = 0; $p < $length; $p++)
$string .= $characters[mt_rand(0, strlen($characters)-1)];
return $string;
}

Send special characters with PHP from Ajax calls

Yesterday I had a very strange situation… Using a jQuery datatable, I was trying to display some info, populating the table with Ajax calls… Everything was OK, until somebody didn’t try to use special characters, like “ö” or ENTER… Than the datatable went crazy… Here is what I found out:

Basically, with Ajax I was calling a php file, which will return the result of a query, let’s say it’s somebody’s name now. If that name contains special characters, first you have to call the following function in the php file:

function fix_str($str){
$str = htmlentities($str, ENT_COMPAT, ‘UTF-8′);
$str = str_replace(chr(10),”,$str);
$str = str_replace(chr(13),’
’,$str);
$str = rawurlencode($str);
return $str;
}

This guarantees that the result sent back to the jQuery won’t be messed up…

On the Javascript side, to get the result well formatted, if you have the Ajax call result in the variable “resp”:

str = unescape(resp);

This way you will be able to pass any character through the Ajax call.

Check all uncheck all HTML checkboxes with Javascript

The following little piece of code shows how to check all / uncheck all HTML checkboxes with simple Javascript, without jQuery. It’s not something hard to do, but as I use it a lot, it’s nice to have it at one click…

<form name=”formname” method=”POST” action=”?action=dosomething”>

Check all: <input type=”checkbox” name=”checkall” id=”checkall” value=”Check All” onclick=”Check(‘formname’,$(‘.check_list’))”>

Option 1<input type=’checkbox’ class=”check_list” name=”check_list[]” value=”1″>
Option 2<input type=’checkbox’ class=”check_list” name=”check_list[]” value=”2″>
Option 3<input type=’checkbox’ class=”check_list” name=”check_list[]” value=”3″>

</form>

The corresponding Javascript code:

function Check(form_name,chk){ // alternate check all/check none of items

if(eval(“document.”+form_name+”.checkall.value”)==”Check All”){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
eval(“document.”+form_name+”.checkall.value=’UnCheck All’”);
}

else{
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
eval(“document.”+form_name+”.checkall.value=’Check All’”);
}

}

Sort multi-dimensional array in PHP

How to sort multi-dimensional arrays using PHP? Sorting one dimensional array is pretty easy, but to do this with two or multi-dimensional arrays, is a little different. Here is how to do it:

There’s a given 2 dimensional array:

$array[] = array(“id”=>1,”title”=>”Monkey”);
$array[] = array(“id”=>2,”title”=>”Alien”);
$array[] = array(“id”=>3,”title”=>”Stargate”);
$array[] = array(“id”=>4,”title”=>”New”);

We use the usort function of PHP:

usort($array, “cmp”);

… where cmp is the following function:

function cmp($a, $b){
return strcmp($a["title"], $b["title"]);
}

After sorting the $a array, it will look like this:

$array[0] = array(“id”=>2,”title”=>”Alien”);
$array[1] = array(“id”=>1,”title”=>”Monkey”);
$array[2] = array(“id”=>4,”title”=>”New”);
$array[3] = array(“id”=>3,”title”=>”Stargate”);

Now, the array is sorted by ‘title’ in ascending order. To sort in in descending order, just change the order of the compared elements in the cmp function:

return strcmp($b["title"], $a["title"]);

Create Excel file from PHP with tables using headers

Did You ever needed to create pure and simple Excel spreadsheets from php? Are you tired with complicated classes, large and nonsense code? Here is a tiny code sample, showing how easy it  is to create Excel files directly from a php file. I won’t comment the lines, as the code is obvious…

$file_name = “myfile.xls”;

header(‘Pragma: public’);
header(‘Expires: 0′);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(‘Cache-Control: public’);
header(‘Content-Description: File Transfer’);
header(‘Content-Type: text/xls; name=”‘ . $file_name . ‘”‘);
header(‘Content-Disposition: attachment; filename=”‘ . $file_name . ‘”‘);
header(‘Content-Transfer-Encoding: binary’);

$content = ‘<table>
<tr>
<td>Header1 (A)</td>
<td>Header2 (B)</td>
<td>Header3 (C)</td>
</tr>
<tr>
<td>Content 1A</td>
<td>Content 1B</td>
<td>Content 1C</td>
</tr>
<tr>
<td>Content 2A</td>
<td>&nbsp;</td>
<td>Content 2C</td>
</tr>
</table>’;

echo $content