Senza togliere niente all' ennesimo ed eccellente tutorial di Sephiroth, vorrei solo dire che io la parte di esempio in php l' avrei scritta cosi' per 3 motivi:
1 - non c'e' debug e l' aggunta ripetuta di Total e' uno spreco di risorse notevoli ( 100 record ad esempio sarebbero diversi Kb in piu' ... )
2 - preferisco ottimizzare php al meglio e il print e' piu' lento di echo e stampare output un po' alla volta e' piu' lento che accodare tutto l'output in un' unica stringa per poi stamparla con una sola chiamata echo.
3 - il campo descrizione potrebbe avere , come tutti i campi di testo, dei caratteri speciali tra cui proprio la & commerciale, con un urlencode() vi risolvete eventuali "scherzi" in output.
A parte l' aggiunta di una funzione , urlencode(), che appesantisce relativamente ma che e' diciamo "indispenzabile", penso che ottimizzare al limite dell' eccesso la parte in php, soprattutto quando si tratta di farlo interagire con Flash, sia indispensabile, cosi' da rendere l' interazione e l' applicativo il piu' "real time" possibile.
codice:
<?php
// start output declaration
$toFlash = "";
// Include the external file (the variables for the database connection)
include("config.inc.php");
// Connect at the Db using the "config.inc.php" variables
$connessione = mysql_connect($server,$user,$pass) or die( $toFlash = "ERROR=No database connection.\n\r".mysql_error() );
// Select the database
$database = mysql_select_db($database,$connessione) or die( $toFlash = "ERROR=Database {$database} doesn't exist.\n\r".mysql_error() );
// The query text (Select all from the products table in our database)
$select = "SELECT * FROM products";
// Make the query
$result = mysql_query($select);
// Count the rows (total result)
$toFlash = "Total=".mysql_num_rows($result);
// For each result
while($list = mysql_fetch_array($result)){
// Set some variables, using the info get from the database
// $list is the array that contains the Db values
// For example $list["id"] is the value of an "id" field in the Db
$id = $list["id"];
$name = $list["name"];
$stock = $list["quantity"];
$cost = $list["cost"];
$description = urlencode($list["description"]);
$colors = $list["colors"];
$image = $list["image"];
// Print in the browser window a string, in a format that Flash can read
// (Flash read namevar=value&namevar2=valuevar2....)
// We'll see after why we've some "|"
$toFlash .= "&Oggetto{$id}={$name}|{$stock}|{$cost}|{$description}|{$colors}|{$image}";
}
echo $toFlash;
?>
Tutto quello che c'e' da sapere sull' ottimizzazione di PHP lo trovate tra gli articoli di http://freephp.html.it dategli un' occhiata, e' molto interessante, nonche' utile.
P.S. la parte in flash potra' rimanere come era , se non per l'aggiunta di eventuale debug tipo:
codice:
// Create a new LoadVars object, called "catalog"
catalog = new LoadVars()
// Load the vars from the external Php file
catalog.load(php_file)
// When the vars are loaded
catalog.onLoad = function( success ) {
if( success && this["ERROR"]==undefined ) {
for(this.a=1;this.a<=this.total;this.a++){
// Create an array (ObjectN) for each object in the catalog
this["object"+this.a] = this["Oggetto"+(this.a)].split("|")
//Add into the combobox the names of the objects in the catalog
_root.comboname.addItem(this["object"+this.a][0])
// Create into the array ObjectN a new array whith the avaible colors of the object
this["object"+this.a].colors = this["object"+this.a][4].split(",")
// Remove the 4th element of the array (The colors now are in the "colors" array)
this["object"+this.a].splice(4,1)
// Remove "OggettoN", that was a "temporary" object
delete(this["Oggetto"+this.a])
}
}
else if( this["ERROR"]!=undefined ) {
// a trace or a dinamic textField ...
trace( this["ERROR"] );
}
else {
// a trace or a dinamic textField ...
trace( "Error while loading file." );
}
}