Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Parsing XML file using CakePHP

. Wednesday, August 6, 2008
6 comments

After spending one whole evening, I found out CakePHP has its own XML class for handling xml files. In the meantime, i tried SimplePie (for RSS only, not XML), SimpleXML, and XMLize. But still they werent of any great help compared to cakephp's core XML class.

How to parse it then?
First of all, you need to import the XML class in your controller class using App::import(). Here is the controller class parsing a particular xml file and printing out (like print_r) the returned array.

Controller class:

<?

class ParseController extends AppController {
var
$name = "Parse";
var
$uses = array('MyModel');

function
xml() {
// import XML class
App::import('Xml');

// your XML file's location
$file = "my_xml_file_location.xml";

// now parse it
$parsed_xml =& new XML($file);
$parsed_xml = Set::reverse($parsed_xml);

// see the returned array
debug($parsed_xml);
}
}

?>

Search Engine Friendly URLs with CakePHP

. Saturday, July 12, 2008
1 comments

This helper will let you create search engine friendly urls without having you to change the controller logic.

Lets assume we have an 'articles' controller (/app/controllers/articles_controller.php) in our website. And the database table looks like this:
id, category_id, title, content, updated, created

The current url structure:
yoursite.com/articles/category/[category-id]/
yoursite.com/articles/view/[article-id]/

So, it means we dont have the 'title' of our articles included in the url. Only ID is present. This helper will let you add the title as well in the url.

Helper class

<?
class SefHelper extends Helper {

var $helpers = array('Html');

function clean($string) {
return r(" ", "-", strtolower($string));
}

function article($array) {
$link = "/articles/view/";
$link .= $array['Article']['id'] . "/";
$link .= $this->clean($array['Article']['content_title']) . "/";

return $link;
}

function category($array) {
$link = "/articles/category/";
$link .= $array['Category']['id'] . "/";
$link .= $this->clean($array['Category']['title']) . "/";

return $link;
}
?>


View
<? echo $html->link("Article Title here", $sef->article($articles_result)); ?>

The return of this helper function will be: /articles/view/[id]/[title-of-the-article]

Scaffolding in CakePHP

. Monday, June 9, 2008
9 comments

What is scaffolding?
Its something that you will find very useful in production apps. When you begin, you may want to throw up stuff real quick in order to get started. Ultimately, its a built in system for adding, editing, viewing and deleting your database records quickly.

Database table
Suppose we want to work with a table named users, and here is its SQL structure

CREATE TABLE `users` (
`id` bigint(20) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`email` varchar(96) NOT NULL,
`password` varchar(50) NOT NULL,
`updated` int(11) NOT NULL,
`created` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
)

Model
Now lets create a model file first. It should be placed here: /app/models/user.php
<?
class User extends AppModel
{
var $name = "User";
var $uses = "users" // table name
}
?>


Controller
Create a controller file here: /app/controllers/users_controller.php
<?
class UsersController extends AppController
{
var $name = 'Users';
var $scaffold;
}
?>

View
Not required since the views for scaffolding are generated by CakePHP automatically.

Visit http://yoursite.com/users/ and start scaffolding.
Note: Always visit yoursite.com/controllerName/ to see the visual output of your controller.

I will show you a user registration and logging system in CakePHP soon.

Installing CakePHP

.
0 comments

Download
Get the latest version of CakePHP (current version is 1.2)

Requirements
A HTTP server (better if Apache) with session and mod_rewrite enabled.
PHP 4.3.2 or higher
A MySQL database (PostgreSQL is supported too)

If you want to install it in your PC (Windows), then read this article: http://frinity.blogspot.com/2008/05/installing-apache-php-mysql-in-windows.html

Upload the files
Unpack the downloaded file, and upload its content to your server. Make sure that it looks like this from your root.

/wwwroot
/cake
/app
/cake
/vendors
.htaccess
index.php

Configure your database
Rename app/config/database.php.default to database.php. Open it and edit the details.

var $default = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '');


CakePHP installed!
Now we can move to another article for creating your first controller!

Understanding the MVC framework (PHP)

.
0 comments

I have already mentioned about MVC framework and CakePHP in one of my articles, therefore I am going to discuss more about it today. Please note, CakePHP is one of the MVC frameworks written in PHP. Don't get confused that MVC framework means CakePHP.

MVC
MVC stands for Model View Controller. It is ultimately a software design pattern where you can separate your code and make it reusable, thus making you write less code.

Model
For each and every database table, we have a separate model - this is what represents a particular database table. And according to the naming convention of CakePHP, its name is always the singular of its table's name. For example, if the name of the database table is 'users', the model name will be 'User' (first character of the name is always in uppercase).

Controller
A controller is used for managing the logic of a certain section of your application. Generally, it is used for managing a single model. For example, you have a model named 'User', and you will require a controller for managing the logic of user registrations, logins, log outs, account settings, etc. According to the naming convention of CakePHP, controller names are always plural. In this case, the controller name would be 'Users'.

View
A view is a page template, usually named after its controller's action (a controller is a php class with many functions known as its actions). As you can guess, this is responsible for the visual output of your application.

I will show you how to install CakePHP (including the php files) in my next article.

Frameworks I prefer most for web applications

. Wednesday, June 4, 2008
2 comments

According to wikipedia: "web application framework is a software framework that is designed to support the development of dynamic websites, Web applications and Web services. The framework aims to alleviate the overhead associated with common activities used in Web development. For example, many frameworks provide libraries for database access, templating frameworks and session management, and often promote code reuse".

Here are some of the popular frameworks that you will find handy when developing applications.

Blueprint (CSS)
Blueprint is a CSS framework, which aims to cut down on your CSS development time. It gives you a solid CSS foundation to build your project on top of, with an easy-to-use grid, sensible typography, and even a stylesheet for printing.

Script.aculo.us (Javascript)
script.aculo.us provides you with easy-to-use, cross-browser user interface JavaScript libraries to make your web sites and web applications fly. But once you have jQuery, I am sure you will not use any other javascript libraries at all.

jQuery (Javascript)
jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. jQuery is designed to change the way that you write JavaScript. Also, dont forget to have a look at jQuery UI.

CakePHP (PHP)
CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. Using commonly known design patterns like MVC and ORM within the convention over configuration paradigm, CakePHP reduces development costs and helps developers write less code.

Installing Apache, PHP, MySQL in Windows

. Tuesday, May 27, 2008
1 comments

WAMP
W
indows Apache MySQL PHP. There are a few WAMP installers out there including EasyPHP and WampServer (XAMPP for Mac). I prefer WampServer since it comes with an easy to use service manager as a tray icon. The service manager allows you to choose versions and manage all the extensions (modules) of apache, php, and mysql. This way, you don't even have to edit any configuration files manually. You cant get it any easier!

Download and install WampServer
Download WampServer from their website.
http://www.wampserver.com/en/download.php

WampServer can be downloaded from SourceForge too
http://sourceforge.net/projects/wampserver/

Now run the installer and follow the instructions. Its better if you do not install it in C: drive (where you have Windows installed).

You are now ready to run your php files
Now open your browser, and visit http://localhost. This is the host that runs in your local machine. Click on the WampServer tray icon, then click 'www directory'. Now if you place a file named 'test.php' in that directory, you can run the file from your browser using this link http://localhost/test.php.

For managing your MySQL databases using phpMyAdmin, visit http://localhost/phpmyadmin.

Hope that helps :)

Your Workspace

.
0 comments

I am going to introduce you how to set up a workspace where you will be able to manage your web projects.

Webpage Editor
When it comes to a software to edit your webpages, Dreamweaver is the first choice. The latest version CS3 has got a nice user friendly interface, and its really very easy to use. But you have to pay a few hundreds of bucks for that. If you want a free editor, then I would recommend you to use Notepad++. Its open source as well.

Photoshop
There is nothing as useful as Photoshop. It simply rules. Even though it will cost you a few hundreds, its worth buying it. The latest version Photoshop CS3 Extended even lets you export (slice) your layout in CSS which makes a designers life a lot easier.

Server
If you are a PHP/MySQL developer like me, then you have to install Apache, PHP, and MySQL in your computer for testing your web applications. Its also necessary to install phpMyAdmin for managing your mysql databases. (I will show you how to install them in my next post)