www.pokeroconnor.com

Unix time converter

April3

Thanks to the Captain for the handiest Unix time -to- date converter, and vice versa! 

And in case you ever need to create a Unix time format, you can use php’s date and mktime functions to do so:

$hour= date(”H”);

$minute= date(”i”);

$second= date(”s”);

$day = date(”j”);

$month = date(”n”);

$year = date(”Y”);

$unix_time = mktime($hour,$minute,$second, $month, $day, $year);

You can override your server’s timezone setting using date_default_timezone_set.

Zend Framework Starters

March2

Here is a collection of the better Zend tutorials, resources and tidbits I have started collecting.

First off, here is probably the best quick tutorial to the ZF, or rather its the best that I have seen so far.

Something obvious, that cost me time, was that when you create a custom class in Zend for which to extend other classes such as controllers from, you must place the new custom class in your applications include path, e.g create a new directory in your applications dir called ‘custom’, and then add this to your damn include path

set_include_path(’../library’ . PATH_SEPARATOR .
‘../application/custom’ . PATH_SEPARATOR . get_include_path());

Also important, in any child classes of the new custom class:

If we want an init()function in any of the child classes, we must also call parent::init() from that class.

Two decent Apress books on the Zend Framework, http://www.amazon.com/Pro-PHP-Patterns-Frameworks-Testing/dp/1590598199 and Practical Web 2.0 Applications with PHP. The latter is especially recommended…

The symlink for library directory on A2 is library -> /usr/lib/php/ZendFramework/latest/library/. Fascinating, I know. To remove a symlink, do unlink symbolic_link.

Zend Forms and Related

Fetching row data from mysql in Zend Framework.

From the official Zend docs, here’s the basics of a form controller – nice.

class UserController extends Zend_Controller_Action
{
public function getForm()
{
// create form as above
return $form;
}

public function indexAction()
{
// render user/form.phtml
$this->view->form = $this->getForm();
$this->render('form');
}

public function loginAction()
{
if (!$this->getRequest()->isPost()) {
return $this->_forward('index');
}
$form = $this->getForm();
if (!$form->isValid($_POST)) {
// Failed validation; redisplay form
$this->view->form = $form;
return $this->render('form');
}

$values = $form->getValues();
// now try and authenticate....
}
}

And $values = $form->getValues(); returns an assoc array of values, like so:

Array ( [username] => pokero [password] => 99998883 [login] => Login )

To start off your form, probably best to do the following – set the form action and method:

$form->setAction('/resource/process')
     ->setMethod('post');

Also, to render a form in a view, just stick this in your view:

 <?php echo $this->form ?>

And here is a list of the validators shipped with the Zend framework (frustratingly hard to find on the Zend site :(  ), which I found on this great ZF blog:

  • Alnum
  • Alpha
  • Barcode
  • Between
  • Ccnum
  • Date
  • Digits
  • EmailAddress
  • Float
  • GreaterThan
  • Hex
  • Hostname
  • InArray
  • Int
  • Ip
  • LessThan
  • NotEmpty
  • Regex
  • StringLength

OK, I eventually found them on the Zend site, here.

And a list of the different form elements:

  • Zend_Form_Element_Button
  • Zend_Form_Element_Captcha
  • Zend_Form_Element_Checkbox
  • Zend_Form_Element_File
  • Zend_Form_Element_Hidden
  • Zend_Form_Element_Hash
  • Zend_Form_Element_Image
  • Zend_Form_Element_MultiCheckbox
  • Zend_Form_Element_Multiselect
  • Zend_Form_Element_Password
  • Zend_Form_Element_Radio
  • Zend_Form_Element_Reset
  • Zend_Form_Element_Select
  • Zend_Form_Element_Submit
  • Zend_Form_Element_Text
  • Zend_Form_Element_Textarea

Form styling and decorators

Will be adding more on decorators here, but for now, just a quick one to sort the annoying dt and dd elements that render the labels on different lines in default forms. Just sticking a <br> tag after each <dd> will sort that:

<script type=”text/javascript”>

$(document).ready(function(){

$(”dd”).after( $(”br”) );

});

</script>

And something that really drove me mad for while trying to get the info, is the really simple way of rendering a different than standard view for an action:

$this->_helper->viewRenderer(’popular’);

This was well hidden in the docs on Zend site, they really try and make shit difficult to find, f*ckers. The thing is most of the examples on the Zend site actually assume you are instantiating a View object from scratch – of course it is explained here that in your actions the view object already exists, and there is a much simpler way.

For setting up an automatic email verification system, this example from Cake PHP is great. And a very nicely written piece of code on validating identical fields. Good one on throwing exceptions.

For an example on a basic Zend Form in action, check out this Lotto Results checker form.

Something else you should really do is change the default htaccess from, for example,

RewriteEngine on

RewriteRule !\.(swf|js|ico|gif|jpg|png|css)$ index.php

to the following, as suggested here. This has the major advantage that you can now add non-framework files easily to your public folder.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php

Useful redirect is return $this->_forward(’index’);

And styling Zend forms with Dojo…simple example and worth a look.

And on that link above, here is some nice simple styling, easily expandable for Zend Forms:

/* Zend Form Styling */
.zend_form, .zend_form_dojo {
width: 700px;
margin: 5px auto;
padding: 2px;
overflow: auto;
}

.zend_form dt, .zend_form_dojo dt {
padding: 0;
clear: both;
width: 15%;
float: left;
text-align: left;
margin: 5px 5px 5px 0;
}

.zend_form dd, .zend_form_dojo dd {
padding: 0;
float: left;
width: 68%;
margin: 5px 2px 5px 0;
}

.zend_form p, .zend_form_dojo p {
padding: 0;
margin: 0;
}

.zend_form input, .zend_form_dojo input, .zend_form textarea, .zend_form_dojo textarea {
margin: 0 0 2px 0;
padding: 0;
}
posted under php | 4 Comments »

MySQL Handy Tricks

February17

Let’s get the ball rolling wit this handy one about inserting csv or other delimited data to mysql dbs using INFILEs. This looks  like a basic php version too.

When getting the annoying error 1075 “Incorrect table definition; There can only be one auto column and it must be defined as a key” when trying to add a new primary key, this page has an awesome solution.

Using sprintf() to zerofill variables, handy like so:

$zeroball1 = $this->escape($result->ball1);
$zeroball2 = sprintf("%02d",$zeroball1);
echo $zeroball2;

Zend Framework and MySQL

For counting across rows, first check out the excellent official mysql page on row counting. Then to apply it to Zend framework, this is probably the best starting place for MySql select queries in Zend FW, and I check it regularly. Basically, to run a mysql query like SELECT `results`.`bonus`, COUNT(*) AS `counter` FROM `results` WHERE (draw = 0) GROUP BY `bonus`; you need the following in your model. Note the ‘counter’ => ‘COUNT(*)’ syntax…

$query = $db->select()  

->where("draw = $draw_val")

->from('results',array('bonus','counter' => 'COUNT(*)'))

->group('bonus');

For further and more detailed quality examples of Zend and MySql, this is good. Check out lars’ comment here on counting across multiple tables.

posted under MySQL, php | No Comments »

sms via php

February11

Haven’t had time to check it out yet, but this looks promising, and just what I want to be able to send sms messages to mobiles in Ireland from a php script. http://www.webdeveloper.com/forum/showthread.php?t=170285

Good one here too, based on TM4B, which by all accounts is a good gateway to use. Here’s a list of SMTP delivery gateways to sms, note for Ireland that Meteor are way ahead of the curve, and phonenumber@mymeteor.ie or phonenumber@sms.mymeteor.ie can be used to send sms via SMTP to Meteor customers. Sadly, Vodafone and O2 do not provide this service in Ireland, even though they provide elsewhere in the world. Once again Irish consumers getting the worst of it… :(

And best of all, here’s an open source SMS gateway!! Haven’t tried it fully yet, but if I can get that working will save a tonne of cash!

posted under php | No Comments »

Php links starter

February10

Here’s some starter links:

.

posted under php | 1 Comment »
Newer Entries »