Archive

Archive for the ‘Programming’ Category

Open/Edit .ctp with Dreamweaver CS4

September 3rd, 2010

CakePHP 1.3 default file extension for the views template are in .ctp

If you open up .ctp files with Adobe Dreamweaver CS4, you will noticed that all the codes are in black and white. To make Dreamweaver show the color syntax of PHP & HTML in .ctp files, do the following:

- Navigate to Dreamweaver CS4 Installation folder/configuration/DocumentTypes
- Open up MMDocumentTypes.xml

Search for:

1
winfileextension="php,php3,php4,php5"

Replace with:

1
winfileextension="php,php3,php4,php5,ctp"

Restart your Dreamweaver CS4 if it is running. Open up again and you will notice that .ctp files now has the same color syntax as .php files.

Willie Programming, Software , , ,

Removed Dotted Line around image

September 2nd, 2010

If you would like to remove this irritating dotted line when users click and hold the image, just do the following on your stylesheet in CSS:

1
2
3
a {
    outline: none;
}

Easy !~ But this would also means it will remove dotted line around all hyperlinks (including text hyperlinks) unless you define a class for it:

1
2
3
a.YOURCLASS {
    outline: none;
}

Willie Programming , ,

Shutdown/Restart/Log Off in Windows

August 27th, 2010

Need a icons to do the above in Windows OS?

Easy! Just create a .bat file and fill in the following:

For Shutdown:

1
shutdown.exe -s -t 00

For Reboot:

1
shutdown.exe -r -t 00

For Logoff:

1
shutdown.exe -l

For Abort Shutdown:

1
shutdown -a

Take note that the -t parameter is the number of seconds Windows take to perform this task. For instance if shutdown.exe -s -t 3600, Windows will takes 1 min before it shut down.

Hope it helps :)

Willie Programming, Software

Deprecated functions in CodeIgniter

July 28th, 2010

Since PHP 5.3 was released to the public, its introduces two new error levels: E_DEPRECATED and E_USER_DEPRECATED.

The E_DEPRECATED error level is used to indicate that a function or feature has been deprecated. The E_USER_DEPRECATED level is intended for indicating deprecated features in user code, similarly to the E_USER_ERROR and E_USER_WARNING levels.

Henceforth, a lot of old PHP 4 functions are being deprecated, superseded by newer functions. Apparently, CodeIgniter 1.7.2 or older faced this irritating notice errors.

You may faced errors like such in the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Deprecated: Assigning the return value of new by reference is deprecated in C:\XXXXX.php on line 130
 
Deprecated: Assigning the return value of new by reference is deprecated in C:\XXXX.php on line 136
A PHP Error was encountered
 
Severity: 8192
 
Message: Function set_magic_quotes_runtime() is deprecated
 
Filename: codeigniter/CodeIgniter.php
 
Line Number: 60
A PHP Error was encountered
 
Severity: 8192
 
Message: Assigning the return value of new by reference is deprecated
 
Filename: libraries/Loader.php
 
Line Number: 255

To resolve these issues, just get the latest version of CodeIgniter (still version 1.7.2) from the SVN or re-download it @ the downloads page. Somehow, those deprecated functions were being replaced/fixed.

Willie Programming , ,

Unlink in CakePHP

July 9th, 2010

unlink in CakePHP 1.3 doesn’t seem to work perfectly. You will always receive Warning error (2) due to “No such file or directories”.

Fret not! Allow me to introduce the class File in CakePHP 1.3

Apparently, little documentation, examples or tutorials have surfaced for this File Class. But, we shall make use of this to delete file/directories in CakePHP 1.3

For instance, you would like to delete butterfly.jpg under YOURWEBROOT/img folder. First, you construct a File object:

$file = new File(WWW_ROOT ."/img/butterfly.jpg");

As you can see, we make use of CakePHP WWW_ROOT to define the directory instead of using $this->webroot. This is because the file are store using system directories. By using webroot directories, CakePHP will output “No such file or directories” error again.

Example output of WWW_ROOT: E:\xampp\htdocs\cake13\app\webroot\
Example output of $this->webroot: \cake13\

Last but not least, to delete butterfly.jpg, we do the following:

if($file->delete()){
   echo "file deleted successfully";
}else{
   echo "file failed to be delete";
}

$file->delete() return true when the file has successfully been deleted. Hence, we check for successions of file deletion using the if statement as shown above.

I hope this tutorial helps ! :D

Willie Programming , , , ,

Javascript in CakePHP 1.3

July 8th, 2010

The newly updated version of CakePHP in version 1.3 packs a punch! It is now more easier to code as the Helper methods are better organized now.

For instance, linking external javascript in CakePHP 1.2 is quite an hassle as Javascript itself has Javascript Helper. However, The Javascript Helper is deprecated in 1.3 and will be removed in future versions of CakePHP. Henceforth, lets look at how HTML Helper load javascript.

In CakePHP 1.2, to load an external javascript, we do this:

<?php echo $javascript->link("jquery", true); ?>

Now in CakePHP 1.3, we can make use of HTML Helper, we do the following:

<?php echo $html->script("jquery"); ?>

Nevertheless, if you have multiple external js file to load, you can load it into an array:

<?php echo $html->script(array('jquery','wysiwyg','scripts')); ?>

Easy and more organized now eh? External CSS files were all along being load using HTML Helper. Now with the loading of Js files through HTML helper instead of invoking Javascript Helper, it is much more convenient! Moreover, you will only need to load one helper which is HTML Helper instead of loading 2. :D

Willie Programming , , ,

Dropdown menu for WordPress Archives

June 20th, 2010

As your wordpress ages on, the archives list is getting longer and longer. This doesn’t look neat and tidy at all. Hence, just replace the following code:

Original:

1
2
3
<ul>
   <?php wp_get_archives('type=monthly'); ?>
</ul>

Replace with:

1
2
3
4
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
   <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> 
   <?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> 
</select>

Disclaimer :: The original archive code should be written in most wordpress theme. Just replace it and you will get:

For more template tags and regarding archives, do refer to http://codex.wordpress.org/Template_Tags/wp_get_archives

Willie Programming, Software

Missing Database Table in Cake PHP

June 4th, 2010

Sometime if you encounter “Missing Database Table” from CakePHP errors, it might not really be due to missing of some database tables.

What you need to do is simple. Clear the cache folders found in CakePHP. The directory should be /app/tmp/cache.

Clear (delete) all files found in these 3 folders:
- models
- persistent
- views

By right, we should only clear the files in models folder. Just to be on the safe side, I strongly recommend clearing all these 3 folders.

Nevertheless, if you are uploading CakePHP files after development baking from your localhost to your actual web host, you could also faced “Missing Views” error and etc. Just clear the cache in those 3 folders will do.

Hope this solves your problem :)

Willie Programming , , , ,

Model View Controller

May 12th, 2010

MVC Framework. The first time when I heard from my ming-en chan, I don’t even understand what the shit it is about.

However once I have enlightened, MVC actually helps to make coding easier and neater. The diagram below describe how the Model-View-Controller works:

Model: Render and process data with database
View: The interface which the user will see on the web browser
Controller: Contain business logic and handle request from the user

Source: Many thanks to Ming-en chan and myself for coming up with this splendid diagram during our FYP days. Good old school days. Miss it very much =(

Willie Programming , , , , ,

connecting C# to MS Access

May 10th, 2010

To connect C# to Microsoft Access Database, we need to make use of OLEDB class. Below is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Data.OleDb;
 
class Test
{
    static void Main(string[] args)
    {
        //create the database connection
            OleDbConnection Connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\database.mdb");
 
            //create the command object and store the sql query
            OleDbCommand Command = new OleDbCommand("SELECT rank, name FROM EMPLOYEE WHERE Employee_ID = 99999", Connection);
 
            Connection.Open();
 
           //create the datareader object to connect to table
            OleDbDataReader Reader = Command.ExecuteReader();
    }
}

Lets say if we decided to display the query result into some textbox, we do the following:

1
2
3
4
5
6
7
8
9
10
           //Iterate through the database
           while(Reader.Read())
           {
                //Assuming the first column data is a string
                name.Text = Reader.GetString(0); 
                //Assuming the second column data is a integer
                age.Text = Reader.GetInt32(1); 
           }
           //Close the database connection
           Connection.Close();

Simple eh? :D Hope it helps!

Willie Programming , , ,