Zend Framework

Archive for March 6th, 2009

PHP: Listing files in particular directory, a real world example

Posted by Faheem Abbas on March 6, 2009

We are preparing our static files specially java script, css and images files to be moved to Amazon CDN. The thing we need to make sure here is that only those files that are in use should be moved. I was given a task to list all the js, css and images we are currently using in our application.

Initially I opened notepad and start writing down the names of those files.

Hehe, we don’t think out of the box.

The whole process was disgusting and I was feeling tired.

However working for a bit of time, an idea come to my mind, why not use computer to do the task for me.

And this is what I did.

Create .php file and write the following code in that file.

<form action=”" method=”post”>

<input type=”text” name=”path” />

<br>

<input type=”submit” value=”Submit” />

</form>

<?php

$pathName = $_POST['path'];

if($pathName){

$dir = new DirectoryIterator($pathName);

foreach($dir as $fileInfo) {

if($fileInfo->isDot()) {

// do nothing

} else {

echo $fileInfo->__toString().’<br>’;

}

}

}

?>

The code above did everything for me. Created a list of files in particular directory. I took print out of that list and crossed (x) files under construction.

In the first few lines I am creating form, adding input box for the directory path to be entered and submit button.

In the php code

  1. I get the path
  2. If path is not empty, I create an instance of DirectoryIterator available in PHP5, giving it directory path entered via input box.

3.Loop the $dir object and echo the result as string.

Posted in Php | 1 Comment »

Zend Framework Form: working with dropdown(select) list

Posted by Faheem Abbas on March 6, 2009

Though I’ve already discuss how to create dropdown in one of my previous article, however here I would discuss it a bit in detail.

You can create dropdown using the following code.

<?php

class CustomForm extends Zend_Form

{

public function init()

{

$this->setMethod(‘post’);

$this->setAction(‘user/process’);

$country = $this->createElement(’select’,'countries’);

$country ->setLabel(‘Countries:’)

->addMultiOptions(array(

‘US’ => ‘United States’,

‘UK’ => ‘United Kingdom’

));

}

}

Or can also be created using the following code.

<?php

class CustomForm extends Zend_Form

{

public function init()

{

$this->setMethod(‘post’);

$this->setAction(‘user/process’);

$country = new Zend_Form_Element_Select(‘countries’);

$country ->setLabel(‘Countries:’)

->addMultiOptions(array(

‘US’ => ‘United States’,

‘UK’ => ‘United Kingdom’

));

}

}

The options are hard code in both of the cases. This will not be requirement always.

Sometime or most of the time application need that drop down should be populated fetching records from the data base. So for this purpose I would like to implement the following code.

Fist in your model write the following.

<?php

class Countries extends Zend_Table

{

protected $_name = “countries”;

public function getCountriesList()

{

$select  = $this->_db->select()

->from($this->_name,

array(‘key’ => ‘id’,'value’ => ‘country_name’));

$result = $this->getAdapter()->fetchAll($select);

return $result;

}

}

Keep in mind that the records we fetch will have an associated array having id as key and country name as value. If you don’t defined the above array, array(‘key’ => ‘id’,'value’ => ‘country_name’), as I have defined, you will not get what you want. So be careful while fetching records for constructing drop down.

And now in your form write the following code.

<?php

class CustomForm extends Zend_Form

{

public function init()

{

$this->setMethod(‘post’);

$this->setAction(‘user/process’);

$countries = new Countries();

$countries_list = $countires->getCountriesList();

$country = new Zend_Form_Element_Select(‘countries’);

$country ->setLabel(‘Countries:’)

->addMultiOptions( $countriesList

));

}

}

In the above code the only thing changes is

  1. I have instantiated model  using

$countries  =  new Countries();

2. Call getCountiresList() method. And assigned that list to the drop down,  instead of assigning hard coded values.

Posted in Zend Framework | 2 Comments »