Posts Tagged tips

Implementing Quick Table Search using jQuery filter

Before sometime I had written a post on jQuery case-insensitive expression filter. Now today we will learn about how to put simple filter for our HTML table using jQuery and it's case-insensitive expression.

It's takes just few lines of Javascript / jQuery code to implement quick filter for an HTML table, here you go:

The HTML

 
<label for="kwd_search">Search:</label>
<input type="text" id="kwd_search" value=""/>
<table id="my-table" border="1" style="border-collapse:collapse">
<thead>
<tr>
<th>Name</th>
<th>Sports</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sachin Tendulkar</td>
<td>Cricket</td>
<td>India</td>
</tr>
<tr>
<td>Tiger Woods</td>
<td>Golf</td>
<td>USA</td>
</tr>
<tr>
<td>Maria Sharapova</td>
<td>Tennis</td>
<td>Russia</td>
</tr>
</tbody>
</table>
 

Read the rest of this entry »

Tags: , , ,

Truncate last N lines of a file using PHP

For one of my project I needed to remove certain footer / tail-end lines from the large-file to make it clean as it was containing some summary of records within the file. Because if I do that it can be directly loaded into database by LOAD DATA command.

Usually one can read file into an array using PHP's file function but that's okay when size of file is in few hundred KB, if your file-size is running in to few MB to few hundred MB we need to find and use some efficient way to play with them. Because with large file reading as an array we can (and certainly will) run into memory outage issue.

To remove lines from the end of file you can use following snippet of code:

 
// Function to truncate last N lines from the file
function truncate_last_n_lines_of_file($file, $lines_to_remove, $chunk = 1024)
{
	// Open file in read+write mode
	$handle = @fopen($file, "a+");
	$lines_found = array();
 
	// Check if it's a valid file handle
	if($handle)
	{
		// size of file
		$max_length = $file_size = filesize($file);
		if(intval($file_size) == PHP_INT_MAX)
		{
			$max_length = PHP_INT_MAX;
		}
 
		// loop through file as long as we are not done with truncating required files
		for($length = 0; $length < $max_length; $length += $chunk)
		{
			if( ($max_length - $length) > $chunk)
			{
				$seek_size = $chunk;
			}
			else
			{
				$seek_size = $max_length - $length;
			}
 
			// read data in chunk
			fseek($handle, ($length + $seek_size) * -1, SEEK_END);
			$data = fread($handle, $seek_size) . $data;
 
			// Loop thorugh chunk to see if we are done with truncate
			for($i = $chunk; $i > 0; $i--)
			{
				if($data[$i-1] == "\n")
				{
					$lines_found[] = $i;
				}
				if(count($lines_found) == $lines_to_remove+1)
				{
					ftruncate($handle, ($max_length) - ($seek_size - $lines_found[$lines_to_remove]));
					return true;
				}
			}
			fclose($handle);
		}
		return false;
	}
}
 

Read the rest of this entry »

Tags: , , ,

jQuery case-insensitive Contains Expression..!

Most of the time to search through DOM and filter text we need a jQuery Expression/filter which gives us case-insensitive result. I believe it's required to have it as a part of jQuery base-library but anyway writing your own is very simple.

I have gone through some of other implementation before writing this but here this version is complete copy of jQuery library's "contains" filter except it is case-insensitive.

In below code when it's using $(elem).text() it's calling Sizzle.getText internally which is Utility function for retreiving the text value of an array of DOM nodes.

$.extend($.expr[":"],
{
    "contains-ci": function(elem, i, match, array)
	{
		return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
	}
});

Here are some other nice jQuery Selectors and Expressions: Read the rest of this entry »

Tags: , , ,

Reading MSSQL BLOG column data with PHP

I was working on a migration script to transfer data from legacy desktop software to Portal we have developed. While migrating attachments (stored as TIFF files) in BLOB (Binary Large Object) in Microsoft SQL Database Server using PHP it was not a cake-walk. When I observed every time it was creating a file of same size (4KB). I quickly understood that there is some limitation (of size) for reading records in PHP configuration.

I searched through PHP.INI file and found following settings:

; Valid range 0 - 2147483647.  Default = 4096.
;mssql.textlimit = 4096
 
; Valid range 0 - 2147483647.  Default = 4096.
;mssql.textsize = 4096

I removed ";" and made values as zero, thinking that it will let PHP read unlimited size of data but didn't work out. So changed it to max value specified as following and worked perfect: Read the rest of this entry »

Tags: , , , , ,

Posting or Uploading Files using cURL with PHP

cURL - groks those URLs

groks URLs

cURL is very important tool for PHP programmers, we tends to do so many thing with using this tool. Posting data, imitating a form post and lot more, it's usage is countless. Some time back I came across situation where we have to post files to a 3rd party faxing service. I knew there are many possibilities but I didn't wanted to get into sockets and file stream to get this done, just wanted to post FILE in addition to rest of the post data.

// URL on which we have to post data
$url = "http://localhost/tutorials/post_action.php";
// Any other field you might want to catch
$post_data['name'] = "khan";
// File you want to upload/post
$post_data['file'] = "@c:/logs.log";
 
// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
 
// Just for debug: to see response
echo $response;

Above Code is the bare minimum required to post/upload file using CURL. As used in above example, it's not mandatory to use array index as "file". You can use anything required depending upon the requirement.

Posting multiple files is also easy, with the same code you can add multiple file upload using following lines:

$post_data['alian_error_log'] = "@c:/alian_app_error.log";
$post_data['myapp_error_log'] = "@c:/myapp_error.log";

Here @ is the key character, prefixing local file path with @ does all the trick.

 Read the rest of this entry »

Tags: , , , , , ,

How to auto-restart Windows Service upon failure

Windows-service recovery from failure

Windows-service recovery from failure

If you're one of those who is administering Windows as a web-server administrator or as a windows admin and you need to take some action upon failure of your windows-service then right click on service for which you want to set recovery go to "Properties > Recovery" and choose action for First, Second and Subsequent failures of the service.

Here we have following choices:

Take No Action: Generally the default action unless modified for any non-windows service we or other programs might have created. For crucial services like web-server (apache or IIS), we usually would like to take some actions instead of doing nothing.

Restart the Service: The best option for 90% of the services which don't required restart of computer and you still want that windows-service to get started again, then choose this option.

Run a Program: I believe this option can either help you draw user's attention (as service runs in the background) by popping-up some alert message or to either writing some logs and then manually restarting that service via that program or there can be any other possibility which I don't know. Read the rest of this entry »

Tags: , , ,

Printing data table vertically..! Why? and How?

Last week one of my friend called me up as he got stuck when he has to print data table vertically. Yes vertical table, it means that you want your data to print from top to bottom and not left to write, which is standard conventional way to generate html table.

Vertical Table (Printing array data from top to bottom)

Vertical Table (Printing array data from top to bottom)

I took it as a challenge and wrapped that up in few minutes. But at that time I was so close to the problem that I could not see that PHP has some inbuilt functions which can help me do that and I did it other way around without using inbuilt PHP function. Here is the sample code for the same, which prints table vertically with the help of inbuilt PHP function array_chunk.

Array is the most powerful feature of PHP and playing with array is always been fun.

Sample code below uses array_chunk but I must say it was not a great help but it makes you less logic.!
// Function to print array data vertically
function print_vertical_table_sys($data_list, $rows)
{
	$columns = (count($data_list)/$rows);
	$data_list = array_chunk($data_list, $rows);
 
	echo '
<table border="1" style="border-collapse:collapse">';
	for($row_index = 0; $row_index < $rows; $row_index++)
	{
		echo '
<tr>' . "\r\n";
		for($column_index = 0; $column_index < $columns; $column_index++)
		{
			echo '
<td>' . $data_list[$column_index][$row_index] . '' . "\r\n";
		}
		$column_index = 0;
		echo '' . "\r\n";
	}
	echo '</table>
 
';
}
 
// Function Usage
$list = array();
for($i = 0; $i &lt;= 59; $i++)
{
	$list[] = $i+1;
}
//print_vertical_table_sys ($list, 10);

Now my question to you is how would you do that if you come across such requirement? Assume that you have list of array items starting from 1, 2, 3, ..... N and you want to print it with fixed length rows which starts like 1 and next cell will have 11 or something similar (similar to what you see in above image).

It's not that difficult but I would suggest give it a try and you will enjoy. Doesn't matter what technology you work with as the example which I will show you not will not use any functionality specific to PHP. Basically above code snippet is very close to that but I would suggest that before reading further more you give it a try. Come out of your regular work and make this fun exercise. Read the rest of this entry »

Tags: , , , ,

Paying the bills.!