Header menu

_________________________________________________________________________________
Showing posts with label cakephp. Show all posts
Showing posts with label cakephp. Show all posts

Tuesday, 8 October 2013

Add span tag( or any html tag) to link in cakephp

To add <span> tag to cakephp link add span to label

"<span>Label name</span>"

and add

"array('escape' => false)"

to link as shown in below example.

Example:
<?php echo $this->Html->link(
    '<span>My Label</span>',
    array('action' => 'add'),
    array('escape' => false)
// This line will parse rather then output HTML
); ?>
One thing to note here 'escape' => false' must be added to 3rd parameter of link.
You can see the structure of cakephp link for more information at

http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html

Saturday, 21 September 2013

Upload file in cakephp using move_uploaded_file function

move_uploaded_file()

Easiest way to upload file in cakephp is by using move_uploded_file function.
But it beneficial when you have to put file uploader at few place. If you need
to place large number of form which upload file then use component method otherwise
this is easier and much faster way.

I need one multipart form which can select file to upload i have  created one in
index.ctp and rest of the work will be done by index function in controller.



This is my controller file

<?php
class ContactsController extends ContactManagerAppController {
    public $uses = array('ContactManager.Contact');

    public function index() {
        if($this->request->is('post')){
            pr($this->request->data);
            $filePath = "./img/".$this->request->data['Contact']['image_file']['name']; 
            if(move_uploaded_file($this->request->data['Contact']['image_file']['tmp_name'], $filePath)){
            echo "File uploaded Successfully";
            }
       }
     } 
}








This is view for my Controller


index.ctp
<?php
    echo $this->Form->create('Contact', array('enctype' => 'multipart/form-data', 'class'=>'form-horizontal'));
?>
    <label class="control-label" for="inputError">Image</label>
<?php
    echo $this->Form->file('image_file');
    echo $this->Form->submit(__('Submit') , array('class'=>'btn btn-primary'));
?>




Thursday, 12 September 2013

Write sql queries in cakephp controller

How to write sql queries in cakephp controller 

public function index(){
    $result= $this->User->query("SELECT * FROM users ;");  
}

User :- It is the name of my Model
users:- Table name in database

$result:- contain array of result of sql query

Get session information in cakephp

How to get session information in cakephp 

In the controller you can use

$this->Session->read();

to get all details of session variable . And you can use

$this->set('session',$this->Session->read());


to use these values in corresponding view page.

-----------------------------------------------------------------------------------------------------------------------

If you wish to use session variable in each page then use set method in appcontroller to set its value

public function beforefilter(){
$this->set('session',$this->Session->read());
}

Now this session variable is accessible in every page of view.

Note: this should be placed in Appcontroller...

Wednesday, 11 September 2013

Cakephp 1.2 - include problem of css and js , and improper functioning

Changes to made to be made cake php version 1.2 properly :

1. uncomment the statement in file apache2/conf/httpd.config on wamp server

LoadModule rewrite_module modules/mod_rewrite.so

2. In the same file make sure

 AllowOverride all (if there is none in place of all .. change it to all for your "C:/wamp/www" directory)

3. Make sure that all three .htaccess files have default configuration .. there must not me any change ..

Monday, 9 September 2013

Design form in cakephp

Note:Input name should be same as field name in database

Form to be written in .ctp in view

<?php
echo $this->Form->create('Model_name');
echo $this->Form->input('field1');
echo $this->Form->input('field2', array('rows' => '3'));
echo $this->Form->end('submit');
?>

Friday, 6 September 2013

Create two different url for admin and front site in cakephp

Most easy way to do this is by the use of scaffold variable.

Open the controller for which you want to make different admin page
and add the following statement:

public $scaffold='admin';

Now to go to app /config/core.php
and add the following statement:

Configure::write('Routing.prefixes', array('admin'));

Now create function for admin in controller like
public function admin_index(){
   
}


Now you access your admin using http://www.domain.com/admin/controller_name
and site using http://www.domain.com/controller_name