Funçao php com conexão sql

PHP

15/02/2016

Boa noite, estou configurando um pequeno sistema. Ele faz alguns cálculos buscando dados de algumas tabelas no sql. Ele busca os dados certo e faz as contas quase certo kkkk. Funciona assim:

if ($findCharacters[0] >= 0 && $findCharacters[0] <= 10)
{
$this->Points = (int) ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] )
;
}
elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50)
{
$this->Points = (int)(( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] )
);
}

Na primeira linha ele gera o resultado de 6000 pontos, o que esta correto. Porém na segunda linha (" elseif ") ele gera um resultado todo louco. A intenção do código era a seguinte: Pegar o resultado da linha acima e somar com a nova conta, gerando assim um resultado que muda de 11-50. Na segunda linha ele calcula os pontos até $findCharacters[0] <= 50. Sendo assim o resultado da soma de 0-10 + 11-50 deveria ser de: 25500, só que ele gera um resultado de: 55000. Então gostaria de saber o seguinte, como eu faço para pegar o resultado do "if" anterior e somar com o resultado da conta seguinte no "elseif" (( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] ))

Lembrando que se deixar o elseif dessa forma:
elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50)
{
$this->Points = ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] )
);
Ele gera o resultado conforme essa variação de resetes 11-50, totalizando 19500 pontos.
Desde já agradeço a atenção.
Jefferson

Jefferson

Curtidas 1

Melhor post

Jothaz

Jothaz

17/02/2016

Tenta usar o operador "+=":

if ($findCharacters[0] >= 0 && $findCharacters[0] <= 10) 
{
	$this->Points += (sua expressão)
}
elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50) 
{
	$this->Points += (sua expressão)
}


O que equivale a:

	$this->Points =  $this->Points + (aqui a sua expressão) 


Assim ficará o contador acumulativo.
GOSTEI 2

Mais Respostas

Marcio Araujo

Marcio Araujo

15/02/2016

Use as tags code:


if ($findCharacters[0] >= 0 && $findCharacters[0] <= 10) 
{
$this->Points = (int) ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] )
;
}
elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50) 
{
$this->Points = (int)(( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] )
);
}




elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50) 
{
$this->Points = ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] )
);

GOSTEI 1
Jefferson

Jefferson

15/02/2016

Boa noite, agradeço pela resposta. Fiz o teste e colocando dessa forma:

elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50) 
{
$this->Points = ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] )
);


Ele retorna somente os pontos nesse intervalo de 11 a 50. Eu preciso que ele pegue o resultado do if anterior ( de 0 a 10 ) e some (+) com o resultado do elseif. Por exemplo resultado do if (0 a 10 ) foi de 6000, e o resultado do elseif (11 a 50) foi de 5500, eu preciso que o resultado final seja a soma do if e do elseif, o que iria ser de 11500. E não somente de um intervalo especifico. Ele tem sempre que somar com o if anterior.

Desde ja agradeço a atenção e boa noite.
GOSTEI 0
Jefferson

Jefferson

15/02/2016

Também não deu certo :( Fiz dessa forma:


                            if($findCharacters[0] >= 0 && $findCharacters[0] <= 10) 
                            {
                                $this->Points += (int) ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] )
                                ;
                            }
                            elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50) 
                            {
                                $this->Points += (int)(($findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
                                                      ($findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] )



Vou colocar o código todo aqui para ficar melhor a visualização de vocês. e talvez eu esteja fazendo algo errado. O código se refere a um sistema de reset acumulativo de mu online. Peço desculpas por colocar um codigo grande aqui :D


 switch($PANELUSER_MODULE['RESET']['RESET_MODE'])
                    {
					case 4: 
                            if($findCharacters[0] >= 0 && $findCharacters[0] <= 10) $this->intervalResets = '0-10';
                            elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50) $this->intervalResets = '11-50';
                            elseif($findCharacters[0] >= 51 && $findCharacters[0] <= 100) $this->intervalResets = '51-100';
                            elseif($findCharacters[0] >= 101 && $findCharacters[0] <= 150) $this->intervalResets = '101-150';
                            elseif($findCharacters[0] >= 151 && $findCharacters[0] <= 200) $this->intervalResets = '151-200';
                            elseif($findCharacters[0] >= 201 && $findCharacters[0] <= 300) $this->intervalResets = '201-300';
                            elseif($findCharacters[0] >= 301 && $findCharacters[0] <= 400) $this->intervalResets = '301-400';
                            
                            $this->LevelReset  = $PANELUSER_MODULE['RESET'][$this->intervalResets]['LevelReset'][(int)$findTypeAccount->type];
                            $this->LevelAfter  = $PANELUSER_MODULE['RESET'][$this->intervalResets]['LevelAfter'][(int)$findTypeAccount->type];
                            $this->ZenRequire  = $PANELUSER_MODULE['RESET'][$this->intervalResets]['ZenRequire'][(int)$findTypeAccount->type];
                            $this->CleanItens  = $PANELUSER_MODULE['RESET'][$this->intervalResets]['CleanItens'][(int)$findTypeAccount->type];
                            $this->CleanMagics = $PANELUSER_MODULE['RESET'][$this->intervalResets]['CleanMagics'][(int)$findTypeAccount->type];
                            $this->CleanQuests = $PANELUSER_MODULE['RESET'][$this->intervalResets]['CleanQuests'][(int)$findTypeAccount->type];
                            $this->LimitResets = $PANELUSER_MODULE['RESET']['LimitResets'][(int)$findTypeAccount->type];
                            $this->ResetPoints = true;
                            
                            if($findCharacters[0] >= 0 && $findCharacters[0] <= 10) 
                            {
                                $this->Points += (int) ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] )
                                ;
                            }
                            elseif($findCharacters[0] >= 11 && $findCharacters[0] <= 50) 
                            {
                                $this->Points += (int)(($findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
                                                      ($findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] )
                                );
							}
                            elseif($findCharacters[0] >= 51 && $findCharacters[0] <= 100)
                            {
                                $this->Points += (int)(( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['51-100']['Points'][(int)$findTypeAccount->type] )
                                );
                            }
                            elseif($findCharacters[0] >= 101 && $findCharacters[0] <= 150) 
                            {
                                $this->Points += (int)(( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['51-100']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['101-150']['Points'][(int)$findTypeAccount->type] )
                                );
                            }
                            elseif($findCharacters[0] >= 151 && $findCharacters[0] <= 200) 
                            {
                                $this->Points += (int)(( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['51-100']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['101-150']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['151-200']['Points'][(int)$findTypeAccount->type] )
                                );
                            }
                            elseif($findCharacters[0] >= 201 && $findCharacters[0] <= 300) 
                            {   
                                $this->Points += (int)(( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['51-100']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['101-150']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['151-200']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['201-300']['Points'][(int)$findTypeAccount->type] )
                                );
                            }
                            elseif($findCharacters[0] >= 301 && $findCharacters[0] <= 400) 
                            {
                                $this->Points += (int)(( $findCharacters[0] * $PANELUSER_MODULE['RESET']['0-10']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['11-50']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['51-100']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['101-150']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['151-200']['Points'][(int)$findTypeAccount->type] ) +
                                                      ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['201-300']['Points'][(int)$findTypeAccount->type] ) +
													  ( $findCharacters[0] * $PANELUSER_MODULE['RESET']['301-400']['Points'][(int)$findTypeAccount->type] )
                                );
                            } 
                            break;
                    }



O reset acumulativo tem um intervalo de resetes e cada intervalo desse o char ganha "x" pontos conforme os dados abaixo.

Intervalo de Resets Pontos pelo intervalo Pontos Total do intervalo

0-10 600 6000
11-50 500 19500
51-100 400 19600
101-150 300 14700
151-200 200 9800
201-300 100 9900
301-400 50 4950

Total = 84450
GOSTEI 0
Jothaz

Jothaz

15/02/2016

Mesmo com o fragmento do código ainda é complicado entender o cenário.

Vou dar outra sugestão.

Continue usando o "+=", mas retire o elseif e use somente if.

if($findCharacters[0] >= 0 && $findCharacters[0] <= 10) 
{
 
}
if($findCharacters[0] >= 11 && $findCharacters[0] <= 50) 
{

)
GOSTEI 0
POSTAR