PDA

View Full Version : [php] Dichiarazione variabili


gnappoman
11-05-2010, 08:59
:cry:
Ciao sul mio sito sto utilizzando apgform http://apgform.sourceforge.net/, per creare col php una semplice tabella excel a partire da un form
di seguito posto una parte di codice per capirci
Dunque il problema è che il codice fa un array con il contenuto del form, lo mette in una variabile, che poi va scritta direttamente sul file, però se si tratta di un valore numerico lo riporta bene, se si tratta di testo lo riporta bene, se si tratta di un CAP per esempio 00190 fa casino, mi riporta 190 perchè non lo prende come stringa ma come numero.
Dato che sulla variabile di array non ho formule da applicare, posso dire al php di trattare questa variabile sempre come stringa?
<?php
/*
apgForm 1.7

*/
$email = $_POST['email'];
// Change this to the page you want the user to be redirected to after form is submitted (i.e. success.html and error.html)



// Change this to the character(s) you want to be placed instead of line breaks(new line, enter, etc)
$lbChar = " "; // default is a space, you may change it to whatever you want

// Don't change anything below this line

// Determine if the form was sent through the GET methog or the POST method.
if($_POST){
$array = $_POST;
} else if($_GET){
$array = $_GET;
} else {
die("You must Access this file through a form."); // If someone accesses the file directly, it wont work :)
}

//Check if the filename was sent through the form or not
if(!$array['filename']){
// if the filename wasnt sent through the form, it will become form.xls, you can change the default if you want.
$array['filename'] = "form.xls"; //Set the file to save the information in

} else {
if(!(stristr($array['filename'],".xls"))){
$array['filename'] = $array['filename'] . ".xls";
}
}

// Define the tab and carriage return characters:
$tab = "\t"; //chr(9);
$cr = "\n"; //chr(13);

if($array){
// Make The Top row for the excel file and store it in the $header variable
$keys = array_keys($array);
foreach($keys as $key){
if(strtolower($key) != 'filename' && strtolower($key) != 'title'){
$header .= $key . $tab;
}
}
$header .= $cr;

//Make the line with the contents to write to the excel file.
foreach($keys as $key){
if(strtolower($key) != 'filename' && strtolower($key) != 'title'){

$array[$key] = str_replace("\n",$lbChar,$array[$key]);
$array[$key] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$array[$key]);
$array[$key] = str_replace("\\","",$array[$key]);
$array[$key] = str_replace($tab, " ", $array[$key]);
$data .= $array[$key] . $tab ;
}
}
$data .= $cr;

if (file_exists($array['filename'])) {
$final_data = $data; // If the file does exist, then only write the information the user sent
} else {
$final_data = $header . $data; // If file does not exist, write the header(first line in excel with titles) to the file
}
// open the file and write to it

$fp = fopen($array['filename'],"a"); // $fp is now the file pointer to file $array['filename']

if($fp){

fwrite($fp,$final_data); //Write information to the file
fclose($fp); // Close the file
// Success
}

Grazie :fagiano:

vladix
11-05-2010, 09:02
ma lo sai che in america fanno delle competizioni su chi scrive codice funzionante su una sola riga ... ci dovresti andare :fagiano:

EDIT: adesso si vede normale ...bah ... saranno gli effetti di ieri sera ghgh

gnappoman
11-05-2010, 09:45
ahah

vhost87
11-05-2010, 15:29
Hai due strade:

a) O casti la variabile a stringa tramite l'operatore di cast: (string) $variabile.
b) O lo tratti come numero tramite la funzione di php number_format();

Il tuo problema, vediamo se ho capito bene, è che se un qualsiasi utente inserisce 00123 come CAP lui salva un 123, escludendo i primi 2 ZERI, giusto?

Hai sopra le due soluzioni ;)

gnappoman
11-05-2010, 18:46
si il problema e' quello, grazie mille della risposta, adesso provo..

gnappoman
12-05-2010, 13:56
dunque cast mi sembra la funzione giusta per me,
però se guardiamo un attimo il codice

if($_POST){
$array = (string) $_POST;
} else if($_GET){
$array = (string) $_GET;
ho provato a fare questa modifica, ma mi dà errore,
come faccio?
tnx

vhost87
12-05-2010, 14:37
dunque cast mi sembra la funzione giusta per me,
però se guardiamo un attimo il codice

if($_POST){
$array = (string) $_POST;
} else if($_GET){
$array = (string) $_GET;
ho provato a fare questa modifica, ma mi dà errore,
come faccio?
tnx

Mumble... tu stai cercando di 'castare' un Array... forse è piu corretto un qualcosa del tipo (string) $_POST['valore']; :)

gnappoman
12-05-2010, 14:42
Ciao,
grazie,
ma se volessi castare l'array?
devo prima imploderlo?
Grazie

gnappoman
12-05-2010, 14:45
cioè il mio problema è che il php quando mi trova il valore CAP, usa una variabile int, al posto di una stringa, mentre vorrei che tutte le variabili fossero trattate come stringa!

vhost87
12-05-2010, 14:46
Ciao,
grazie,
ma se volessi castare l'array?
devo prima imploderlo?
Grazie

Se hai pochi valori puoi usare la list() di PHP e quindi poi castare i singoli valori, altrimenti puoi castarlo ad oggetto:

$obj = (object) $array;

e poi:

echo (string) $obj->elemento;

se non ricordo male :)

Mel1
09-12-2010, 15:44
Ciao a tutti, chiedo gentilmente un piccolo aiutino, premetto che sono alle primissime armi con il codice PHP, avrei bisogno che chi si collega al mio sito personale, digitando

NOME e Cognome (uso un file html con opzione <Form>)

di verificarlo e quindi di farlo accedere al sito.

non riesco a capire bene l'uso delle variabili in PHP, vorrei usare un sistema di condizioni (IF se uguale a "PIPPO PLUTO" entri, altrimenti non puoi accedere, lo stesso se non digiti nulla) anche perchè è un sito personale e solo alcuni amici e parenti possono accedere.

spero di essermi spiegato
Ringrazio chiunque possa darmi una mano.

spero di essere nel post giusto...se così non fosse chiedo scusa in anticipo, mi sono registrato da poco.
Saluti