Monthly Archives: July 2009

Javascript code to prompt delete

This simple javascript code can be used to prompt user whether they want to proceed with an action or not. Usually be used when user click on delete or remove button/link.

The javascript code

1
2
3
4
5
6
7
8
9
10
11
12
<script LANGUAGE="JavaScript">
<!--
function confirmSubmit()
{
var agree=confirm("Are you sure you want to DELETE this record?");
if (agree)
	return true ;
else
	return false ;
}
// -->
</script>

The HTML code

1
<a href="?act=delete&id=1234" onClick="return confirmSubmit()">Delete</a>

PHP code for xml parser (rss)

I’ve been looking for PHP code to read RSS feed from blog. It is a simple XML reader code actually and can use a ready class available on the net.

I found this class magpierss which is quite easy to use. You just need to include one file in your code and that one file will call few other library files.

The PHP code you need to use to get the XML data is just like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
 
define('MAGPIE_DIR', 'rssreader/');
require_once(MAGPIE_DIR.'rss_fetch.inc');
 
$url = 'http://www.azwan.net/blog/feed/';
 
if ( $url ) {
	$rss = fetch_rss( $url );
	echo "Channel: " . $rss->channel['title'] . "<p>";
	echo "<ul>";
	foreach ($rss->items as $item) {
		$href = $item['link'];
		$title = $item['title'];
		echo "<li><a href=$href>$title</a></li>";
	}
	echo "</ul>";
}
?>

Download code

A dynamic header

This is a dynamic header if you use include ‘header.php’ in your programming.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
echo '<!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=iso-8859-1" />';
 
// set title
if ($title == '')
	$title = 'My home page'; // default title
$title .= ' - mydomain.com'; // append branding (e.g. your domain name)
echo '<title>'.$title.'</title>';
 
// set description if any
if ($description != '')
	echo '<meta name="description" content="'.$description.'" />';
 
// set keyword
if ($keywords != '')
	echo '<meta name="keywords" content="'.$keywords.'" />';
 
// insert css if any
if(!empty($css)){
	foreach($css as $item){
		echo '<link rel="stylesheet" href="'.$item.'" type="text/css" />';
	}
}
 
// can have static if only one css available
echo '<link rel="stylesheet" href="style.css" type="text/css" />';
 
// insert javascript if any
if(!empty($js)){
	foreach($js as $item){
		echo '<script language="javascript" src="'.$item.'"></script>';
	}
}
 
echo '</head>';

From the file you include, you can set something like this

1
2
3
4
5
6
7
8
9
	//set variables
	$title = 'My home page';
	$description = '';
	$keyword = '';
	$js = array('formvalidation.js','datecalc.js'); //include javascript
	$css = array('style.css','form.css') // include css
 
        //include header
	include 'header.php';

Basic CSS template

This CSS can be used as default css for your website. It contains most standard HTML tags. Can further edit from here. I’ll add more common tags in future. Please let me know if I missed any other important HTML tags

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
body {
margin:0;
}
 
body,p,td,input,select,div {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:13px;
}
 
h1 {
font-size:200%;
}
 
h2{
font-size:180%;
}
 
hr {
height:1px; border:#CCCCCC thin solid;
}
 
a:link {color: blue; text-decoration: none; }
a:visited {color: blue; text-decoration: none; }
a:hover {color: blue; text-decoration: underline; }
a:active {color: blue; }
 
.linethrough { text-decoration:line-through; }
.underline { text-decoration:underline; }
.smallfont { font-size:80%; }
.smallerfont  { font-size:70%; }
.highlight { background-color:#FFFF00; }

Content is king. Besides that?

You have heard in internet business content is king. What come after the content in internet business then?

The answer would be the community (loyal visitors that keep coming back and participate).

To come out with a successful website, you need both content and community. To make it even successful, you need to educate the community to create content on your website. This is what big websites do. For example youtube.com, wikipedia.org, facebook.com, digg.com, rapidshare.com and many more.

If the community do not create the content, you need to find other means of adding massive contents on your site as strong reasons for people to come back. This model used by google and other search engines that use so called crawler to go and bring back data from millions of websites on the net.

So always remember to create a successful website you need CONTENT + COMMUNITY.

PHP code to read and display a text file line by line

If you have a text file and want to read it line by line, you can use the fgets function. This can be useful even if you have a very large files.

1
2
3
4
5
6
$handle = fopen($filename, 'r');
while (!feof($handle)) {
        $buffer = fgets($handle);
        echo $buffer;
        echo '<br />';
    }

Alternatively you can output the file to an array first and display it one at a time. This method is not suitable for a large file.

1
2
3
4
5
6
7
8
$file_array = file($filename);
foreach ($file_array as $line_numbers=>$line)
{
 echo $line_numbers+1; //add 1 because array numbers start with 0
 echo '. ';
 echo $line;
 echo '<br />';
}

How to remove wuauclt in your processes list

I had problem with my other PC (windows xp) that ran very slow. I checked the processes running and found this wuauclt program is running. Tried to kill it but it reappear again.

I did some research on the net and found that the file is windows file for auto update and can’t be removed by kill it in the process list.

The solution is logon your PC via safe mode, find file named wuauclt.exe in c:\windows\system32. Rename it to another file name like wuaucltxxxx.exe. Then restart your PC as normal.

How to get more from Adsense?

Always been thinking how to improve Google Adsense income but the key things are still back to these 3 criterias:

1. Get more traffic (impression)

– use what ever medium you can think of including video marketing, search engine marketing, social networking (like fb,twitter), social bookmarking (like digg,delicous), article directory, press release, special report, your opt-in list, link from your other websites, your  favorite forums, blog comments, IM status etc

2. Get more clicks (conversion)

– positioning (must be near to main content)
– color scheme (either very same to your color scheme or totally contra)
– ad size – use large ones (728×90,300×250)

3. Target for high-priced ads (value)

– niche with high profit margin, advertiser willing to pay more in advertising if they get more in profit (high margin business)

So what ever you do, don’t forget these 3. Forget about those people selling ‘secrets’ because they will tell all these 3 again with slightly different context. Spare your money to buy some good tools and pay for marketing.

SQL statement to list duplicate contents

You will need this SQL statement to list all duplicate contents in a table

SELECT email,
COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )

source

If you want to list all duplicate records

SELECT t1.* FROM
`some_table` t1,
( SELECT documentno, COUNT(1) AS dup
  FROM `some_table`
  GROUP BY documentno
  HAVING dup > 1 ) AS t2
WHERE t1.documentno = t2.documentno;

Then how to remove the duplicate records?

1. Create new table and insert clean records (without duplicate)

CREATE TABLE new_table AS
SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];

Note: Be careful, this will create a table without primary key, auto-increment etc. So make sure you update your table after run this statement

2. Drop old table (with duplicates)

source

How to backup MySQL table in phpMyAdmin

Sometimes when you are going to execute an SQL statement to your production database, you need to be very careful especially when you are using DELETE or UPDATE command.

To ensure your original data will be available, you can backup the whole table that you are going to do the operation.

To backup the whole table is very easy in phpMyAdmin (even in other SQL tool).

phpmyadmin

After you done with the operation and it is successful, you can easily drop the temporary backup table.