config_db.php (che è il file da includere)Mentre per il file JS mi limito a dire che viene usato in tutte le pagine in cui c'è un Form con campi obbligatori per controllare e validare questi campi, mostrerò ora i files che verranno richiamati in tutte le pagine del tutorial. Questi files verranno inclusi in quasi tutte le pagine che trattano i dati del DB.
functions.php
js.js
In Altervista, per esempio, è possibile accedere a questi dati da Pannello di controllo andando in:Ecco il file "config_db.php" con le variabili e le matrici che verranno usate nel corso di questo piccolo tutorial.
AlterSito -> Risorse & Upgrade -> DataBase
Nella pagina che si apre scendere alla voce "Come accedere" per trovare:
Hostname/server: localhost
Username: .............
Password: facoltativa (puoi lasciare il campo vuoto)
Database: ............
<?php
$Host = "localhost";
$User = "username";
$Password = "password";
$DBName = "nome_db";
$table = "rubrica";
$campi_rubrica = array (
"id",
"cognome",
"nome",
"nato",
"indirizzo",
"cap",
"citta",
"prov",
"tel1",
"tel2",
"tel3"
);
$nomi_campi = array (
"id" => "non inserire",
"cognome" => "Cognome",
"nome" => "Nome",
"nato" => "Data di nascita (formato: gg/mm/aaaa)",
"indirizzo" => "Indirizzo",
"cap" => "CAP",
"citta" => "Città",
"prov" => "Prov",
"tel1" => "Telefono di casa",
"tel2" => "Cellulare",
"tel3" => "Fax"
);
?>
La seconda matrice, $nomi_campi = array (), che è una matrice associativa, é particolarmente comoda usata in un ciclo di questo genere:<?php
echo "<table>";
for($b=0; $b<count ($campi_rubrica); $b++) {
echo "<tr><td>".$b."</td>\n";
echo "<td>".$campi_rubrica[$b]."</td>\n";
echo "<td>".$nomi_campi[$campi_rubrica[$b]]."</td>\n";
echo "</tr>\n";
}
echo "</table>";
?>
oppure con quest'altro:<?php
echo "<table>";
$nr = 0;
foreach ($nomi_campi as $chiave => $valore) {
echo "<tr><td>".$nr."</td>\n";
echo "<td>".$chiave."</td>\n";
echo "<td>".$valore."</td>\n";
$nr ++;
}
echo "</table>";
?>
che restituiscono questo risultato:# | $campi_rubrica() | $nomi_campi() |
0 | id | non inserire |
1 | cognome | Cognome |
2 | nome | Nome |
3 | nato | Data di nascita (formato: gg/mm/aaaa) |
4 | indirizzo | Indirizzo |
5 | cap | CAP |
6 | citta | Città |
7 | prov | Prov |
8 | tel1 | Telefono di casa |
9 | tel2 | Cellulare |
10 | tel3 | Fax |
<?php
function mostra_campi($tabella) {
global $Host;
global $User;
global $Password;
global $DBName;
$Link = mysql_connect ($Host, $User, $Password);
$fields = mysql_list_fields($DBName, $tabella, $Link);
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {
$field[] = mysql_field_name($fields, $i);
}
mysql_close ($Link);
return $field;
}
// ==============================
function time_serial_da_db($data_letta) {
$data_1 = str_replace("/", "-", $data_letta);
$sub = explode("-", $data_1);
$a = $sub[0];
$m = $sub[1];
$g = $sub[2];
$timestamp_rec = mktime(0,0,0,$m,$g,$a);
return $timestamp_rec;
}
// ==============================
function time_serial($data_letta) {
$data_1 = str_replace("/", "-", $data_letta);
$sub = explode("-", $data_1);
$g = $sub[0];
$m = $sub[1];
$a = $sub[2];
$timestamp_rec = mktime(0,0,0,$m,$g,$a);
return $timestamp_rec;
}
// ==============================
function verify_record($table, $nome, $cognome, $data) {
global $Host;
global $User;
global $Password;
global $DBName;
$Query = "SELECT * FROM ".$table." WHERE nome = '".$nome."' AND cognome = '".$cognome."' AND nato = '".$data."';";
if (!$link = mysql_connect($Host, $User, $Password)) {
die ('errore nella connessione');
}
if (!mysql_select_db($DBName, $link)) {
die('DataBase non trovato');
}
$result = mysql_query($Query, $link);
if (!$result) {
die ("errore nella Query");
}
$num_rec = mysql_num_rows($result);
mysql_close ($link);
if ($num_rec != 0) {
return $num_rec;
} else {
return FALSE;
}
}
// ==============================
function conta_record($tabella) {
global $Host;
global $User;
global $Password;
global $DBName;
$Link = mysql_connect ($Host, $User, $Password);
$Query = "SELECT * FROM ".$tabella;
if (mysql_db_query ($DBName, $Query, $Link)) {
$Result = mysql_db_query ($DBName, $Query, $Link);
$num_rec = mysql_num_rows($Result);
}
mysql_close($Link);
return $num_rec;
}
// ==============================
function connect_db($tabella, $where = false) {
global $Host;
global $User;
global $Password;
global $DBName;
$campi = mostra_campi($tabella);
$Query = "SELECT * FROM ".$tabella;
if ($where) {
$Query .= $where;
}
$Query .= ";";
if (!$link = mysql_connect($Host, $User, $Password)) {
die ('errore nella connessione');
}
if (!mysql_select_db($DBName, $link)) {
die('DataBase non trovato');
}
$result = mysql_query($Query, $link);
if (!$result) {
die ("errore nella query<br />".mysql_error());
}
$num_rec = mysql_num_rows($result);
if ($num_rec == 0) {
echo "nessun record nella tabella";
} else {
echo $num_rec . " record trovati in archivio<br />\n";
echo "<table>\n";
echo "<tr>\n";
echo "<th>Nome</th>\n";
echo "<th>nato</th>\n";
echo "<th>indirizzo</th>\n";
echo "<th>città</th>\n";
echo "<th>Telefoni</th>\n";
echo "</tr>\n";
while ($row = mysql_fetch_array($result)) {
echo "<tr>\n";
echo "<td>" . $row['cognome'] . " ";
echo $row['nome'] . "</td>\n";
$data = $row['nato'];
$timestamp = time_serial_da_db($data);
$nuova_data = date ("d-m-Y",$timestamp);
echo "<td>" . $nuova_data . "</td>\n";
echo "<td>" . $row['indirizzo'] . "</td>\n";
echo "<td>" . $row['cap'] . " ";
echo $row['citta'] . " ";
echo $row['prov'] . "</td>\n";
echo "<td>";
$tel = array();
if ($row['tel1'] != "") {
$tel[] = $row['tel1'];
}
if ($row['tel2'] != "") {
$tel[] = $row['tel2'];
}
if ($row['tel3'] != "") {
$tel[] = $row['tel3'];
}
if (is_array($tel)) {
$telefono = implode("<br />", $tel);
}
echo $telefono;
echo "</td>\n";
echo "</tr>\n";
}
mysql_close ($link);
echo "</table>\n";
}
}
?>
mktime (date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"))
o, più semplicemente:mktime(date("H, i, s, m, d, Y"))
Questo potrebbe essere un possibile valore per la data 5/6/10: 1275767480$where = " WHERE cognome LIKE '%".$lemma."%' OR nome LIKE '%".$lemma."%' OR indirizzo LIKE '%".$lemma."%' OR citta LIKE '%".$lemma."%' order by ".$campo;
Questa stringa va ad aggiungersi alla stringa della Query SELECT nel caso l'argomento $where non sia FALSE.$Query = "SELECT * FROM ".$tabella;
if ($where) {
$Query .= $where;
}
$Query .= ";";
....forse potrebbe tornare utile quando un post diventa Kilometrico ( per testo o sequenza di immagini ecc. ) e non si vuole occupare troppo spazio visivo di una pagina ? :Cartelli2:
- La matrice $nomi_campi() viene usata per scrivere le didascalie a fianco ai controlli che serviranno per scrivere i dati da inserire nel DB o modificare quelli già esistenti.
Codice: Non sei autorizzato a vedere i links. Registrati o Login<?php
$Host = "localhost";
$User = "username";
$Password = "password";
$DBName = "nome_db";
$table = "rubrica";
$campi_rubrica = array (
"id",
"cognome",
"nome",
"nato",
"indirizzo",
"cap",
"citta",
"prov",
"tel1",
"tel2",
"tel3"
);
$nomi_campi = array (
"id" => "non inserire",
"cognome" => "Cognome",
"nome" => "Nome",
"nato" => "Data di nascita (formato: gg/mm/aaaa)",
"indirizzo" => "Indirizzo",
"cap" => "CAP",
"citta" => "Città",
"prov" => "Prov",
"tel1" => "Telefono di casa",
"tel2" => "Cellulare",
"tel3" => "Fax"
);
?>