How Do You Start A Facebook Page?

Hi guys! Most of the top posts here in my blog is related to web development with Facebook. Many asks beforehand how to create a Facebook page where we will pull our data. So here’s a step by step tutorial on how to do such task.

How Do You Start A Facebook Page?
Our final output.

Photos are kinda small, you have to click each photos below to enlarge.

Step 1

Go to Facebook “Create A Page” section that can be found in this link: Create a Page. You should see this page:

Create a Facebook page.
Create a Facebook page.

Step 2

In this example, we are going to create a “Brand or Product” for our website. Click “Brand or Product” box and fill up the required filled.
On the “Category” dropdown, select “Website”.
On the “Brand or Product Name” field, I’ll enter “Mike Dalisay Works” for example.
Check “I agree to Facebook Pages Terms” checkbox.
It should look like this:

Creating a brand or product Facebook page.
Creating a brand or product Facebook page.

Step 3

Click the “Get Started” button. It will make you set up some information for your Facebook page such as profile picture, about, Facebook web address, and enable ads option. Just follow the flow.

Facebook page info set up.
Facebook page info set up.
Facebook page profile picture.
Facebook page profile picture.
Facebook page “About” info
Facebook page “About” info
Facebook web address.
Facebook web address.
Enable ads. You can click the skip button.
Enable ads. You can click the skip button.

Step 4

At this stage, you already have successfully created your Facebook page! Congrats! Now Facebook will give you some guidelines on how to use your Facebook page.

Like your own page.
Like your own page.
Invite others to like your page.
Invite others to like your page.
Do your first post!
Do your first post!

Cron Job Example with PuTTy

Today we are going to run a PHP script on schedule, in other words, this PHP script should be executed in certain period of time.

This could be any script you want, it can be used to send email to your users, make changes to your file system or database, crawl a website, etc.

Running a script on schedule has a lot of benefits because it does the job automatically, without you worrying about it.

Cron Job Example with PuTTy
Execute a program via command line

We’re gonna be able to achive this using:

  1. PuTTy - a secure shell or terminal emulator used to access remote systems over the internet.
  2. Cron - the time based job scheduler in unix-like operating systems which powers most servers today.
  3. cURL – a command line tool for getting and sending files using URL syntax. This is usually installed already in your server.

You can skip step 1, 2 and 3 if you know how to log in using PuTTy.

Step 1
If you don’t have PuTTy in your computer, you can download it here, choose putty.exe if you’re on windows.
Step 2
Run putty.exe, it will look like this:

Cron Job Example with PuTTy
  • Arrow # 1 is where you’re gonna put your host name or IP address of your server.
  • Arrow # 2 is the button you’re gonna click (or you can simply press enter) immediately after entering your server host name of IP address.

Step 3
You’ll be asked to enter your server username and password. It looks like this:

2

Step 4
You should be in the crontab, type: crontab -e and press enter. By the way, a crontab is a simple text file in your server with a list of commands meant to be run at specified times, more info here.

3

It will list the the cron jobs (not yet editable)

4

Step 5
To make it editable, you should press the “insert” button on your keyboard.

5

Now you can edit your cron jobs or schedules, there are so many tutorials on how to construct a cron job, you can find some here and here.

On my example above, the cron runs two php script every one minute via cURL. A brief explanation:

#  MIN  HOUR  MDAY  MON  DOW  COMMAND
*/1   *   *   *   *   curl http://mydomain.com/myphp_script.php

MIN – Minute 0-60
HOUR - Hour [24-hour clock] 0-23
MDAY - Day of Month 1-31
MON - Month 1-12 OR jan,feb,mar,apr…
DOW - Day Of Week 0-6 OR sun,mon,tue,wed,thu,fri,sat
COMMAND - Command to be run Any valid command-line

Step 6
After you’re done editing, press the “Esc” key on your keyboard to exit from the edit mode. It will again look like this:

4

Step 7
To exit cron tab, you should type: :wq and press enter.

6

Then it will look like this:

8

That’s it! Our PHP scripts will be executed every one minute!

Past Month Traffic Details By Google Analytics

Hi guys! Earlier, I (accidentally) checked out our website’s Google Analytics account. I was surprised and happy about what I saw. 

The Stats

For the past month, we got almost 12 thousand unique visitors, more than 14 thousand visits and more than 22 thousand page views. For me these statistics are very encouraging. I realized that I can help other people by writing something about programming. I feel like today, I’m realizing my dream of being a teacher in the future.

The Sources

Most of the traffic we got are from search engines, so I thank Google, Bing, Yahoo Search and the like for bringing more people to our website. Second source of traffic are from referral links, so I thank those people who liked, tweeted, shared and linked our posts to Facebook, Twitter, Stackoverflow, DZone, Hacker News and other websites that are linked to our place. Third source of traffic is the direct traffic, thanks for bookmarking, subscribing and returning here guys!


The New Perspective

Since I’m encouraged and inspired by your positive feedbacks, I will blog more high quality contents (Hmm, maybe as often as every week?) that can be useful for different level of programmers and not just short code snippets or quick tips. I will still reply to your comments depending on my available time. Yes, I’m also aware of negative feedbacks on this site but I prefer to look at it in a positive way. For me, negative feedbacks are great opportunities to learn something. 

Regarding code snippets or quick tips or fast posts, I’d still love to blog about it. I believe they are helpful too. Little code blocks with a little story or experience. I’ll blog these things on a different website or sub domain, something like php.codeofaninja.comjquery.codeofaninja.com or android.codeofaninja.com. I just want to put more high quality posts as much as possible on our main website codeofaninja.com 

Again, thanks guys! See you!

Verify if Java was Installed in Ubuntu 11.10

Ubuntu Quick Tip: After my JDK installation, I wanted to verify if it was really installed in my computer. Here's how I verified it

1. Run your terminal.

2. Type:

$ sudo updatedb
$ locate java

  • It asked me to enter my password after typing sudo updatedb.
  • updatedb means you are updating the database for locate or locating your app.
  • locate finds files by name.
Verify if Java was Installed in Ubuntu 11.10
Click to enlarge.

3. If it returns something like this:

Click to enlarge.

it means JDK was installed.

PHP: Get Current Domain Name

PHP Quick Tip: I used this code when I was suddenly just got into a project and too lazy to meet and greet its configuration file to see how the current domain name was defined. I had to provide a link in a page but I don’t want it to be hard coded like this:

<a href=“http://somehardcodeddomain.com/users/add/”>
     The Link
</a>

This is because there are instances when the client wanted to change their domain name. So if that happens, to prevent re-coding links like that, we have to get current domain name programatically:

<a href=”http://<?php echo $_SERVER['HTTP_HOST']; ?>/users/add/”>
     The Link
</a>

Get DIV height in JavaScript

I’m going to show you how to get DIV height in JavaScript.

I was in a situation when I have to animate a div by maintaining/grabbing/pausing its current height first but its height is not specified in the CSS and is dynamic.

Anyway, this tutorial will focus on how to get the dynamic div’s height.

Here’s a live demo and script:

View Live Demo here
<html>
    <head>
        <title>jquery get dynamic div height</title>
        <!– just some styles –>
        <style type=‘text/css’>
        #div_1{
           width:150px;
            border:thin solid red; 
            float:left;
        }
        .btn{
            padding:10px; 
        }
        </style>
    </head>
<body>
 
<!– the div with some contents in which we will get the height –>
<div id=‘div_1′>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis
dapibus, mauris ac feugiat bibendum. 
</div>
 
<!– onClick of this button, some text will be added and display the div_1 height –>
<input type=‘button’ class=‘btn’ id=‘add_text’ value=‘Add Text and Get New Height’ />
 
<!– This will display the current height –>
<div id=‘result’></div>
 
<script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”>
</script>
<script type=‘text/javascript’>
$(document).ready(function() {
    //load the current height
    get_div_height();
 
    $(“#add_text”).click(function(){
        //append the some text to change current height
        $(“#div_1″).append(“The Code of a Ninja.”);
         
        //load the new height
        get_div_height();
    });
 
    //function to get current div_1 height
    function get_div_height(){
        var div_height = $(“#div_1″).height();
        $(“#result”).html(“div height is: “ + div_height + ” px”);
    }
     
});
</script>
 
</body>
</html>

You can also get your browser’s current window height by doing:

    $(window).height()

and your document’s height (height of html document) by:

    $(document).height()

more info on this link. :)

Paginating, Sorting and Displaying Data with CakePHP

Updated last July 15, 2013: CakePHP 2.x Pagination Tutorial: Helper, Conditions, Limit, Sorting and More!

Today I’m going to show you how easy it is for CakePHP to do pagination, sorting and displaying data from the database (I’m using MySQL). Pagination is useful if you have many rows of data, image if you have thousands of records, your page would load slow and it will be very inconvenient for your users to browse that page. Sorting is useful if you want to view your data in alphabetical, ascending or descending order.

We will have our table look like this:

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(32) NOT NULL,
  `lastname` varchar(32) NOT NULL,
  `email` varchar(32) NOT NULL,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
)

So in our app/models/user.php we will have something like this:

<?php
class User extends AppModel{
    var $name = ‘User’;
?>

A function on our app/controllers/users_controller.php:

    //for this example, we display two records per page
    $this->paginate = array(
        ‘limit’ => 2
    );
     
    //we will set it to users variable for the view
    $this->set(‘users’, $this->paginate(‘User’));
}

Our view file on app/views/users/view_users.ctp:

<?php
echo “<div class=’page-title’>Users</div>”; //title
//this ‘add new user’ button will be used for the next tutorial
echo “<div style=’float:right;’>”;
    $url = “add/”;
    echo $form->button(‘Add New User’, array(‘onclick’ => “location.href=’”.$this->Html->url($url).“‘”));
echo “</div>”;
echo “<div style=’clear:both;’></div>”;
 
if( sizeOf( $users ) > 0 ){ //check if there are user records returned
?>
<table>
    <tr>
        <!–
            Here on the table heading (<th></th>) is where our SORTING occurs,
            User has to click heading label to sort data in ascending or descending order,
            $paginator->sort(‘Firstname’, ‘firstname’); is a CakePHP function that builds the link for sorting
            the first parameter ‘Firstname’ will be the label 
            and the second parameter ‘firstname’ is actually the database field
        –>
        <th style=’text-align: left;’><?php echo $paginator->sort(‘Firstname’, ‘firstname’); ?></th>
        <th><?php echo $paginator->sort(‘Lastname’, ‘lastname’); ?></th>
        <th><?php echo $paginator->sort(‘Email’, ‘email’); ?></th>
        <th><?php echo $paginator->sort(‘Username’, ‘username’); ?></th>
        <th>Action</th>
    </tr>
    <tr>
    <?php
        foreach( $users as $user ){ //we wil loop through the records to DISPLAY DATA
            echo “<tr>”;
                echo “<td>”;
                    //$user is our foreach variable
                    //['User'] is from our model/alias
                    //['firstname'] is the database field
                    echo “{$user['User']['firstname']}“;
                echo “</td>”;
                echo “<td>{$user['User']['lastname']}</td>”;
                echo “<td>{$user['User']['email']}</td>”;
                echo “<td>{$user['User']['username']}</td>”;
                echo “<td style=’text-align: center;’>”;
                    //’Edit’ and ‘Delete’ link here will be used for our next tutorials
                    echo $html->link(‘Edit’, array(‘action’=>‘edit/’.$user['User']['id']), null, null);
                    echo ” / “;
                    echo $html->link(‘Delete’, array(‘action’=>‘delete/’.$user['User']['id']), null, ‘Are you sure you want to delete this record?’);
                echo “</td>”;
            echo “</tr>”;
        }
    ?>
    </tr>
</table>
 
<?php
    //here is our PAGINATION part
    echo “<div class=’paging’>”;
 
    //for the first page link
    //the parameter ‘First’ is the label, same with other pagination links
    echo $paginator->first(‘First’);
    echo ” “;
     
    //if there are previous records, prev link will be displayed
    if($paginator->hasPrev()){
        echo $paginator->prev(‘<<’);
    }
     
    echo ” “;
    //modulus => 2 specifies how many page numbers will be displayed
    echo $paginator->numbers(array(‘modulus’ => 2)); 
    echo ” “;
     
    //there are next records, next link will be displayed
    if($paginator->hasNext()){ 
        echo $paginator->next(‘>>’);
    }
     
    echo ” “;
    //for the last page link
    echo $paginator->last(‘Last’);
     
    echo “</div>”;
     
}else{ //if there are no records found, display this
    echo “<div class=’no-records-found’>No Users found.</div>”;
}
 
?>

Our output will look like this:

Paginating, Sorting and Displaying Data with CakePHP
cakephp-pagination-page-2

Data is displayed, you can click table heading link for sorting and page links are on the bottom part of the page. That’s it!

CakePHP: Access Current Controller, Action and Parameters

Hi guys, today I’m going to show you how to access or get CakePHP’s current page controller, action and parameters. I found this useful when high lighting certain elements of a page for example are tabs, sidebars, etc. that tells the user what part of the system or website they are currently in.

CakePHP: Access Current Controller, Action and Parameters

For instance we will have the following URL:

http://somedomain.com/users/edit/27/

Based on that URL, our controller is users. To access it we will have:

 $current_controller = $this->params['controller'];

Our action is edit and we will access it this way:

 $current_action = $this->action;

Our first parameter is 27, which is the ID of the record to be edited. We can access it by doing:

$user_id = $this->params['pass'][0];

In case that we have second parameter, we have to access it this way:

  $second_parameter = $this->params['pass'][1];

For the third parameter:

$third_parameter = $this->params['pass'][2];

and so on…

You can do these codes either in your controller or view files.