Pages

Sunday, June 9, 2013

Cakephp/PHP-Maintaing Pagination id's in sequance for each page

 Cakephp/PHP-Logic,Setting Pagination id's in sequance

 $paginationdata=$this->Paginator->params();  
 $currentpage=$this->Paginator->current();  
 foreach($data as ($key=>$value))  
 {  
   echo ($paginationdata['limit']*($currentPage-1))+$key+1;  
 }  
Now for any page id's will start according to selected page for example for first page on a limit 5...1,2,3,4,5 on second page limit 5.....6,7,8,9.10

PHP-How to generate Youtube like video url's



 <?php  
 $encoded=base64_encode(Uniqid().$myVideosValue['Uservideo']['id']);  
 ?>  


and then send this along with URL and at receiving end decode it as follows

 <?php  
 $decoded= substr(base64_decode($paramstr),13);  
 ?>  




Cakephp-Convert time from ffmpeg extension to required format

Recently I used ffmeg to encode videos to flv format in one of my projects.

You need to include its php extension to perform the same or you can use the software too..I will discuss the same in some lator post..

Meanwhile after encoding videos it was returning me its time in following format

"00:04:43.77"

where as client asked me to change the same to

"00:04:43" i.e, to eliminate the millisecond part..


I used

 <?php  
 $vidlen=new DateTime($myvideolength);  
 echo "converted time=";  
 $vidlen->format("H:i:s");  
 ?>  

PHP-get User Browser

 <?php  
 function get_user_browser()  
 {  
   $u_agent = $_SERVER['HTTP_USER_AGENT'];  
   $ub = '';  
   if(preg_match('/MSIE/i',$u_agent))  
   {  
     $ub = "ie";  
   }  
   elseif(preg_match('/Firefox/i',$u_agent))  
   {  
     $ub = "firefox";  
   }  
   elseif(preg_match('/Safari/i',$u_agent))  
   {  
     $ub = "safari";  
   }  
   elseif(preg_match('/Chrome/i',$u_agent))  
   {  
     $ub = "chrome";  
   }  
   elseif(preg_match('/Flock/i',$u_agent))  
   {  
     $ub = "flock";  
   }  
   elseif(preg_match('/Opera/i',$u_agent))  
   {  
     $ub = "opera";  
   }  
   return $ub;  
 }  
 ?>  

Cakephp-using Timthumb in cakephp

Using TimThumb in cakephp

Timthumb library is used to generated required sized images on the fly..it produces images of required size which are stored in its cache as such page is loaded much faster...

Timthumb can be easily downloaded from

http://timthumb.googlecode.com/svn/trunk/timthumb.php

Download the file and place it in

/webroot/files/timthumb.php

create a folder called cache with permissions "777" under the same folder.

now use the following code to echo images via timthumb

 echo $this->Html->image('/files/timthumb.php?src='.WWW_ROOT.'/img/imgname.jpg&w=63&h=63');  


this will convert any size image to 63X63 on the fly...you see more options of this library to make a better use of the same.

Cakephp-Send Multiple elements in ajax request frpm ajaxhelper/jshelper

From the following post

http://itfeast.blogspot.in/2013/06/cakephp-20-create-0bserve-field.html

To send multiple elements in ajax request from ajaxhelper/jshelper

 'data'=>$(\'#category,#sort\');  

For example this is how I passed the same in cakephp
  <script type="text/javascript">  
            <?php   
              echo $this->Js->get('#category')->event('change',$this->Js->request(  
                  array('controller' => 'contributors', 'action' => 'changevideos'),  
                  array('update'=>'#vediocontainer','async' => true,'dataExpression' => true,'method' => 'post','data'=>'$(\'#category,#sort,#fileid\').serializeArray()')  
                  ),false);  
             ?>  

Cakephp 2.0-Create 0bserve field equivalent with new jshelper

Many things have changed in v2.0 of cakephp removal of ajaxhelper is one of them in its plce we have a new helper called jshelper() to perform similar tasks here is how to use observe field with jshelper in cakephp 2.x


 <?php  
 echo $this->Js->get('#category')->event->('change',$this->JS->request(array(  
 'controller'=>'contributers',  
 'action'=>'changevideos'),  
 array(  
 'update'=>'#videocontainer',  
 'async'=>true,  
 'dataExpression'=>true,  
 'method'=>Post,  
 'data'=>$(this).serialize()'),  
 false))  
 ?>   

Where #category is the id of element which fires event and #videocontainer is the id of div which is updated of data from controller "contributors" and its action "changevideos"

Cakephp-Sort by custom made field in MYSQL not working in pagination

Recently in one of my cakephp projects.I came across a issue in pagination and sorting of data on basis of a custom made field..

Case:
I had a MYSQL field "video_length" in STRING which should have been in TIME(DB was not created by me)...

My requirement was sort on basis of this field so in my find function of Model I used

"TIME_TO_SEC(UserVideo.video_length)" AS vid_len and sorted by the same

It was working fine in normal scenario but was not showing sorted results under pagination may be because it was not permanent column of table

Solution:

I did some tricky thing here..I needed the column in format TIME_TO_SEC so in my fields property in my find function I customized fields and added concerned field in name of old field sth like

$this->UserVideo->find('all',array('fields'=>array("TIME_TO_SEC(UserVideo.video_length)" AS video_length)))

See words marked in bold

And finally all was coming well under pagination as well

Cakephp->check if there was a referrer page/last page

Cakephp check for a last page/redirect to last page


 if($this->reffrrer()!='/')  
 {  
 $this->redirect($this->reffrrer());
 }  
 This works in controller