2011-12-05

Cakephp : create a list movies

my work link
Hello.. Welcome again to my cake php tutorial. This tutorial will show how to show list of movies. Actualy This is more simple than making a blog in cakePHP manual. Without delay.. LET's continue. I put my cakePHP on Kerjaan4/Cake/blackforest. or for a simple let's say "myCake/". Now let's type myCake/movie to continue. Wait.. You perhaps surprised, I'm not building anything but why I must called something that's not exist?!? Well.. We will learn something not only creating form basic but learning the error.
Every page in cake using file with extension .ctp and you need to open the ctp then treat it like PHP extension.
Error: You shall not pass
You will found this Error!! This error show us because you don't make the file for show above! But I want to customize the script.. Soo the error will be shown differently. This error show to us how to make page we need.
Next we will make movieController.php but now we need to build missing_controller first for our error pages.
Go to App/View/Errors
Okay.. Now go to destination place
at first there is none
In there build/create ne file and give name missing_controller.ctp

missing_controller.ctp
And type like this!! the main script is
$ref=@ $_SERVER['HTTP_REFERER'];
$s= '<a href='".$ref.'">ERROR!! Please return</a>';
echo $s;
The page are to shown the link you enter are false. You can build a better one for next.
Refresh
This is the result if you reload the browser now. Now let's refresh our memory.. Now let's create another view for movie
just go up once
  1. Just go to app/view
  2. then build new folder
  3. name movie
The example show wrong. Actualy the right one is movie not movies
build new file
Create a new file name index.ctp
simple script
This is the script.. I think to build simple first.

Still Error?
You have this error.. because you don't make the controller for movies. Okay, let's make one now.
Go to app/controller
You will found in controller folder.. there is no file (only folder there).. Just make new file name movieController.php
movieController.php
  1. Create like above. You need to type class {filename} extends AppController
  2. then type all public
  3. in bottom you add function index. If you don't give index, there will be an error again. But let's see the error again in next tutorial.
  4. reload the browser
still give error?
This happen because we not have the table. And this error shown us where to customize the error.
table movies
Let's make movies table. Go to your database then type query to build the table. Or if you like using GUI (like PHPmyadmin).. Please do soo.. this table contain
  • mov_id
  • mov_name
  • mov_type
 don't forget auto increment in mov_id
phpmyadmin
add some file inside. For type you must type Asia, West, Cartoon or anything you desire. You don't have to follow what I type above. Just type whatever you like.
Our new page
Try to reload and we have our new page. Remember the link are myCake/movie. But we  need to show data from database into this page.
index.ctp
now type like above.

THIS IS FRONT PAGE FROM MOVIES
<?php
      $s="<ol>";$i=0;
      foreach ($myMovie as $post){
            $s.="<li>".$post['movie']['mov_name'];
            $s.="</a> (".$post['movie']['mov_type'].")";
      }
$s.="</ol>";
print $s;
$myMovies is from variable you type earlier in movieController.php the controller already take every data inside the table. And you just need to take and used it.

The result
Reload the browser then see the result. In my example, there is more than 10. You perhaps less.. but that's no different.

let's more detail about contoller
 Next we discuss of this. But today.. let's skip it.

Source: Dedicated.

Tidak ada komentar: