E’ stato rilasciato da ilmeteo.it un plugin per wordpress per le previsioni meteo.
A questo indirizzo http://imeteo.wordpress.com/ è possibile scaricarlo e vedere le istruzioni per installarlo.
Archive for the ‘php’ Category
Rilasciato plugin ilmeteo.it per wordpress
Evitare Cache di una immagine con PHP
Se volete che una immagine venga ripescata dalla cache in PHP è molto semplice, basta accodare una stringa che varia ogni volta all’indirizzo dell’immagine.
Il Browser sarà così ingannato e penserà di dover recuperare una nuova immagine.
Per esempio si potrebbe usare la funzione time che ritorna il timestamp.
'<img src="immagine.jpg?t='.time().'"/>'
Modulo Drupal per nuovo tipo di contenuto
In Drupal è molto semplice sviluppare un modulo che permeta di avere a disposizione un nuovo tipo di contenuto che estenda il classico nodo.
Vediamo come sviluppare un modulo che aggiunga il tipo di contenuto libro.
Creiamo la cartella node_book che andremo a mettere nella cartella modules del nostro sito.
Dentro questa cartella mettiamo il file node_book.info con le specifiche del nodo:
; $Id$
name = node book
description = "nodo libro"
core = 6.x
e il file node_book.module con il codice vero e proprio.
Ecco i vari hook che si devono implementare:
Hook menu
function node_book_menu()
{
$items[] = array( 'path' => 'node_book', 'callback' => 'node_book_page', 'access' => true, 'type' => MENU_CALLBACK );
return $items;
}
hook perm
function node_book_perm()
{
return array('create book node', 'edit own book nodes');
}
hook access
function node_book_access($op, $node, $account) {
if ($op == 'create') {
// Only users with permission to do so may create this node type.
return user_access('create nameofnodetype', $account);
}
// Users who create a node may edit or delete it later, assuming they have the
// necessary permissions.
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own nameofnodetype', $account) && ($account->uid == $node->uid)) {
return TRUE;
}
}
}
hook help '. t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') .' '; case 'admin/build/block': Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually. If you want certain blocks to disable themselves temporarily during high server loads, check the "Throttle" box. You can configure the auto-throttle on the throttle configuration page after having enabled the throttle module. You can configure the behaviour of each block (for example, specifying on which pages and for what users it will appear) by clicking the "configure" link for each block. ', array('@throttle' => url('admin/settings/throttle')));
function node_book_help($path, $arg) {
switch ($path) {
case 'admin/help#block':
return '
return t('
}
}
Ora passiamo agli hook che implementano veramente il nostro modulo:
hook form
function node_book_form(&$node) {
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
}
if ($type->has_body) {
// In Drupal 6, we can use node_body_field() to get the body and filter
// elements. This replaces the old textarea + filter_form() method of
// setting this up. It will also ensure the teaser splitter gets set up
// properly.
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
}
// NOTE in node_example there is some addition code here not needed for this simple node-type
// Now we define the form elements specific to our node type.
$form['editor'] = array(
'#type' => 'textfield',
'#title' => t('Editore'),
'#default_value' => isset($node->editor) ? $node->editor : '',
);
$form['book_author'] = array(
'#type' => 'textfield',
'#title' => t('Autore'),
'#default_value' => isset($node->book_author) ? $node->book_author : '',
);
$form['place'] = array(
'#type' => 'textfield',
'#title' => t('Luogo'),
'#default_value' => isset($node->place) ? $node->place : '',
);
$form['year'] = array(
'#type' => 'textfield',
'#title' => t('Anno'),
'#default_value' => isset($node->year) ? $node->year : '',
);
$form['book_type'] = array(
'#type' => 'textfield',
'#title' => t('Tipo'),
'#default_value' => isset($node->book_type) ? $node->book_type : '',
);
$form['magazine'] = array(
'#type' => 'textfield',
'#title' => t('Rivista'),
'#default_value' => isset($node->magazine) ? $node->magazine : '',
);
return $form;
}
hook insert
/**
* Implementation of hook_insert().
*
* As a new node is being inserted into the database, we need to do our own
* database inserts.
*/
function node_book_insert($node) {
db_query("INSERT INTO {node_book} (vid, nid, editor, book_author, place, year, book_type, magazine) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s')", $node->vid, $node->nid,$node->editor,$node->book_author,$node->place,$node->year,$node->book_type,$node->magazine);
}
hook update
/**
* Implementation of hook_update().
*
* As an existing node is being updated in the database, we need to do our own
* database updates.
*/
function node_book_update($node) {
// if this is a new node or we're adding a new revision,
if ($node->revision) {
node_book_insert($node);
}
else {
db_query("UPDATE {node_book} SET editor='%s', book_author='%s', place='%s', year='%s', book_type='%s', magazine='%s' WHERE vid = %d", $node->editor, $node->book_author, $node->place, $node->year, $node->book_type, $node->magazine, $node->vid);
}
}
hook nodeapi
/**
* Implementation of hook_nodeapi().
*
* When a node revision is deleted, we need to remove the corresponding record
* from our table. The only way to handle revision deletion is by implementing
* hook_nodeapi().
*/
function node_book_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'delete revision':
// Notice that we're matching a single revision based on the node's vid.
db_query('DELETE FROM {node_book} WHERE vid = %d', $node->vid);
break;
}
}
hook_delete
/**
* Implementation of hook_delete().
*
* When a node is deleted, we need to remove all related records from out table.
*/
function node_book_delete($node) {
// Notice that we’re matching all revision, by using the node’s nid.
db_query(’DELETE FROM {node_book} WHERE nid = %d’, $node->nid);
}
hook load
/**
* Implementation of hook_load().
*
* Now that we've defined how to manage the node data in the database, we
* need to tell Drupal how to get the node back out. This hook is called
* every time a node is loaded, and allows us to do some loading of our own.
*/
function node_book_load($node) {
$additions = db_fetch_object(db_query('SELECT editor,book_author,place,year,book_type,magazine FROM {node_book} WHERE vid = %d', $node->vid));
return $additions;
}
hook view
/**
* Implementation of hook_view().
*
* This is a typical implementation that simply runs the node text through
* the output filters.
*/
function node_book_view($node, $teaser = FALSE, $page = FALSE) {
$node = node_prepare($node, $page);
$node->content['myfield'] = array(
'#value' => 'Editore: '.$node->editor.'<br />'.'Autore: '.$node->book_author.'<br >'.'Luogo: '.$node->place.'<br >'.'Anno: '.$node->year.'<br >'.'Tipologia: '.$node->book_type.'<br >',
'#weight' => 1,
);
return $node;
}
Ora manca solo il DB, ecco la tabella per i campi in più del nostro nodo
CREATE TABLE node_book (
vid int(10) unsigned NOT NULL default ‘0′,
nid int(10) unsigned NOT NULL default ‘0′,
editor varchar(255) NOT NULL default ”,
book_author varchar(255) NOT NULL default ”,
place varchar(255),
year varchar(255),
book_type varchar(255),
magazine varchar(255),
PRIMARY KEY (vid, nid),
KEY `node_type_book_nid` (nid)
)
Dopo aver creato la tabella e attivato il modulo avrete un nuovo tipo di contenuto disponibile.
PDO, Abstraction Layer
Finalmente con la versione 5.1 PHP ha un nuovo livello di astrazione per la connettività con i database veramente valido.
PDO, PHP Data Objects
Ho deciso di studiarlo un po’:
Creo un Database di prova:
DROP TABLE IF EXISTS first_table;
--CREATE TABLE first_table (
--id INT(10) NOT NULL AUTO_INCREMENT,
--name VARCHAR(255) NOT NULL,
--surname VARCHAR(255) NOT NULL,
--PRIMARY KEY (id)
--) TYPE=MyISAM;
CREATE TABLE first_table (
id INT(10) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
surname VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) TYPE=innoDB;
INSERT INTO first_table (name,surname) VALUES ('matteo','magni');
INSERT INTO first_table (name,surname) VALUES ('john','starks');
INSERT INTO first_table (name,surname) VALUES ('michael','jordan');
INSERT INTO first_table (name,surname) VALUES ('kobe','bryant');
INSERT INTO first_table (name,surname) VALUES ('earvin','johnson');
INSERT INTO first_table (name,surname) VALUES ('larry','bird');
INSERT INTO first_table (name,surname) VALUES ('patrick','ewing');
INSERT INTO first_table (name,surname) VALUES ('reggie','miller');
INSERT INTO first_table (name,surname) VALUES ('ron','harper');
INSERT INTO first_table (name,surname) VALUES ('anfernee','hardaway');
INSERT INTO first_table (name,surname) VALUES ('tim','hardaway');
INSERT INTO first_table (name,surname) VALUES ('mitch','richmond');
INSERT INTO first_table (name,surname) VALUES ('jason','williams');
INSERT INTO first_table (name,surname) VALUES ('allen','iverson');
INSERT INTO first_table (name,surname) VALUES ('dwayne','wade');
Lo importo così dopo aver creato il DB pdo
mysql -p -D pdo < pdo.sql
Per le prove utilizzerò sia la versione con tabella MyISAM che quella con tabella InnoDB, la differenza è che per la prima non sono disponibili le transazioni.
Usiamo le eccezioni per connetterci al DB
//USO le eccezioni
$string_dsn = 'mysql:host=localhost;dbname=pdo'; // mysql
$string_username = 'user';
$string_password = 'password';
try {
$mypdo = new PDO($string_dsn, $string_username, $string_password);
}
catch(PDOException $e) {
echo 'Errore di connessione: '.$e->getMessage();
}
Query fatta con errore, mi restituisce il numero dell’errore:
$mypdo->exec('SELECT * FROM SELECT'); // errore generato appositamente
echo 'Errore N: '.$mypdo->errorCode();
Se la uso così è molto utile, mando io il messaggio che voglio:
if($mypdo->errorCode() !== '' || !$mypdo->exec('SELECT * FROM SELECT'))
echo 'errore nella query SELECT * FROM SELECT';
Così mi ritorna un array di tre elementi con le notizie sull’errore:
var_dump($mypdo->errorInfo());
//array di tre elementi
if(count($mypdo->errorInfo()) == 1) // ... tutto ok
if(count($mypdo->errorInfo()) > 1) { // ... ci sono errori
$errorinfo = $mypdo->errorInfo();
echo $errorinfo[2]; // stringa con l' errore
}
QUERY semplice senza preparare niente,
Mi ritorna i dati accessibili sia con il numero della colonna (partendo da 0) che con il nome della colonna
Query senza parametri aggiuntivi per stabilire che Fetch usare:
foreach($mypdo->query('SELECT * FROM first_table') as $row)
{
echo '--------------<br />';
echo '1: '.$row[0].' - 2: '.$row[1].'<br />';
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
mi ritorna i dati accessibili sia con il numero della colonna (partendo da 0) che con il nome della colonna.
Esplicito il tipo di Fetch con il parametro PDO_FETCH_*
PDo::FETCH_BOTH #usatelo come mysql fetch_array, risultato duplicato, quello di default
foreach($mypdo->query('SELECT * FROM first_table', PDO::FETCH_BOTH) as $row)
{
echo '--------------<br />';
echo '1: '.$row[0].' - 2: '.$row[1].'<br />';
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
PDO::FETCH_ASSOC #usatelo come mysql_fetch_assoc
foreach($mypdo->query('SELECT * FROM first_table', PDO::FETCH_ASSOC) as $row)
{
echo '--------------<br />';
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
PDO::FETCH_NUM #analogo a mysql_fetch_row
foreach($mypdo->query('SELECT * FROM first_table', PDO::FETCH_NUM) as $row)
{
echo '--------------<br/>';
echo '1: '.$row[0].' - 2: '.$row[1].'<br/>';
}
PDO::FETCH_OBJ #ritorna un oggetto con le proprieta’ che hanno il nome dei campi
foreach($mypdo->query('SELECT * FROM first_table', PDO::FETCH_OBJ) as $row)
{
echo '--------------<br />';
echo 'id->'.$row->id.' - name-> '.$row->name.'<br />';
}
PDO::FETCH_LAZY #combina *_BOTH e *_OBJ
foreach($mypdo->query('SELECT * FROM first_table', PDO::FETCH_LAZY) as $row)
{
echo '--------------<br />';
echo '1: '.$row[0].' - 2: '.$row[1].'<br />';
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
echo 'id->'.$row->id.' - name-> '.$row->name.'<br />';
}
PDO::FETCH_BOUND #Lo vediamo con bindColumn,
restituiti come attributi alle variabili PHP
Per Evitare problemi con DB case sensitive e non case sensitive
Setto che i nomi delle colonne siano restituiti sempre minuscoli
$mypdo->setAttribute(PDO::CASE_LOWER);
/*
* Psso usare anche
* PDO::CASE_UPPER
* PDO::CASE_NATURAL come sono fornite dal driver, metodo di default
*/
oppure si usa sempre FETCH_NUM
Utilizzo delle Transazioni
inizio la transazione
Su Mysql devo usare tabelle InnoDB perchè MyIsam non supportano le transazioni
Però…
The following example begins a transaction and issues two statements that
modify the database before rolling back the changes.
On MySQL, however, the DROP TABLE statement automatically commits the
transaction so that none of the changes in the transaction are rolled back.
Quindi niente drop per una transazione.
$mypdo->beginTransaction();
$mypdo->exec('DROP TABLE first_table');
$mypdo->rollBack(); // la tabella mytable rimarra' invariata :-)
come detto su MySQL la cancella lo stesso.
$mypdo->beginTransaction();
$mypdo->exec("INSERT INTO first_table (name,surname) VALUES ('gianni','morandi')");
$mypdo->rollBack(); // la tabella mytable rimarra' invariata :-)
$mypdo->beginTransaction();
$mypdo->exec("INSERT INTO first_table (name,surname) VALUES ('gianni','morandi')");
$mypdo->commit(); // la tabella mytable rimarra' invariata :-)
mi inserisce il nuovo valore.
Prepared Statement
Metodo Fetch
Metodo implementato nello statement che è in grado di spostare il cursore dei risultati di una query in un ciclo.
Gli posso passare i parametri come a query.
/*
* PDO::FETCH_ASSOC, PDO::FETCH_BOTH, PDO::FETCH_BOUND, PDO::FETCH_LAZY,
* PDO::FETCH_OBJ, PDO::FETCH_NUM
*/
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare('UPDATE first_table SET name = "pippo" WHERE id = 17');
// eseguo la query
$stpdo->execute();
$mypdo->commit();
echo 'SELECT';
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare('SELECT * FROM first_table');
// eseguo la query
$stpdo->execute();
//stampo a video il contenuto dell'array che mi crea fetch
while ($row=$stpdo->fetch())
{
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
$mypdo->commit();
La potenza prepare e execute
Potenza del metodo prepare
$mypdo->beginTransaction();
$stpdo = $mypdo->prepare('UPDATE first_table SET name = ? WHERE id = ?');
//i punti interrogativi saranno sostituiti da execute
$stpdo->execute(array('gianni', 17));
$mypdo->commit();
echo 'SELECT';
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare('SELECT * FROM first_table');
// eseguo la query
$stpdo->execute();
//stampo a video il contenuto dell'array che mi crea fetch
while ($row=$stpdo->fetch())
{
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
$mypdo->commit();
Posso preparare la query una volta e fare poi i vari inserimenti.
potenza prepare e execute ancora
$mypdo->beginTransaction();
$stpdo = $mypdo->prepare('UPDATE first_table SET name = :name WHERE id = :id');
//i punti interrogativi non sempre sono chiari, così lo è di più
$stpdo->execute(array(':name'=>'marcello',':id'=>17));
$mypdo->commit();
echo 'SELECT';
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare('SELECT * FROM first_table');
// eseguo la query
$stpdo->execute();
//stampo a video il contenuto dell'array che mi crea fetch
while ($row=$stpdo->fetch())
{
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
$mypdo->commit();
Metodo fetchAll
ritorna un array le righe del resultset
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare('SELECT * FROM first_table');
// eseguo la query
$stpdo->execute();
$row=$stpdo->fetchAll();
//stampo a video il contenuto dell'array che mi crea fetch
var_dump($row);
$mypdo->commit();
Metodo fetchAll
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare('SELECT * FROM first_table');
// eseguo la query
$stpdo->execute();
$row=$stpdo->fetchAll(PDO::FETCH_OBJ);
//così ho un array di oggetti
//stampo a video il contenuto dell'array che mi crea fetch
var_dump($row);
$mypdo->commit();
Metodi bind
bindParam
$id=10;
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare("SELECT * FROM first_table WHERE id = ? ");
$stpdo->bindParam(1,$id, PDO::PARAM_INT);
$stpdo->execute();
while ($row=$stpdo->fetch())
{
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
$mypdo->commit();
Oppure:
$id=10;
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare("SELECT * FROM first_table WHERE id = :id ");
$stpdo->bindParam(':id',$id, PDO::PARAM_INT);
$stpdo->execute();
while ($row=$stpdo->fetch())
{
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
$id=13;
$stpdo->bindParam(':id',$id, PDO::PARAM_INT);
$stpdo->execute();
while ($row=$stpdo->fetch())
{
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
$mypdo->commit();
Così ho preparato la query una volta sola, ed ho legato :id al valore della variabile $id.
bindValue
$id=10;
$mypdo->beginTransaction();
$stpdo = $mypdo->prepare('SELECT * FROM first_table WHERE id < ? ');
$stpdo->bindValue(1, $id, PDO::PARAM_INT);
$stpdo->execute();
while ($row=$stpdo->fetch())
{
echo 'id: '.$row['id'].' - name: '.$row['name'].'<br />';
}
$mypdo->commit();
bindColumn
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare("SELECT * FROM first_table ");
$stpdo->bindColumn(1,$id);
$stpdo->bindColumn(2,$name);
$stpdo->bindColumn(3,$surname);
$stpdo->execute();
while($row = $stpdo->fetch(PDO::FETCH_BOUND))
echo $id.' '.$name.' '.$surname.'<br />'; // stampo i nomi
$mypdo->commit();
In pratica specifico prima come devono chiamarsi i dati letti da una query e di che tipo devono essere.
a variabile $id non l’avevo mai creata, nonostante ciò non ho avuto nessun notice o warning perche’ questa viene popolata e inizializzata ( se inesistente ) automaticamente all’ interno della chiamata a bindColumn.
Poi la variabile viene automaticamente riassegnata ad ogni ciclo del while ,
permettendoci di evitare i soliti $row[N] per il fetch_row o $row['key'] per il fetch_assoc.
bindColumn
$mypdo->beginTransaction();
// preparo la query
$stpdo = $mypdo->prepare("SELECT * FROM first_table ");
$stpdo->bindColumn(1,$id, PDO::PARAM_INT);
$stpdo->bindColumn(2,$name, PDO::PARAM_STR);
$stpdo->bindColumn(3,$surname, PDO::PARAM_STR);
$stpdo->execute();
while($row = $stpdo->fetch(PDO::FETCH_BOUND))
echo $id.' '.$name.' '.$surname.'<br />'; // stampo i nomi
$mypdo->commit();
i parametri non modificano il risultato, ma “forse” ottimizzano le query
così abbiamo visto anche come funizona PDO::FETCH_BOUND
E se volessi usare Postgres invece di Mysql?
Semplice creo il DB pgsql come quello mysql e basta che cambi queste rige con i diversi parametri di connessione:
$string_dsn = 'pgsql:host=localhost;dbname=pdo'; // pgsql
$string_username = 'user';
$string_password = 'password';
ed il gioco è fatto, bello no?!
Lo studio l’ho fatto partendo da questi link:
http://it2.php.net/pdo
Pillola PDO
Gabba Gabba Hey
Bonzo
PHP day 2008
Venerdì scorso ho partecipato al primo giorno del PHP day 2008.
Rispetto allo scorso anno l’evento è risultato molto più interessante, forse anche per una mia maggior predisposizione allo scambio di opinioni e battute con i partecipanti. Purtroppo non ho potuto partecipare ai workshop del sabato, speriamo nel prossimo anno.
Ecco il talk che ho apprezzato di più:
Nel canale Enterprise si è assistito a un piccolo confronto tra tre CMS tra i più diffusi.
Avendoli utilizzati tutti e tre, ho apprezzato e seguito con interesse il confronto e la mia piccola conclusione è che Drupal è il miglior compromesso tra potenza e semplicità (ovviamente grazie a views e cck), eZpublish è potentissimo ma adatto a grosse realtà (con le sue 102 tabelle e tutto il resto è forse troppo esoso di risorse per un semplice blog), mentre Joomla rimane un po’ staccato avendo una flessibilità minore rispetto agli altri due, nonostante ciò è quello con un community maggiore, quindi è sempre da tenere d’occhio.
Oltre a questo ho fatto in tempo a sentire il talk di Daniele Teti sui Design patterns e quello di Simone Carletti sullo zend framework ed i web services. Quest’ultimo è stato ricco di esempi e molto interessante, non a caso ho scaricato lo Zend il giorno dopo e penso che, almeno come libreria comicerò ad usarlo. Uno dei suoi vantaggi è proprio il fatto di poter essere usato a pezzi, non costringendo il programmatore a sapere tutto sul framework.
Dal Blog di Fullo un po’ di Link su foto, video e altro dell’evento.
Fullo Blog
Ci si vede nel 2009
Gabba Gabba Hey
Bonzo

Social