Tag Archives: snippets - Page 3

How to Find and Replace Data in MySQL

Recently, while migrating my blog, I had to find all the occurrences of my old URL and replace it with my URL. One way of doing this was to get a database dump, open it in a text editor and the do a find replace and the import it back. This is a 4 step process. However, I found that we can do this only in 1 step directly in MySQL using the UPDATE statement with REPLACE function.

Read more »

Get various parts of URL using JavaScript

Recently I wanted to get a domain name of the page on which I was currently on. I was trying to parse the entire URL of the page and then parse it using regular expressions etc. to get the domain name. I was able to get the required information using this method, but then I thought that this is something for which there should be an easier way, and indeed there is an easier way.

We can use the Location Object to get all the various parts of the URL. (e.g. window.location.href to get the entire URL)

Note: There is no public standard that applies to the location object, but all major browsers support it.
Read more »

Sticky Header and Footer using CSS

In some sites we want to keep the Header and Footer always visible and fixed to top and bottom of the screen respectively. In this article I will show how we can implement the sticky (or fixed) header and footer using only HTML and CSS.

To create a layout with sticky header and footer can be easily done using the CSS property position:fixed. In my example, I will be displaying a sticky header and a sticky footer.

Read more »

Get search query string from search engines using PHP

I have often come across situations when I would like to implement some functionality based on what the user searched for in a search engine like Google, Bing etc.

The search query string is normally passed as GET variables ‘q’ or ‘query’. The function below will return the search query string.

// Function returns the query string (q or query parameters) from the referrer
function get_search_query()
	$ref_keywords = '';
	// Get the referrer to the page
	$referrer = $_SERVER['HTTP_REFERER'];
	if (!empty($referrer))
		//Parse the referrer URL
		$parts_url = parse_url($referrer);

		// Check if a query string exists
		$query = isset($parts_url['query']) ? $parts_url['query'] : '';
			// Convert the query string into array
			parse_str($query, $parts_query);
			// Check if the parameters 'q' or 'query' exists, and if exists that is our search query terms.
			$ref_keywords = isset($parts_query['q']) ? $parts_query['q'] : (isset($parts_query['query']) ? $parts_query['query'] : '' );
	return $ref_keywords;

Note: Google has recently stopped sending the search query terms for all logged-in users. So, if a user is logged-in Google and searches for something and then follow a link to your site, you won’t be able to get the query terms that they searched for. The Making search more secure blog post by Google, explains this is more details.

Make a div stick to top when scrolled to

There are times when you would want to display a bar at the top of the page when user scrolls on the page and it should go back to its original position when the user scrolls back up. This is particularly useful when you want to add say a share bar, a search bar, etc and make it always visible even when the user is at the bottom of the page.
Read more »