A New Beginning

Posted: August 24th, 2009 | Author: admin | Filed under: Personal | 4 Comments »

I just started on my new job at a startup company here in Cebu City. I’m excited on this new opportunity to work with a new team and deal with new projects.


Using Zend Framework’s Paginator Library

Posted: June 23rd, 2009 | Author: admin | Filed under: PHP | Tags: , , | No Comments »

Here’s a tutorial on how to use ZF’s paginator library.

Inside your controller:

    public function displayResultsAction() {
        // First condition gets filtered info from a form and passes it to the model
        if($this->_request->getPost('Submit')) {
            $dept_code= $this->_request->getPost('dept_code');

            $results = $this->report_model->getReports($dept_code);
      	} elseif($this->_request->getParam('dept_code')) {
        // Get the filtered info from the url params being passed by zend paginator
       	    $dept_code= $this->_request->getParam('dept_code');

       	    $results = $this->report_model->getReports($dept_code);
       	} else {
            // do anything here...
            // I just display the default report results
            $dept_code = '';

       	    $results = $this->report_model->getDefaultReports();
        }

        $page = $this->_request->getParam('page', 1);

        $paginator = Zend_Paginator::factory($results);
        //Set item count per page
        $paginator->setItemCountPerPage(20);

        $paginator->setCurrentPageNumber($page);

        $this->view->results = $paginator;
        $this->view->dept_code = $dept_code;
    }

Inside your results view page:

<!-- html code here -->

<table class="view" cellpadding="3" cellspacing="0">
    <thead>
        <tr>
            <th width="100">Id #</th>
            <th width="150">Dept Code</th>
            <th width="200">Department</th>
            <th width="200">Project Name</th>
        </tr>
    </thead>
    <tbody>
    <?php   foreach($this->results as $row) { ?>
        <tr class="view-details">
            <td><?=$row->Id?></td>
            <td><?=$row->DeptCode?></td>
            <td><?=$row->DeptName?></td>
            <td><?=$row->ProjName?></td>
        </tr>
    <?php   }    ?>
    </tbody>
</table>
<br>
<!-- specify path of pagination control under your views in the 3rd param -->
<!-- Adding extra params in the url is specified on the 4th function param here -->
<?=$this->paginationControl($this->results, 'Sliding', 'reports/results-pagination.phtml', array('dept_code' => $this->dept_code))?>

<!-- html code here -->

Inside your pagination file view:

<?php if ($this->pageCount): ?>
<div id="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<?php   if($this->dept_code == '') { ?>
    <a href="<?= $this->url(array('page' => $this->previous)); ?>">&lt; Previous</a> |
<?php   } else { ?>
   <a href="<?= $this->url(array('page' => $this->previous, 'dept_code' => $this->dept_code)); ?>">&lt; Previous</a> |
<?php   } ?>
<?php else: ?>
  <span class="disabled">&lt; Previous</span> |
<?php endif; ?> 

<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
    <?php if ($page != $this->current): ?>
    <a href="<?=$this->baseUrl()?>/reports/display-results?page=<?=$page?><?=($this->dept_code!= '') ? '&dept_code=' . $this->dept_code: ''?>"><?= $page; ?></a> |
    <?php else: ?>
    <?= $page; ?> |
   <?php endif; ?>
<?php endforeach; ?>

<!-- Next page link -->
<?php if (isset($this->next)): ?>
  <a href="<?=$this->baseUrl()?>/reports/display-results?page=<?=$this->next?><?=($this->dept_code != '') ? '&dept_code=' . $this->dept_code: ''?>">Next &gt;</a>
<?php else: ?>
    <span class="disabled">Next &gt;</span>
<?php endif; ?>
</div>
<?php endif; ?>

Get value of radio button using jQuery

Posted: June 8th, 2009 | Author: admin | Filed under: Javascript | Tags: , | 2 Comments »

Here’s a tutorial on how to get the value of a checked or selected radio button out of a group of radio buttons:

...
<input type="radio" name="sample" value="1" />
<input type="radio" name="sample" value="2" checked="checked" />
<input type="radio" name="sample" value="3" />
...

//javascript code

<script type="text/javascript">
<!--
    // displays the selected radio button in an alert box
    alert($('input[name=sample]:checked').val())
-->
</script>

Translate timestamp into words using PHP

Posted: May 28th, 2009 | Author: admin | Filed under: PHP | Tags: , | No Comments »

Here’s a nifty class I use for getting the date difference and translating them into words.

 class Date  {

    	public static function date_diff($dateTimeBegin,$dateTimeEnd) {

    		$dateTimeBegin = @strtotime($dateTimeBegin);

    		if($dateTimeBegin === -1) throw new InvalidDate("$dateTimeBegin: invalid date");

    		$dateTimeEnd=@strtotime($dateTimeEnd);

    		if($dateTimeEnd === -1) throw new InvalidDate("$dateTimeEnd: invalid date");

    		$dif=$dateTimeEnd - $dateTimeBegin;

    		if ($dif == 0) return "< 1 second";

    		if ($dif == 1) return "1 second";

    		$return_val = null;
    		$weeks      = floor($dif / 604800);

    		if ($weeks > 0) {
    			$dif -= $weeks * 604800;
    			$return_val .= "$weeks week".(($weeks>1)?'s':'');
    		}

    		$days = floor($dif / 86400);

    		if ($days > 0) {
    			$dif -= $days * 86400;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$days day".(($days>1)?'s':'');
    		}

    		$hours = floor($dif/3600);

    		if ($hours > 0) {
    			$dif -= $hours * 3600;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$hours hour".(($hours>1)?'s':'');
    		}

    		$minutes = floor($dif/60);

    		if ($minutes > 0) {
    			$dif -= $minutes * 60;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$minutes minute".(($minutes>1)?'s':'');
    		}

    		$seconds = $dif;

    		if ($seconds > 0) {
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$seconds second".(($seconds>1)?'s':'');
    		}

    		return $return_val;
    	}

    	public static function format_date($date) {
    	    $return_val = null;
    		$weeks      = floor($date / 604800);

    		if ($weeks > 0) {
    			$dif -= $weeks * 604800;
    			$return_val .= "$weeks week".(($weeks>1)?'s':'');
    		}

    		$days = floor($date / 86400);

    		if ($days > 0) {
    			$date -= $days * 86400;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$days day".(($days>1)?'s':'');
    		}

    		$hours = floor($date/3600);

    		if ($hours > 0) {
    			$date -= $hours * 3600;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$hours hour".(($hours>1)?'s':'');
    		}

    		$minutes = floor($date/60);

    		if ($minutes > 0) {
    			$date -= $minutes * 60;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$minutes minute".(($minutes>1)?'s':'');
    		}

    		$seconds = $date;

    		if ($seconds > 0) {
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$seconds second".(($seconds>1)?'s':'');
    		}

    		return $return_val;
    	}
    }

Adding security to your SVN directory using .htaccess

Posted: May 25th, 2009 | Author: admin | Filed under: Server Related Stuff | No Comments »

If you are familiar with SVN, your directory tree of your project or website would be populated by .svn folders. These folders are a security vulnerability since this is where you keep your file revisions and other information regarding the structure and layout of your website. Here’s a simple solution made by Adam Gotterer that protect and secure those directories by using .htaccess:

RewriteRule (\.svn)/(.*?) - [F,L]

Another good tip is to use SVN export instead of a checkout or rsync.