

Salam, laypark with me and discuss bout programmer life's and share something useful here. Not only a code but also a real programmer life's..








$query = $this->db->getwhere('mytable', array(id => $id), $limit, $offset); $this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20); $query = $this->db->get();
Note: the method chaining in the second part of this example is only available in PHP5.
You can also create a model object, load it in and build custom methods to handle a custom task. You'd want to do this in the model and not the controller to help isolate code into the MVC silos.
CakePHP takes a slightly different route by automatically loading in the model that matches the current controller (controllers tend to be named similarly to the models they are associated with). You can turn off this automated loading and even assign different models that should be loaded by the controller instead.
CakePHP also takes things further by establishing all the model associations for you, allowing for some really easy querying. For example, assuming I'm in a controller named post_controller, I could do the following:
$this->Post->Comment->findAllByPostId($id)
I chose this particular query because it shows two different concepts. The first is the fact that I can access the Comment model via the Post model (assuming I've defined that association in the Post model). The second is the fact that I have a method called findAllByPostId. CakePHP allows records to be grabbed via findByX and findAllByX queries where X is equal to the field name you're trying to find.
Where I think Cake shines is in its ability to pull in all associated data automatically. Take the following query as an example:
$this->Post->findById($id)
This query would automatically pull in all the comments associated with this Post. Really handy stuff.
Validation
When working with models, you'll inevitably have to handle data validation. Data validation in CodeIgniter is handled via a validation class. A set of rules get defined and assigned to the validation object. The validation object automatically (I assume) validates the data passed via the URL or form. From there, you can decide how that gets handled. The validation class can also help automate some of the process of setting error messages for specific fields.
CakePHP handles its validation through the model itself in one of two ways. The first uses a single test against each field defined in a validate variable declared in the model. This works okay for simple stuff but it quickly becomes a cumbrance. Beyond simple validation, I take advantage of the beforeSavecallback to perform any custom validation, invalidating any fields that fail.
It's a toss up for me as to which one "wins". CakePHP 1.2 will have its validation system reworked a bit to allow for more flexibility.
CakePHP handles this fairly well by using a default layout (which you can easily switch at runtime). The layout has two variables be default: title_for_layoutand content_for_layout. Each action automatically links to a particular view which gets spat into place. Again, it's the "automagic" approach. As long as you name your files a specific way, controllers automatically get linked to models and views. It's easy enough to override all of this, too, and define your own layouts or view files. There's no convenient way to get the generated view data, however, making custom built caching mechanisms difficult to implement.
CodeIgniter takes a very straightforward approach: like include files, almost. Each file gets loaded in and processed. There's a templating class but it doesn't simplify things much beyond the built-in view handling. You can mimic the CakePHP approach by always including the header and footer calls but it's not as seamless. CodeIgniter offers hooks allowing view and caching mechanisms to be overridden and replaced with a custom system.
CodeIgniter in my mind wins this hands down with classes for FTP, Email, File Uploading, XMLRPC, Zip encoding and more.
CakePHP on the flip side comes pretty light but tries to make up for it using the Bakery. You can, like CodeIgniter, easily drop in 3rd party classes for any features you might need. Interestingly, although I haven't tried it, you could probably drop in many of the CI classes into CakePHP without issue.
CakePHP allows for application-wide changes to be done via the base application controller that all other controllers inherit from. Likewise, you can create global model methods using the application model file. However, you can fine tune things at the controller level using any of the controller-level callbacks (beforeFilter, afterFilter and beforeRender). Things like auto-loading helpers and components can also be specified easily at the individual controller level.
CodeIgniter allows for the auto-loading of helpers, libraries and plugins but does this application-wide.
Documentation is key to understanding any framework well enough to develop within it.
CodeIgniter has a complete list of all components with each method and property documented within. CI also has forums and a wiki which feature a lot of user-submitted code.
CakePHP, on the other hand, isn't as well organized. The manual is starting to show its age with some sections not really going much beyond what the APIoffers. Because of the format of the original documentation, you can also get it in other formats such as CHM and PDF. CakePHP has the Bakery which contains user-submitted articles, components, etc. The dev team also hangs out heavily on the IRC channel (#cakephp at irc.freenode.net). Finally, there's the CakePHP Google Group which is pretty active.
I'm a pretty pragmatic individual and I honestly feel that these two frameworks have a lot going for them. They take a much simpler approach to application development than the complexity that is something like Symfony.
I'm still personally a fan of CakePHP over CodeIgniter for much of the "automagic" that I mentioned. And it's shortcomings have been getting addressed with each new iteration (1.2 will be a considerable leap over 1.1 but it will still be awhile before it's released).
This comparison was based on the documentation for CodeIgniter 1.5.2 and having used CakePHP 1.1. I have specifically avoided the subject of performance due to the amount of time required to design, develop and test such a thing.

pagination.php
I think this code will give you an idea how to create a pagination.//max displayed per page$per_page = $pageCount;//get start variable$start = $_GET['start'];$query = "SELECT * FROM ".$table." ".$condition;//count records$record_count = mysql_num_rows(mysql_query($query));$max_pages = ceil($record_count / $per_page);if(!$start){$start = 0;}//setup prev and next var$prev = $start - $per_page;$next = $start + $per_page;//set variable for 1st page$i = 1;//show prev buttonif(!($start <= 0)){$pagePrev = "<a href='".$page."?start=".$prev.$PageQuery."'>Prev</a>";$pageFirst = "<a href='".$page."'?start=0".$PageQuery."'>[First]</a>";}$pageNum = '';for($x=0;$x<$record_count;$x=$x+$per_page){if($start!=$x){$pageNum.= "<a href='".$page."?start=".$x.$PageQuery."'>".$i."</a>";$NoOfPage = $x / $per_page;}else{$pageNum.=$i;}$i++;}$lastP = $per_page * $NoOfPage;if(!($start >= $record_count - $per_page)){$pageNext = "<a href='".$page."?start=".$next.$PageQuery."'>Next</a>"; $pageLast = "<a href='".$page."?start=".$lastP.$PageQuery."'>[Last]</a>";}index.php$UID = $_SESSION['uid'];$pageCount = 25; //setting up rows per page$table = "pengurusan_pengguna"; //table to be called$condition = "where id_pengguna='".$UID."'"; //condition for SQL$page = "index.php";//the page to be displayed
$PageQuery = "&nokp=".$NoKP; //the other querystringinclude_once('../Function/pagination.php');//include the pagination object$SQLSelect = "SELECT * FROM pengurusan_penggunawhere id_pengguna='".$UID."'LIMIT $start, $per_page";$result = mysql_query($SQLSelect);echo "Bil. | Tarikh | Aktiviti <br />";if(mysql_num_rows($result) > 0 ){//Show data$Count = 0;while($row = mysql_fetch_array($result)){$UID = $row['id'];$Tarikh = $row['tarikh'];$Aktiviti = $row['aktiviti'];$Count++;echo $Count." ".$Tarikh." ".$Aktiviti." <br />";}echo $pageFirst." ".$pagePrev." ".$pageNum." ".$pageNext." ".$pageLast;
index.phpSee the video below for the explaination :
<html>
<table width="70%" align="center">
<tr>
<td><h1>Banner</h1></td>
</tr>
</table>
<table width="70%" align="center">
<tr>
<td width="20%">
<a href="index.php">Home</a><br />
<a href="index.php?page=tutorial">Tutorial</a><br />
<a href="index.php?section=administrator&page=index">Admin</a><br />
</td>
<td width="80%">
<?php
$section = $_GET['section'];
$page = $_GET['get'];
if($page){
//check for section
if(!$section){
$section = "inc";
}
$path = $section."/".$page.".php";
/* How it program --
Logicaly is like this :
section = administrator
page = index
path = administrator/index.php
*/
if(file_exists($path)){ //build in function
include($path);
}
else{
echo "Sorry, page does not exist";
}
}
else{
include('inc/home.php');
}
?>
</td>
</tr>
</table>
</html>