Saturday, September 25, 2010

Cannot make the website html validate? try HTML Tidy

Sometimes you cannot seems to find the line where the validation errors come from. In such cases you can try using HTML Tidy to do a auto clean up.



W3C Markup Validation Service also has this option. or you an check out the project page.

http://tidy.sourceforge.net/

Tuesday, September 7, 2010

Magento Video Tutorials: Thanks Armando Roggio :)

Cannot express how greatful I am to this guy. http://www.ecommercedeveloper.com/articles/1540-Building-a-Magento-Theme-Start-to-Finish-Part-One-Prolegomena

I tried to start Magento several times and just got frustrated with the issues with locahost and complex nature of the file structure.
but thanks to above tutorial Magento is like riding a bike.

Videos are on version 1.3 but after reading this note about version 1.4 you can follow the tutorials easily on 1.4

Magento's Theme Hierarchy--Changes in CEv1.4 and EEv1.8

Thursday, September 2, 2010

Mangento Sample Data - 404 Page Error

I see few solutions for this error in the web but for me following steps corrected my error.
Magento Version - 1.4.1.1

Admin -> System -> Index Management -> Reindex all given Indexes

Sri Lanka District and City List Sql File

For a recent project I had to find all the Sri Lankan District and related Cities to populate in two drop downs. The list is not fully complete and Kilinochchi District is not in the list.
Districts - 24
Cities - 2044

Two tables, city table refer district table by did.

Download Sri Lankan District - City Sql file

You can easily import this sql file to mysql using phpmyadmin.

Tuesday, August 31, 2010

Mysql insert multiple/batch of rows using single query

You don't need to loop a mysql insert query to add multiple records to a table. mysql insert support multiple inserts in one query.

Let say you have a table called students with two fields fname and age.

insert into students (fname, age) values
('Kusal', '26'),
('Tom', '18'),
('Ann', '23')

Remember to give the column names in the query.

Quick, Simple and Easy Image Gallery Using jQuery

This is a simple image gallery I wrote for a project. Its small and easy to implement. Ideal if you need to build a simple and quick gallery.
You can improve the code by adding a loading.gif

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How to add table rows using jQuery</title>
<style>
.thumb{vertical-align:top; padding:2px; border:1px #CCC solid;}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>

$(document).ready(function(){    
    
    $(".image").click(function() {    
        //$('#main').hide();
        var image = $(this).attr("href");
        $('#main').attr("src", image);
                
        $('#main').load(function() {            
              $('#main').fadeIn('slow');
        });
        
    return false;
    });
});    

</script>
</head>

<body>
<a href="images/image1.jpg" class="image"><img alt="Image1" src="thumb/image1.jpg" class="thumb" border="0"/></a>
<a href="images/image2.jpg" class="image"><img alt="Image2" src="thumb/image2.jpg" class="thumb" border="0"/></a>
<a href="images/image3.jpg" class="image"><img alt="Image3" src="thumb/image3.jpg" class="thumb" border="0"/></a>
<a href="images/image4.jpg" class="image"><img alt="Image4" src="thumb/image4.jpg" class="thumb" border="0"/></a>
<div id="container" style="height:400px; padding-top:20px;"><img id="main" src="images/image1.jpg" /></div>

</body>
</html>


View Demo


Sunday, August 29, 2010

Wait till a dynamically loaded image fully load using jQuery

I had this problem when I was creating simple jQuery gallery with thumbs. Once the thumb is clicked I wanted to wait till the image is fully loaded until I called the fadeIn.
we can use jQuery load to achieve this.

$('#image').hide();
$('#image').attr("src", image); //next image path
//still in hide
$('#image').load(function() {    
    $('#image').fadeIn('slow');
});        


How to round numbers in JavaScript

JavaScript number rounding is done using Math.round
This will round a given number to the nearest whole number

<script type="text/javascript">

var number = 100.58;
number = Math.round(number);
alert(number); //101

</script>


If you do need some decimal points preserved, following code will round the number to the nearest tenth.

<script type="text/javascript">

var number = 100.58;
number = Math.round(number*10)/10;
alert(number); //100.6

</script>


Javascript Go Back Link With History

Here is a really easy way to add a go back link to your page navigation.
This code uses JavaScript to get the browser history for the specific site.

<a href="#" onClick="history.go(-1)">Back</a> 
<input type=button value="Back" onClick="history.go(-1)">


You can change the -1 to any number of pages you need to go back in history :D

Thursday, August 26, 2010

How to add table row (tr) using jQuery

I think the easiest method is to find the last tr and insert the new row after the last tr.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How to add table rows using jQuery</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script>

$(document).ready(function(){    
    
    $('#add').click(function(){
                
        $('#my_tbl tr:last').after('<tr><td>&nbsp;</td><td>&nbsp;</td></tr>');        
    });
});    

</script>
</head>

<body>

<table id="my_tbl" border="1" cellpadding="1" cellspacing="1">
    <tr>
        <td> Head1 </td>
        <td> Head2 </td>
    </tr>
</table>

<br />

<input type="button" id="add" value="Add Row" />

</body>
</html>

View Demo

The only place where this code does not work is when there are no tr's in the table to start with. If that is the case you can always add a dummy <tr></tr> block to the top to start with.

Please comment if you know a better solution.

Monday, August 23, 2010

Best way to get all rows in a mysql table - mysql_num_rows() or count()

Normally there are two ways to get the total number of rows in a mysql table.

$result = mysql_query("select count(id) from table");
$data = mysql_fetch_array($result);
$all_rows = $data[0];

Or

$result = mysql_query("select id from table");
$all_rows = mysql_num_rows($result);


As you have guessed the best server resource friendly method is to use mysql count() function instead of returning all the rows as a result-set to use in php mysql_num_rows() function.

Sunday, August 22, 2010

Convert html/css to pdf using php (html2pdf)

All web developers at one point will need to create pdf documents on the fly with dynamic data.
Usaully php developers go for the fpdf class. html2pdf is very effective php converter you could use in your project that convert html/css into a pdf document.

html2pdf

Wednesday, August 18, 2010

Video thumb rotator with jQuery Cycle Plugin

This is a simple code I used to rotate video thumbs which were retrieved using the YouTube API.
I use the jQuery Cycle Plugin to do the rotation on mouse over.

http://jquery.malsup.com/cycle/

Make sure you download the cycle plugin
http://jquery.malsup.com/cycle/download.html

<html>
<head>
<title>Video thumb rotator with jquery Cycle Plugin</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cycle.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    
    $('.adimagebg').cycle();
    $('.adimagebg').cycle('pause');
    
    $(".adimagebg").hover( function () {
        $(this).cycle({ 
            fx: 'fade',
            speed: 300,
            timeout:  1200
        });
    },
    function(){
        $(this).cycle('pause');
    });
});
</script>
</head>

<body>

    <div class="adimagebg">
        <a href="#"><img width="100" src="image1.jpg" border="0" /></a>
        <a href="#"><img width="100" src="image2.jpg" border="0" /></a>
        <a href="#"><img width="100" src="image3.jpg" border="0" /></a>
    </div>

</body>
</html>


View Demo

Freelance Jobs

Tuesday, August 17, 2010

How to get the totalResults from YouTube video API search

Following code will get the total number of results returned for a given search query, This will help you design custom pagination for the results.

$query = $yt->newVideoQuery();
$query->setOrderBy('updated');
$query->setSafeSearch('none');
$query->setVideoQuery('Test');
$query->setAuthor('GoogleDevelopers');
$query->setMaxResults(10);
$query->setStartIndex(1);
        
$videoFeed = $yt->getVideoFeed($query->getQueryUrl(2));

$all_results = $videoFeed->getTotalResults()->text;

Thursday, August 12, 2010

How to get a editable video entry - youtube api

I'm playing with the youtube api these days, this is another tip I think which will save you some time.

The usual getVideoEntry method will not return you a editable video entries. you must pass some extra parameters like this (3rd parameter tell to get full metadata)

$videoEntry = $yt->getVideoEntry($id, null, true);

or you can use

$entry = $yt->getFullVideoEntry($id);

Tuesday, August 10, 2010

Fatal error: Class 'Zend_Uri_Http' ZendGdata (YouTube API)

I followed the YouTube API - Developer's Guide: PHP, yet I got this error running their code, don't know why they are not updating the guide.

Fatal error: Class 'Zend_Uri_Http' not found in /home/path/public_html/ZendGdata/library/Zend/Gdata/App.php

As the error show us the fix is really simple, just load the Class 'Zend_Uri_Http'

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();

Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Uri_Http');

2010/08/14 Note: you have to load this class in your page where you call the YouTube API service (not in any zend php client library files.)

Freelance Jobs

Saturday, August 7, 2010

jQuery - Get the element Title value of a clicked item

Like the previous post you can get the title value of a element easily as well.

$('.clsname').click(function(){
     var idval = this.title;
});

jQuery - Get the element ID value of a clicked item

This is small tip that I found useful when completing a new project. As usual with jQuery everything is simple as 1 2 3

$('.clsname').click(function(){
     var idval = this.id;
});

Sunday, August 1, 2010

php post content-length exceeds - post_max_size

1) If you have access to the php.ini file there is a easy fix, just edit the post_max_size line with a higher value you want.

2) If you do not have access to the php.ini file you can create a text file and name it as php.ini (remember to check if the file extension is .ini not .txt) and put this line inside:
post_max_size = 15M
save and upload to the folder where your php script is placed.

(I have used 15M, you can put any value you want)

It is also better to have a chat with your hosting technical help team.

Saturday, July 31, 2010

How to loop through a php assoative array

Looping through a php assoative array is really easy with foreach loop.

<?php

$array = array ("Fruit" => "Apple", "Animal" => "Dog", "Number" => 1, "Name" => "Jhon" );

foreach($array as $key => $value)
{
    echo $key.': '.$value;
    echo '<br />';
}

/* result

Fruit: Apple
Animal: Dog
Number: 1
Name: Jhon

*/

?>

Thursday, July 29, 2010

jquery datepicker with current date and custom date format

I searched the net to find a more simpler solution to set the current date with jQuery UI datepicker but didn't find anything. So here is the solution I used. Mixture of both normal JavaScript and jQuery. You can also check out how to use a custom date format with jQuery UI datepicker.

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var d = year + "-" + month + "-" + day;

$('.date-pick').datepicker({ dateFormat: 'yy-mm-dd' }).val(d);

Wednesday, July 21, 2010

How to get number of characters of a String in php - strlen()

You can use strlen() to easily find the length of an php string.

<?php
$str = 'Hello World';
echo strlen($str); // 11
?>

Many new php developers make the mistake of using count() which is used to count elements in an array or properties in an object.

Saturday, July 17, 2010

Get Select Box Value - jQuery Tip

It is easy as 1,2,3 with jQuery :)

$('#selectbox').val()

Lets see how to write this with on change event

$('#selectbox').change(function(){
     alert($(this).val());
});

Lets see how to get the selected text (not the value)

$('#selectbox').change(function(){
     alert($('#selectbox :selected').text());
});

Thursday, June 10, 2010

wamp www root index.php file got deleted

I accidentally delete the default www root folder index.php for WAMP server all the time. So I keep a back up of the index.php
I uploaded my index.php if you want to restore.

http://www.mediafire.com/?yhdt1tz2cjn

Monday, May 31, 2010

Magento error after sample data import

Did you import the sample data sql file to the mysql databse before install Magento?

I imported the sample data sql after installation and spent hours looking for the error.
The Magento site says we should import it before installation but its not visible enough :D

Saturday, May 8, 2010

jquery css problem getting border width - fixed

I had problem getting the border width by calling the css() normaly like this,
$('#id').css("border-width");

I don't know why this don't work but the following works :)

$('#id').css("borderLeftWidth");