Tuesday, September 28, 2010

Zend Framework: Multiple Check Box Handling

Here I will try to describe how to handle multiple check box with Zend Form

Below is the Form from/user.php
<?php
class Form_User extends Zend_Form
{
    public function __construct($options = null)
    {
        parent::__construct($options);
      
        $this->setName('User_Form');
       
        $user_id = new Zend_Form_Element_Text('user_id');
        $user_id->setLabel('User ID')
                  ->setRequired(true)
                  ->addValidator('NotEmpty');

      
        ..................................
        ....................................
        ....................................

        $area = array(

                            'index' => 'Home Page',
                            'admin' => 'Admin Home',
                            'agent' => 'Agent Contorl',
                            'user' => 'User Contorl',
                            'cdist' => 'Card Distribution',
                            'cardsell' => 'Card Sell',
                            'manualmt' => 'Manual Money Transfer',
                            'moneydelivery' => 'Money Delivery',
                            'transactionmonitor'    => 'Transaction Moniitor',
                            'adminreport'   =>  'Admin Report',
                            'agentreport'   =>  'Agent Report',
                            'customercare'   =>  'Customer Care',

);

$privileges = new Zend_Form_Element_MultiCheckbox('privileges');

$privileges->setLabel("Select Previliges");
            foreach($area as $prev => $p)
            {
                $privileges->addMultiOption($prev,$p);
            }

        $user_photo = new Zend_Form_Element_File('user_photo');
        $user_photo->setLabel('Photo');
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save Data')
                 ->setAttrib('class', 'button')
        ;
       
         $this->addElements(array(
                $user_type,
                $agent_id,
                $user_id,
                .........
                $privileges,
                $submit));

    }
}


Now The Controller UserController.php


class UserController extends Zend_Controller_Action
{

    public function init()
    {
     /    }

       public function addAction()
    {
        // action body
       
        $this->view->headTitle('Add New User', 'PREPEND');
        $form = new Form_User();
        $this->view->form = $form;
      
        if ($this->getRequest()->isPost()) {
       
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
        //$sp = implode(",",$privileges);
        $user_photo = ($form->getValue('user_photo')) ? $form->getValue('user_photo') : "";

        $data = array(
            'user_type' => $form->getValue('user_type'),
            'agent_id' => $form->getValue('agent_id'),
            'user_id' =>strtolower($form->getValue('user_id')),
           ..............................
.................................
            'privileges' => serialize($form->getValue('privileges')),
            'user_status' => $form->getValue('user_status'),
            'user_photo' => $user_photo,
        );

      
        $user = new Model_DbTable_Userinfo();

        $user-> addUser($data);
        $this->_redirect('/user/');

        } else {
            print_r($formData);
            $form->populate($formData);
        }
        }
    }



The Model to Do the database action

class Model_DbTable_Userinfo extends Zend_Db_Table_Abstract
{
    protected $_name = 'user_info';

       public function addUser($data)
    {
       
        $this->insert($data);
    }
}









To edit the Multiple check box value do as follows in Edit action

public function editAction()
    {
        // action body
        $sess = new Zend_Session_Namespace('uri');

        $this->view->title = "Edit User";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Form_User();
        //$form->user_id->setAttrib('readonly','true');
        $form->removeElement('user_id');
        $user_id = new Zend_Form_Element_Hidden('user_id');
        $form->addElement($user_id);
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
        $user_id = $form->getValue('user_id');
        $agent_id = $form->getValue('agent_id');
        $user_photo =  $form->user_photo->getFileName('user_photo',false);

        if(!empty($user_photo))
            {
        $data = array(
            'user_type' => $form->getValue('user_type'),
            'agent_id' => $form->getValue('agent_id'),
            'user_pass' => $form->getValue('user_pass'),
            'user_name' => $form->getValue('user_name'),
            'user_address' => $form->getValue('user_address'),
            'user_contact' => $form->getValue('user_contact'),
            'user_email' => $form->getValue('user_email'),
            'privileges' => serialize($form->getValue('privileges')),
            'user_status' => $form->getValue('user_status'),
            'user_photo' => $form->getValue('user_photo'),
        );
            }
    else
        {
        $data = array(
            'user_type' => $form->getValue('user_type'),
            'agent_id' => $form->getValue('agent_id'),
            'user_pass' => $form->getValue('user_pass'),
            'user_name' => $form->getValue('user_name'),
            'user_address' => $form->getValue('user_address'),
            'user_contact' => $form->getValue('user_contact'),
            'user_email' => $form->getValue('user_email'),
            'privileges' => serialize($form->getValue('privileges')),
            'user_status' => $form->getValue('user_status'),
        );
        }
        $user = new Model_DbTable_Userinfo();
        $user->updateUser($data,$user_id);
        $this->_redirect($sess->req_uri);
        } else {
        $form->populate($formData);
        }
        }
        else {
        $user_id = $this->_getParam('user_id', 0);
        $user = new Model_DbTable_Userinfo();
        $user_data = $user->getUser($user_id);
        $form->populate($user_data);
        $prev = unserialize($user_data['privileges']);
        $form->privileges->setValue($prev);
        }
    }





Hope Thats help you.

Tuesday, September 7, 2010

How to detect if the user is logout in php?

You cannot detect when a user closes their browser or navigates off your site with PHP, and the JavaScript techniques of doing so are so far from guaranteed as to be useless.
Instead, your best bet is most likely to store each user's last activity time.
  • Create a column in your user table along the lines of 'last_activity'.
  • Whenever a user loads a page, update their last_activity to the current time.
  • To get a list of who's online, just query the database for users with last_activity values more recent than 10/20/whatever minutes ago.

Source: http://stackoverflow.com/questions/887919/how-to-detect-if-the-user-is-logout-in-php

Jpgraph and Zend Framework: The image “ ” cannot be displayed, because it contains errors

Problem: My Below Code failed to produce Jpgraph Graph and give the above Error.

set_time_limit(0);
        require ("jpgraph/jpgraph.php");
        require ("jpgraph/jpgraph_pie.php");
        require ("jpgraph/jpgraph_pie3d.php");

        // set the directory where images are cached by JpGraph
        //$jpgcache = APACHE_CACHE_DIR;

        $tran =  new Model_Report();
        $result = $tran->all_transaction_req();

        foreach($result as $row)
        {
        $level[]=$row['tran_req_date'];
        $data[]=$row['amount'];
        }
        //print_r($data);
        //exit;
        $graph_name = 'chart.png';
        $graph = new PieGraph(500,400,"auto");
        $graph->SetShadow();

        // Set A title for the plot
        $graph->title->Set("Transaction Statistics");
        $graph->title->SetFont(FF_VERDANA,FS_BOLD,12);
        $graph->title->SetColor("darkblue");
        $graph->legend->Pos(0.0,0.1,"right","top");


        // Create pie plot
        $p1 = new PiePlot3d($data);
        $p1->SetTheme("sand");
        $p1->SetCenter(0.4);
        $p1->SetAngle(45);
        $p1->value->SetFont(FF_ARIAL,FS_NORMAL,12);
        $p1->SetLegends($level);
        #$p1->SetLabels($level);
        $p1->SetLabelMargin(5);
        $p1->SetLabelPos(0.9);


        $graph->Add($p1);

        header ("Content-type: image/png" );
        $graph->Stroke();
        


Sloution: add exit; after  $graph->Stroke();
       .......
       ........
       $graph->Stroke();
       exit;


Source: Internet

Monday, September 6, 2010

How to Turn off Form AutoCompletion

We can off the Auto Completion in web form by following way.

1. Set autocomplete="off" in input box.


<input type="text" name="cc" autocomplete="off" />
 
 
2.  Set autocomplete="off" in Form Tag.
 
 
3. The Best way I prefere
First page ( HTML Form ) : 

<form method="post"> 
<input type="hidden" name="username" value="random1"> 
<input type="hidden" name="password" value="random2"> 
Username: <input type="text" name="random1" value=""><br />
Password: <input type="password" name="random2" value="">
</form>

Where "random1" and "random2" are random names generated, you can use in combination with unix time.

Second page ( PHP output ) :
<?php
if ( isset($_POST['username'], $_POST['password']) && isset($_POST[$_POST['username']], $_POST[$_POST['password']]) ) { echo 'Username: '.$_POST[$_POST['username']].'<br />'. 'Password: '.$_POST[$_POST['password']]; }
?> 


Sourece: Web Search