Archive August 2010

HTML Table Class on CodeIgniter 2.0

Posted by on Aug 31, 2010 in Backend | 3 comments

CodeIgniter 2.0 is coming out soon, so lets start studying the changes done to the HTML Table Class in the dev-release.

One of the most requested features was the ability to set tag attributes to individual cells. On CI 2.0 this is completely possible, however, since the documentation is not fully updated yet, I decided to make this short post to explain how it works.

Fixing UTF-8 encoding problems on MySQL queries with PHP

Posted by on Aug 24, 2010 in Backend | 4 comments

It happened to me a lot of times, I had my connection set to UTF-8, my database was UTF-8, even my tables and every single field, however, for some reason, the information I updated in phpMyAdmin wasn’t being displayed correctly on my website and the information edited on my website wasn’t being displayed correctly in phpMyAdmin.

You know all those weird characters, those that very often come up when you write accented letters or words in another language like Portuguese or Spanish? That’s what I am talking about.

Here is the solution. A single line that can save you hours of research.

Twitter feed on your website with cURL and PHP

Posted by on Aug 15, 2010 in Backend | 3 comments

Pulling code from here and there, I gathered enough information to create a small function that allow you to pull your Twitter feed and display it in your website. What you do is basically call the function and pass a couple parameters. The styling can be done with CSS.

Alternate row colors with PHP

Posted by on Aug 10, 2010 in Backend | 3 comments

I know there are several ways to accomplish this, however, of all the ones I’ve tried, this is the shortest so far:

$i = 0;
while(true)
{
	echo '<tr, td, div, row, whatever, here class="row'.$i.'">';
	$i = 1 - $i;
}

So, if you make two classes, row1 and row2, each with a different background color, the result will be alternated row colors in whatever you’re doing. Very useful for long lists.

Update: you might be interested in how to alternate row colors with pure CSS.

Element indexes and jQuery

Posted by on Aug 10, 2010 in Frontend | No comments

Once the DOM is structured by the browser, each element in our HTML page has an index relative to its parent. Retrieving an element’s index with jQuery is an easy task, and it is so useful that it can save you a couple of lines of code.

Change selected option by value on select list with jQuery

Posted by on Aug 10, 2010 in Frontend | 3 comments

You have a select element, and you need to “select” one of its options based on one of its values. What you do is use the “selected-selector” of jQuery to do it in a single line.

Lets say I have the following select element and I need to dynamically select the option with a value of 3, which would be the “Peach”.

<select name="myselect" id="myselect">
	<option value="1">Apple</option>
	<option value="2">Pear</option>
	<option value="3">Peach</option>
	<option value="4">Orange</option>
</select>

 

$("#myselect option[value=3]").attr('selected', 'selected');

// Or just...
$("#myselect").val(3);

If you want to get a little bit more technical, make sure you read the official jQuery documentation regarding this subject.

Fix / workaround for the z-index problem on IE7

Posted by on Aug 9, 2010 in Frontend | No comments

IE7 behaves strangely when we talk about z-index. It doesn’t always respect the z-index values of our elements. However, if you come around this weird stuff, just add a higher z-index to the parent of the element you want to put above everything else. Don’t ask me why this works but it does!

Lets take a look at a small example:

<div style="z-index: 3000">
  <div style="position:absolute;z-index:1000;">
    this element will appear above everything else
    <a href="#">Page</a>
        ...
  </div>
</div>

Create thumbnails on the fly with CodeIgniter

Posted by on Aug 9, 2010 in Backend | 9 comments

Before explaining the solution, you should know creating thumbnails on the fly is server inefficient, eats a lot of resources and they should be generated as soon as the images are uploaded. However, there are some occasions when we absolutely need to generate thumbs on the fly. In this article we will learn how to do this in CodeIgniter.

Add www subdomain automatically to your domain with htaccess

Posted by on Aug 9, 2010 in Backend | No comments

Sometimes we need to add the www to our domain automatically, maybe for aesthetics or ajax issues across subdomains. Apache .htaccess is our friend here. You just need to add this to your existing .htaccess file or just make a new one with the following contents:

RewriteCond %{HTTP_HOST} ^your-domain\.com [NC]
RewriteRule ^(.*)$ http://www\.your-domain.com/ [L,R=301]

Prevent image hotlinking with Apache and htaccess

Posted by on Aug 9, 2010 in Backend | No comments

Question of the million dollars. Those annoying image hot-linkers, eating our bandwidth, our food, our… nevermind. Anyway, if you want to give them hell and stop this behaviour, add the following to your .htaccess file (remember to edit your domain name):

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://your-server-ip/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://my-domain.com/.*$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.my-domain.com/.*$  [NC]
RewriteRule .*.(gif|GIF|jpg|JPG|png|PNG)$ - [F]

Bonus!: In the third line, you can optionally write your server IP for an extra layer of security. If you don’t know your server’s IP, just open an MS-DOS console (if you’re on Windows) or a Linux console and ping your domain. ie. “ping dannyherran.com”, would return 67.220.192.149 which is my server IP.