Skip to main content

Posts

JSON array of objects

It can be a situation where you got a array of data that should be sent in the JSON format to server via ajax, then you can do the following <script type="text/javascript" > groups = ['A', 'B', 'C']; users  = ['user1', 'user2', 'user3']; //required format is  [{"group_name" : "A", "leader" : "user1"}, {"group_name" : "B", "leader" : "user2"}, {"group_name" : "C", "leader" : "user3"}] group_leaders = new Array(); len = groups.length; for(var i = 0; i < len; i++ ){     group_leaders.push({"group_name" : groups[i], "leader" : users[i]}); } alert(JSON.stringify(group_leaders)); </script> Here you can test here http://jsfiddle.net/sailesh/6wXdY/

Windows Azure SQL Database PDO

I faced a problem while trying to connect Windows Azure SQL Database from local system using PDO. Here I'm using PHP 5.4+ and Apache 2.4 on Windows 8. I didn't find better tutorial for trouble shooting. Following are the steps that explains you what to do.. Download php_pdo_sqlsrv_54_ts.dll and placed it in php/ext directory. Here you can find it http://www.microsoft.com/en-us/download/details.aspx?id=20098 Download SQLSRV30.EXE and extracted to php/ext directory Open the php/php.ini file and added the following line extension=php_pdo_sqlsrv_54_ts.dll It needs Microsoft SQL Server 2012 Native Client, to go further. So download it from http://www.microsoft.com/en-us/download/confirmation.aspx?id=29065 for 32bits(x86) http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409 for 64bits(x64) http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409 Restart the Apache server. Write the following code in php file to connect. $server_url = "xxxxxx...

Download file using PHP

Downloading a file can be done in two ways: Direct download Indirect download Direct Download: Assumptions: File location: c:/xampp/htdocs/project/docs/test.doc URL: http://localhost/project/docs/test.doc Code:   <a href='http://localhost/project/docs/test.doc'>click here</a> Indirect Download: (recommended) Assumptions: File location: c:/xampp/htdocs/project/docs/test.doc PHP file for the download code: location: c:/xampp/htdocs/project/download.php <?php   $file_name = $_GET['file'];   $file_path = "docs/".$file_name;  //setting the content type   header('content-type: application/octet-stream');   //downloads file as attachment   header("content-disposition: attachment; filename='$file_name'");   //actual file path   readfile($file_path); ?> Note: content-type application/octet-stream can be used for any type of file. To use the above file for download. Create link like as be...

PHP Best Practices

This guide will give you solutions to common  PHP  design problems. It also provides a sketch of an application layout that I developed during the implementation of some  project s. php .ini quirks Some settings in the  php .ini control how  PHP  interpretes your scripts. This can lead to unexpected behaviour when moving your application from development to the productive environment. The following measures reduce dependency of your code on  php .ini settings. short_open_tag Always use the long  PHP  tags:  php echo "hello world"; ?> Do not use the echo shortcut  . asp_tags Do not use ASP like tags:  <% echo "hello world"; %> gpc_magic_quotes I recommend that you include code in a global include file which is run before any $_GET or $_POST parameter or $_COOKIE is read. That code should check if the gpc_magic_quotes option is enabled and run all $_GET, $_POST and $_COOKIE values through the  stripslashes ...

Create Thumbnail

To generate thumbnail, here is the  simple and easiest way. It will help you in creating thumb for jpg, png and gif images. /* generateThumb() * * @param mixed $source Path to source file * @param mixed $destination Path to destination file * @param mixed $width Thumbnail file width * @param mixed $height Thumbnail file height * @return bool */ function generateThumb($source, $destination, $width = 100, $height = 100){ $ext = strtolower(substr($source, strrpos($source, ".")+1)); $format = ($ext == 'jpg')?'jpeg':$ext; $from_format = "imagecreatefrom".$format; $source_image = $from_format ( $source ); $source_width = imagesx ( $source_image ); $source_height = imagesy ( $source_image ); $ratio1 = $source_width/ $width; $ratio2 = $source_height / $height; if($ratio1 > $ratio2){ $width = $width; $height = $source_height/$ratio1; }else{ $width = $source_width/$ratio2; $height = $height; } $target_image = imagecr...

Compare 2 mysql databases

Below code will list all the tables with fields (& type) and total records in each table, so that we can verify whether fields are same in both database fields and record counts. <?php mysql_connect("localhost", "user1", "pwd1") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $tables = array('tbl1', 'tbl2','tbl3'); echo "<table  border='1' style='float: left'>"; foreach($tables as $tbl){   $sel = "SELECT COUNT(1) FROM $tbl";   $res = mysql_query($sel);   $rec = mysql_fetch_row($res);     echo "<tr><th colspan='2'>$tbl ($rec[0])</th></tr>";   $sel1 = "SHOW FIELDS FROM $tbl";   $res1 = mysql_query($sel1) or die($sel1.mysql_error());   while($rec1 = mysql_fetch_row($res1)){     echo "<tr><td>$rec1[0]</td><td>$rec1[1]</td></tr>";   } } ech...

HTTP POST without cURL using PHP

I don't think we do a very good job of evangelizing some of the nice things that the PHP streams layer does in the PHP manual, or even in general. At least, every time I search for the code snippet that allows you to do an HTTP POST request, I don't find it in the manual and resort to reading the source. (You can find it if you search for "HTTP wrapper" in the online documentation, but that's not really what you think you're searching for when you're looking).  So, here's an example of how to send a POST request with straight up PHP, no cURL: <?php      function do_post_request($url, $data, $optional_headers = null) {           $params = array('http' => array(                                 'method' => 'POST',                   ...