Archive for September, 2009

Validate an email address using symfony’s inbuilt validators

    public function isValidEmail($email_addr)
    {
        $myValidator = new sfEmailValidator();
        $myValidator->initialize( $this->getContext() );
        if ( !$myValidator->execute( $email_addr, $err_msg ) )
        {
            return false;
        }
        else
        {
            return true;
        }  

No Comments

A way to Sort an array of Objects in php

I did get this from the PHP.net site..

http://au2.php.net/usort

name = $name;
    }

    /* This is the static comparing function: */
    static function cmp_obj($a, $b)
    {
        $al = strtolower($a->name);
        $bl = strtolower($b->name);
        if ($al == $bl) {
            return 0;
        }
        return ($al > $bl) ? +1 : -1;
    }
}

$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");

usort($a, array("TestObj", "cmp_obj"));

foreach ($a as $item) {
    echo $item->name . "\n";
}
?>

The above example will output:

b
c
d

No Comments

How to export data as a CSV file in Symfony

If you need to export database data as a CSV file in Symfony, try this;

In the action:

    public function executeRegistrantsToCsv(){

  	$id = $this->getRequestParameter('id');

	$c = new Criteria();
	$c->add(RegistrantPeer::EVENT_ID, $id);
	$c->add(RegistrantPeer::STATUS, 1);
	$this->aObjReg = RegistrantPeer::doSelect($c);

	$this->forward404Unless($this->aObjReg);
	$this->setlayout('csv');

	$this->getResponse()->clearHttpHeaders();
	$this->getResponse()->setHttpHeader('Content-Type', 'application/vnd.ms-excel');
	$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename=registrants_report_event_' . $id . '.csv');

  }

in the template registrantsToCsvSuccess.php:

Title,Name,Email,Phone,Organisation,State,City,Country,Login Date,IpAddress
<? foreach($aObjReg as $r): ?>
 <?= $r->getTitle() ?>,<?= $r->getName() ?>,<?= $r->getEmail() ?>,<?= $r->getPhone() ?>,<?= $r->getOrganisation() ?>,<?= $r->getState() ?>,<?= $r->getCity() ?>,<?= $r->getCountry() ?>,<?= $r->getLoginDate() ?>,<?= $r->getIpAddress() ?>,
<? endforeach ?>

in the templates/csv.php:

<?php echo $sf_data->getRaw('sf_content') ?>

4 Comments