gestire numeri

Valshy

Nuovo Utente
14 Mag 2011
23
0
0
Ciao a tutti, vorrei chiedervi se una cosa è fattibile o meno.

Nel mio database ho un campo all'interno della tabella utente, un campo che chiamiamo valore_principale e che è int 11. Questo campo contiene un dato che può andare da 3 ad infinito. Questo numero mi serve per determinare un bonus.
Faccio un esempio:

Valore_principale=3 --> Bonus=1
Valore_principale=4 --> Bonus=1
Valore_principale=5 --> Bonus=2
Valore_principale=5 --> Bonus=2
Valore_principale=6 --> Bonus=3
Valore_principale=6 --> Bonus=3

Come si nota il valore bonus va avanti di due in due. Potrei usare uno switch ma con un numero potenzialmente infinito è impossibile, esiste un altro metodo?
 

dreamer86

Utente Attivo
15 Apr 2011
42
0
0
rafalmachalek.it
Puoi fare per esempio cosi:
PHP:
<?php

$a = range(3,200);

foreach($a as $value)
{
	$bonus = ceil(($value-2)/2);
	echo 'Valore: '.$value.', bonus: '.$bonus.'<br />';
}

?>
 

Eliox

Utente Attivo
25 Feb 2005
4.390
3
0
Un'alternativa potrebbe essere quella di determinare il valore di offset con SELECT MAX determinando il secondo argomento da passare a range().
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
ciao
piccola osservazione
PHP:
<?php
$a = range(3,10);
foreach($a as $value)
{
    $bonus = ceil(($value-2)/2);
    echo 'Valore: '.$value.', bonus: '.$bonus.'<br />';
}
?>

output

Valore: 3, bonus: 1 ok
Valore: 4, bonus: 1 ok
Valore: 5, bonus: 2 ok
Valore: 6, bonus: 2 !!! attenzione: valshy diceva Valore_principale=6 --> Bonus=3
Valore: 7, bonus: 3 questi non so
Valore: 8, bonus: 3
Valore: 9, bonus: 4
Valore: 10, bonus: 4
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
ciao
questa, secondo me, corrisponde meglio a quanto richiesto
PHP:
<?php
$a = range(3,10);//qui come diceva eliox mettere il $max al posto di 10
foreach($a as $value){
	if($value%2 == 0){
		$bonus=(int)(($value-1)/2);
	}else{
		$bonus=(int)($value/2);
	}
	echo 'Valore: '.$value.', bonus: '.$bonus.'<br />';
}
?>
output
Valore: 3, bonus: 1
Valore: 4, bonus: 1
Valore: 5, bonus: 2
Valore: 6, bonus: 2
Valore: 7, bonus: 3
Valore: 8, bonus: 3
Valore: 9, bonus: 4
Valore: 10, bonus: 4
 

dreamer86

Utente Attivo
15 Apr 2011
42
0
0
rafalmachalek.it
ciao
questa, secondo me, corrisponde meglio a quanto richiesto
PHP:
<?php
$a = range(3,10);//qui come diceva eliox mettere il $max al posto di 10
foreach($a as $value){
	if($value%2 == 0){
		$bonus=(int)(($value-1)/2);
	}else{
		$bonus=(int)($value/2);
	}
	echo 'Valore: '.$value.', bonus: '.$bonus.'<br />';
}
?>
output
Il tuo codice produce esattamente lo stesso risultato che il mio. Per quanto riguarda la funzione range(), era solo per fare esempio. Il punto chiave era la formula "$bonus = ceil(($value-2)/2);" per ricavare il bonus partendo dal valore_principale. Non credo che a Valshy serve una funzione range() con il massimo ricavato con SELECT MAX. Penso che voleva solo sapere quale il bonus dopo aver preso un certo valore dal database. Almeno cosi ho capito io ;).
Credo anche che
Valore_principale=3 --> Bonus=1
Valore_principale=4 --> Bonus=1
Valore_principale=5 --> Bonus=2
Valore_principale=5 --> Bonus=2
Valore_principale=6 --> Bonus=3
Valore_principale=6 --> Bonus=3
era pensato come
Valore_principale=3 --> Bonus=1
Valore_principale=4 --> Bonus=1
Valore_principale=5 --> Bonus=2
Valore_principale=6 --> Bonus=2
Valore_principale=7 --> Bonus=3
Valore_principale=8 --> Bonus=3
perché altrimenti non si spiega la ripetizione della stessa riga due volte.
 
Discussioni simili
Autore Titolo Forum Risposte Data
G Qualcuno conosce questo software per gestire le telecamere IP Cam e Videosorveglianza 0
E Come gestire al meglio le immagini legno grande formato Photoshop 0
L Interfaccia grafica per gestire sql server .NET Framework 1
R Plugin per Gestire videocorsi a pagamento WordPress 3
M Come gestire aggiornamenti e modifiche di sito live CMS (Content Management System) 0
S Gestire scelta dropdown con dati da Mysql PHP 2
Tommy03 Gestire dimensioni immagini PHP 4
D Come gestire un singolo checkbox PHP 10
MarcoGrazia Gestire lista campi vuoti in fase di autenticazione jQuery 1
elpirata Gestire permessi accesso alle pagine PHP 3
A [ASP.Net] Gestire il profilo ADMIN in un progetto ASP NET e pattern MVC ASP.NET 0
M [PHP] Come gestire gli utenti online PHP 9
M [php] Come gestire i tags PHP 1
svtmaster professionalità per gestire sito web Offerte e Richieste di Lavoro e/o Collaborazione 4
L [HTML] Gestire movimento GIF HTML e CSS 0
BestSellerMauroTop Software per gestire tanti profili e pagine, e farle crescere in modo autonomo! Annunci servizi di Social Media Marketing 0
W Gestire ccTalk o libreria c++ da php PHP 0
W [PHP] gestire una richiesta curl sul server PHP 0
W Piccolo sistema per gestire alcune periferiche, Java o altro linguaggio? Java 8
bubino8 [WordPress] Gestire SSL (https://) da .htaccess WordPress 9
C [WordPress] Creare e gestire un nuovo sito web WordPress 5
M Gestire ErrorDocument 404 mantenendo lo stesso url Web Server 0
S gestire una tabella mysql PHP 9
R gestire il ritorno di una variabile con success Ajax 2
S Gestire Motion Detection TP-LINK CAMERA NC200 IP Cam e Videosorveglianza 0
A [Javascript] [HTML] Gestire una pagina esterna con 2 iframe Javascript 4
webmachine [PHP] gestire rinnovi PHP 1
P Gestire accesso ad un file xml pubblico PHP 1
D Gestire diversi tipi di pagamento per tipologia di cliente E-Commerce 4
asevenx Cerco plugin per gestire appuntamenti con agenda e campi di selezione WordPress 4
matteoraggi Software per gestire migliaia di permament redirect? SEO e Posizionamento 0
K Gestire response con servlet remota Sviluppo app per Android 1
P Piccolo software per gestire gli utenti. Snippet PHP 5
F Gestire data e ora PHP 6
D Gestire firme digitali con PHP PHP 0
C [Facebook Autolike]Software per gestire più account, mandare like, creare stati, amicizie e altro.. Vendere e Acquistare pubblicita' online 0
D gestire commenti e risposte ... PHP 3
L Gestire password utenti registrati. Quale codifica? PHP 23
asevenx Gestire posizionamento voci sottomenu che si nascondono HTML e CSS 1
N Programma per gestire Cookies secondo disposizioni del Garante Privacy 3/6/2014 G.U. 126 Leggi, Normative e Fisco 0
A Gestire e modificare immagini Java 2
M Gestire la chiusura forzata. Sviluppo app per Android 1
L Sito con doppia lingua. Gestire il cambio PHP 4
felino Wordpress e QTranslate: come gestire i widget WordPress 11
IImanuII Gestire lingua su facebook con php PHP 18
mondrian Gestire un web server su una virtualbox Apache 12
L gestire array per radio-checkbox in ciclo while PHP 2
MarcoGrazia Gestire gruppi e categorie di link con PHP PHP 1
L Button per gestire JS in WebBrowser di VBNet .NET Framework 0
A gestire errori 400-401-403-404-500 con file htaccess PHP 0

Discussioni simili