php update successful but not updating the databse - php

I got problem with this code. I've tried so many possible solution but still no update in phpmyadmin. The code run successfully through the footer since it say "Successfully Updated" but still no changes
<?php
if ($_POST){
$con = mysql_connect("localhost", "root", "pass123");
if (!$con){die('Could not connect:' . mysql_error());}
mysql_select_db("testing", $con) or die('Error:' . mysql_error());
$entry = $_GET['entry'];
$branch_id = $_POST['branch_id'];
$outletname = $_POST['outletname'];
$date = $_POST['date'];
$q11a = $_POST['q11a'];
$q12a = $_POST['q12a'];
$q13a = $_POST['q13a'];
$q14a = $_POST['q14a'];
$q15a = $_POST['q15a'];
$q16a = $_POST['q16a'];
$q17a = $_POST['q17a'];
$q18a = $_POST['q18a'];
$q19a = $_POST['q19a'];
$q20a = $_POST['q20a'];
$q21a = $_POST['q21a'];
$q22a = $_POST['q22a'];
$q23a = $_POST['q23a'];
$q24a = $_POST['q24a'];
$q25a = $_POST['q25a'];
$q26a = $_POST['q26a'];
$q27a = $_POST['q27a'];
$q28a = $_POST['q28a'];
$q29a = $_POST['q29a'];
$q30a = $_POST['q30a'];
$q31a = $_POST['q31a'];
$q32a = $_POST['q32a'];
$q33a = $_POST['q33a'];
$q34a = $_POST['q34a'];
$q35a = $_POST['q35a'];
$q36a = $_POST['q36a'];
$q37a = $_POST['q37a'];
$q38a = $_POST['q38a'];
$q39a = $_POST['q39a'];
$q40a = $_POST['q40a'];
$q41a = $_POST['q41a'];
$q42a = $_POST['q42a'];
$q43a = $_POST['q43a'];
$q44a = $_POST['q44a'];
$q45a = $_POST['q45a'];
$q46a = $_POST['q46a'];
$q47a = $_POST['q47a'];
$q48a = $_POST['q48a'];
$q49a = $_POST['q49a'];
$q50a = $_POST['q50a'];
$q51a = $_POST['q51a'];
$q52a = $_POST['q52a'];
$q53a = $_POST['q53a'];
$q54a = $_POST['q54a'];
$q55a = $_POST['q55a'];
$q56a = $_POST['q56a'];
$q57a = $_POST['q57a'];
$q58a = $_POST['q58a'];
$q59a = $_POST['q59a'];
$q60a = $_POST['q60a'];
$q61a = $_POST['q61a'];
$q62a = $_POST['q62a'];
$q63a = $_POST['q63a'];
$q64a = $_POST['q64a'];
$q65a = $_POST['q65a'];
$q66a = $_POST['q66a'];
$q67a = $_POST['q67a'];
$q68a = $_POST['q68a'];
$q69a = $_POST['q69a'];
$q70a = $_POST['q70a'];
$q71a = $_POST['q71a'];
$q72a = $_POST['q72a'];
$q73a = $_POST['q73a'];
$q74a = $_POST['q74a'];
$q75a = $_POST['q75a'];
$q76a = $_POST['q76a'];
$q78a = $_POST['q78a'];
$q78a1 = $_POST['q78a1'];
$q79a = $_POST['q79a'];
$q79a1 = $_POST['q79a1'];
$q80a = $_POST['q80a'];
$q81a = $_POST['q81a'];
$q82a = $_POST['q82a'];
$q83a = $_POST['q83a'];
$q84a = $_POST['q84a'];
$q85a = $_POST['q85a'];
$timein = $_POST['timein'];
$HOR = $_POST['HOR'];
$MODa = $_POST['MODa'];
$KL = $_POST['KL'];
$ateambox = $_POST['ateambox'];
$box35 = $_POST['box35'];
$box36 = $_POST['box36'];
$box41 = $_POST['box41'];
$box50 = $_POST['box50'];
$box53 = $_POST['box53'];
$box54 = $_POST['box54'];
$box55 = $_POST['box55'];
$box61a = $_POST['box61a'];
$box61aa = $_POST['box61aa'];
$box61b = $_POST['box61b'];
$box61bb = $_POST['box61bb'];
$box61c = $_POST['box61c'];
$box61cc =$_POST['box61cc'];
$box61d = $_POST['box61d'];
$box61dd = $_POST['box61dd'];
$box61e = $_POST['box61e'];
$box61ee = $_POST['box61ee'];
$box73a = $_POST['box73a'];
$box73b = $_POST['box73b'];
$box73c = $_POST['box73c'];
$box74a = $_POST['box74a'];
$box74b = $_POST['box74b'];
$box74c = $_POST['box74c'];
$box74d = $_POST['box74d'];
$box74e = $_POST['box74e'];
$box74f = $_POST['box74f'];
$box74g = $_POST['box74g'];
$box74h = $_POST['box74h'];
$box74i = $_POST['box74i'];
$box74j = $_POST['box74j'];
$box75a = $_POST['box75a'];
$box75b = $_POST['box75b'];
$box75c = $_POST['box75c'];
$box75d = $_POST['box75d'];
$box75e = $_POST['box75e'];
$box75f = $_POST['box75f'];
$box75g = $_POST['box75g'];
$box75h = $_POST['box75h'];
$box75i = $_POST['box75i'];
$box75j = $_POST['box75j'];
$box75k = $_POST['box75k'];
$box75l = $_POST['box75l'];
$box75m = $_POST['box75m'];
$box75n = $_POST['box75n'];
$box75o = $_POST['box75o'];
$box75p = $_POST['box75p'];
$box75q = $_POST['box75q'];
$box75r = $_POST['box75r'];
$box78a = $_POST['box78a'];
$box78b = $_POST['box78b'];
$box78c = $_POST['box78c'];
$box78d = $_POST['box78d'];
$box78e = $_POST['box78e'];
$box78f = $_POST['box78f'];
$box78g = $_POST['box78g'];
$box78h = $_POST['box78h'];
$box78i = $_POST['box78i'];
$box78j = $_POST['box78j'];
$box78k = $_POST['box78k'];
$box78l = $_POST['box78l'];
$box78m = $_POST['box78m'];
$box79a = $_POST['box79a'];
$box79b = $_POST['box79b'];
$box79c = $_POST['box79c'];
$box79d = $_POST['box79d'];
$box91a = $_POST['box91a'];
$box91b = $_POST['box91b'];
$box91c = $_POST['box91c'];
$box92a = $_POST['box92a'];
$box92b = $_POST['box92b'];
if ($q11a == -1){
$naarrival = $naarrival + 1;
}
if ($q12a == -1){
$naarrival = $naarrival + 1;
}
if ($q14a == -1){
$naarrival = $naarrival + 1;
}
if ($q15a == -1){
$naarrival = $naarrival + 1;
}
if ($q26a == -1){
$nadining = $nadining + 1;
}
if ($q31a == -1){
$nadining = $nadining + 1;
}
if ($q32a == -1){
$nadining = $nadining + 1;
}
if ($q44a == -1){
$nadining = $nadining + 1;
}
if ($q45a == -1){
$nadining = $nadining + 1;
}
if ($q78a == -1){
$naproduct = $naproduct + 1;
}
if ($q81a == -1){
$naproduct = $naproduct + 1;
}
if ($q83a == -1){
$napayment = $napayment + 1;
}
$arrival_na1 = $naarrival;
$arrival_ppna1 = 14 - $arrival_na1;
$totalarrival = $q11a + $q12a + $q13a + $q14a + $q15a + $q16a + $q17a + $q18a;
$arrival_score1 = $totalarrival / $arrival_ppna1 *100;
$dining_na1 = $nadining;
$dining_ppna1 = 31 - $dining_na1;
$totaldining = $q19a + $q20a + $q21a + $q22a + $q23a + $q24a + $q25a + $q26a + $q27a + $q28a + $q29a + $q30a + $q31a + $q32a + $q33a + $q34a + $q35a + $q36a + $q37a + $q38a + $q39a + $q40a + $q41a + $q42a + $q43a + $q44a + $q45a + $q46a + $q47a + $q48a + $q49a;
$dining_score1 = $totaldining / $dining_ppna1 *100;
$ordering_na1 = $naordering;
$ordering_ppna1 = 28 - $ordering_na1;
$totalordering = $q50a + $q51a + $q52a + $q53a + $q54a + $q55a + $q56a + $q57a;
$ordering_score1 = $totalordering / $ordering_ppna1 *100;
$product_na1 = $naproduct;
$product_ppna1 = 47 - $product_na1;
$totalproduct = $q58a + $q59a + $q60a + $q61a + $q62a + $q63a + $q64a + $q65a + $q66a + $q67a + $q68a + $q69a + $q70a + $q71a + $q72a + $q73a + $q74a + $q75a + $q76a + $q78a + $q78a1 + $q79a + $q79a1 + $q80a + $q81a;
$product_score1 = $totalproduct / $product_ppna1 *100;
$payment_na1 = $napayment;
$payment_ppna1 = 6 - $payment_na1;
$totalpayment = $q82a + $q83a + $q84a + $q85a;
$payment_score1 = $totalpayment / $payment_ppna1 *100;
$totalattentiveness1 = ($q16a + $q18a + $q51a + $q52a + $q55a + $q57a + $q85a) / 19 * 100;
$totalaccuracy1 = ($q50a + $q54a + $q56a) / 8 * 100;
$totalspeed1 = $q53a / 8 * 100;
$totalfriendliness1 = ($q17a + $q84a) / 6 * 100;
$totalpresentation1 = ($q63a + $q64a + $q67a + $q72a) / 5 * 100;
$totaltaste1 = ($q59a + $q61a + $q70a + $q71a + $q79a + $q80a) / 14 * 100;
$totalquality1 = ($q58a + $q60a + $q62a + $q65a + $q66a + $q68a + $q69a + $q73a + $q74a + $q75a + $q76a + $q78a + $q78a1 + $q79a1 + $q81a) / 28 * 100;
$total1 = $arrival_na1 + $dining_na1 + $ordering_na1 + $product_na1 + $payment_na1;
$total2 = $arrival_ppna1 + $dining_ppna1 + $ordering_ppna1 + $product_ppna1 + $payment_ppna1;
$total3 = $totalarrival + $totaldining + $totalordering + $totalproduct + $totalpayment;
$percentage = ($total3 / $total2) * 100;
if ($percentage >= 86) {
$grade = "A";
} else if ($percentage >= 70) {
$grade = "B";
} else if ($percentage >= 55) {
$grade = "C";
} else {
$grade = "D";
}
mysql_select_db("testing", $con) or die('Error:' .mysql_error());
$insert_query = "UPDATE a_team SET
date='$date',
q11a='$q11a',
q12a='$q12a',
q13a='$q13a',
q14a='$q14a',
q15a='$q15a',
q16a='$q16a',
q17a='$q17a',
q18a='$q18a',
q19a='$q19a',
q20a='$q20a',
q21a='$q21a',
q22a='$q22a',
q23a='$q23a',
q24a='$q24a',
q25a='$q25a',
q26a='$q26a',
q27a='$q27a',
q28a='$q28a',
q29a='$q29a',
q30a='$q30a',
q31a='$q31a',
q32a='$q32a',
q33a='$q33a',
q34a='$q34a',
q35a='$q35a',
q36a='$q36a',
q37a='$q37a',
q38a='$q38a',
q39a='$q39a',
q40a='$q40a',
q41a='$q41a',
q42a='$q42a',
q43a='$q43a',
q44a='$q44a',
q45a='$q45a',
q46a='$q46a',
q47a='$q47a',
q48a='$q48a',
q49a='$q49a',
q50a='$q50a',
q51a='$q51a',
q52a='$q52a',
q53a='$q53a',
q54a='$q54a',
q55a='$q55a',
q56a='$q56a',
q57a='$q57a',
q58a='$q58a',
q59a='$q59a',
q60a='$q60a',
q61a='$q61a',
q62a='$q62a',
q63a='$q63a',
q64a='$q64a',
q65a='$q65a',
q66a='$q66a',
q67a='$q67a',
q68a='$q68a',
q69a='$q69a',
q70a='$q70a',
q71a='$q71a',
q72a='$q72a',
q73a='$q73a',
q74a='$q74a',
q75a='$q75a',
q76a='$q76a',
q78a='$q78a',
q78a1='$q78a1',
q79a='$q79a',
q79a1='$q79a1',
q80a='$q80a',
q81a='$q81a',
q82a='$q82a',
q83a='$q83a',
q84a='$q84a',
q85a='$q85a',
totalarrival='$totalarrival',
arrival_score1='$arrival_score1',
totaldining='$totaldining',
dining_score1='$dining_score1',
totalordering='$totalordering',
ordering_score1='$ordering_score1',
totalproduct='$totalproduct',
product_score1='$product_score1',
totalpayment='$totalpayment',
payment_score1='$payment_score1',
totalattentiveness1='$totalattentiveness1',
totalaccuracy1='$totalaccuracy1',
totalspeed1='$totalspeed1',
totalfriendliness1='$totalfriendliness1',
totalpresentation1='$totalpresentation1',
totaltaste1='$totaltaste1',
totalquality1='$totalquality1',
timein='$timein',
HOR='$HOR',
MODa='$MODa',
KL='$KL',
ateambox='$ateambox',
box35='$box35',
box36='$box36',
box41='$box41',
box50='$box50',
box53='$box53',
box54='$box54',
box55='$box55',
box61a='$box61a',
box61aa='$box61aa',
box61b='$box61b',
box61bb='$box61bb',
box61c='$box61c',
box61cc='$box61cc',
box61d='$box61d',
box61dd='$box61dd',
box61e='$box61e',
box61ee='$box61ee',
box73a='$box73a',
box73b='$box73b',
box73c='$box73c',
box74a='$box74a',
box74b='$box74b',
box74c='$box74c',
box74d='$box74d',
box74e='$box74e',
box74f='$box74f',
box74g='$box74g',
box74h='$box74h',
box74i='$box74i',
box74j='$box74j',
box75a='$box75a',
box75b='$box75b',
box75c='$box75c',
box75d='$box75d',
box75e='$box75e',
box75f='$box75f',
box75g='$box75g',
box75h='$box75h',
box75i='$box75i',
box75j='$box75j',
box75k='$box75k',
box75l='$box75l',
box75m='$box75m',
box75n='$box75n',
box75o='$box75o',
box75p='$box75p',
box75q='$box75q',
box75r='$box75r',
box78a='$box78a',
box78b='$box78b',
box78c='$box78c',
box78d='$box78d',
box78e='$box78e',
box78f='$box78f',
box78g='$box78g',
box78h='$box78h',
box78i='$box78i',
box78j='$box78j',
box78k='$box78k',
box78l='$box78l',
box78m='$box78m',
box79a='$box79a',
box79b='$box79b',
box79c='$box79c',
box79d='$box79d',
box91a='$box91a',
box91b='$box91b',
box91c='$box91c',
box92a='$box92a',
box92b='$box92b',
arrival_na1='$arrival_na1',
arrival_ppna1='$arrival_ppna1',
dining_na1='$dining_na1',
dining_ppna1='$dining_ppna1',
ordering_na1='$ordering_na1',
ordering_ppna1='$ordering_ppna1',
product_na1='$product_na1',
product_ppna1='$product_ppna1',
payment_na1='$payment_na1',
payment_ppna1='$payment_ppna1',
total1='$total1',
total2='$total2',
total3='$total3',
percentage='$percentage',
grade='$grade' WHERE entry='$entry'";
mysql_query($insert_query) or die('Error:' . mysql_error());
mysql_close($con);
}
?>

Check whether you insert query give the correct query or not
echo $insert_qury="update query ";
and copy the output of insert query and test manually to past on sql in table it it update correctly then you query is correct and your error may be on "mysql_query"

You have put everything inside single quote...
Try this:
$insert_query = "UPDATE a_team SET
date='".$date."',
q11a='".$q11a."',
q12a='".$q12a."',..... WHERE entry='".$entry."'";

Related

how to reduce number of variables PHP

My code checks and updates the number of apartments a building has with different bedrooms (1,2,3,4,studio) and calculates their avg min rent price and avg max rent price for particular type of room. How do I reduce the number of variables in this code so its easier to read and work with, if I can do it with arrays and foreach loop how can I use them in this code?
$room_one = $room_two = $room_three = $room_four = $room_studio = 0;
$rentmin_one = $rentmin_two = $rentmin_three = $rentmin_four = $rentmin_studio = 0;
$rentmax_one = $rentmax_two = $rentmax_three = $rentmax_four = $rentmax_studio = 0;
foreach ($nodes as $node) {
$field_bedrooms = "input field value";
$field_rentmin = "input field value";
$field_rentmax = "input field value";
foreach ($field_bedrooms as $field_bedroom) {
if (!is_null($field_bedroom) && !empty($field_bedroom)) {
$bedrooms_nid = $field_bedroom['target_id'];
$bedrooms_vocab = $this->load($bedrooms_nid);
$bedroom_value = $bedrooms_vocab->getName();
if ($bedroom_value == 1) {
$room_one++;
$rentmin_one = $rentmin_one + $field_rentmin;
$rentmax_one = $rentmax_one + $field_rentmax;
}
elseif ($bedroom_value == 2) {
$room_two++;
$rentmin_two = $rentmin_two + $field_rentmin;
$rentmax_two = $rentmax_two + $field_rentmax;
}
elseif ($bedroom_value == 3) {
$room_three++;
$rentmin_three = $rentmin_three + $field_rentmin;
$rentmax_three = $rentmax_three + $field_rentmax;
}
elseif ($bedroom_value == 4 || $bedroom_value == '4+') {
$room_four++;
$rentmin_four = $rentmin_four + $field_rentmin;
$rentmax_four = $rentmax_four + $field_rentmax;
}
elseif ($bedroom_value == 'Studio') {
$room_studio++;
$rentmin_studio = $rentmin_studio + $field_rentmin;
$rentmax_studio = $rentmax_studio + $field_rentmax;
}
}
}
}
$avg_minrent['one'] = !empty($rentmin_one) && !empty($room_one) ? $rentmin_one / $room_one : '';
$avg_maxrent['one'] = !empty($rentmax_one) && !empty($room_one) ? $rentmax_one / $room_one : '';
$avg_minrent['two'] = !empty($rentmin_two) && !empty($room_two) ? $rentmin_two / $room_two : '';
$avg_minrent['three'] = !empty($rentmin_three) && !empty($room_three) ? $rentmin_three / $room_three : '';
$avg_minrent['four'] = !empty($rentmin_four) && !empty($room_four) ? $rentmin_four / $room_four : '';
$avg_minrent['studio'] = !empty($rentmin_studio) && !empty($room_studio) ? $rentmin_studio / $room_studio : '';
$avg_maxrent['two'] = !empty($rentmax_two) && !empty($room_two) ? $rentmax_two / $room_two : '';
$avg_maxrent['three'] = !empty($rentmax_three) && !empty($room_three) ? $rentmax_three / $room_three : '';
$avg_maxrent['four'] = !empty($rentmax_four) && !empty($room_four) ? $rentmax_four / $room_four : '';
$avg_maxrent['studio'] = !empty($rentmax_studio) && !empty($room_studio) ? $rentmax_studio / $room_studio : '';
return [f
$avg_minrent,
$avg_maxrent,
];

85/5000 Asynchronous equation in PHP: a variable depends on another that has not yet been declared

I have a sum in my code that needs the return of a function, but the return of this function depends that the sum is already ready, because it uses a parameter that is still going to be created, can you solve it?
It's a calculator made in excel and I'm programming, but excel is that way and it works, but when I went to the code I got caught up in that problem.
//See the code:
public function valorRenda($rendaMensal, $valorEntrada, $tempoMeses, $imovelTipo){
$resultado = array();
$resultado['tipoSimulacao'] = 'rendaCliente';
$taxaMensal = $this->taxaPorTipo('rendaCliente', $imovelTipo, $rendaMensal) / 100;
//Cálculo do valor da primeira parcela
$primeiraParcela = $rendaMensal * 0.3;
//Calcula valor máximo do financiamento
$multiplicador = 0.5;
$i = 1;
for ($i = 1; $i <= 9999999999; $i++) {
$amortizacao = ($i * $multiplicador) / $tempoMeses;
$juros = (($i * $multiplicador) * ($taxaMensal));
$valor = $amortizacao + $juros + $this->custoMensal($valorImovel,$valorEntrada);
if ($valor >= $primeiraParcela) {
$projecao = ($i * $multiplicador) + $valorEntrada;
$valorImovel = ($projecao * $primeiraParcela / $valor);
break;
}
}
$financiamentoMaximo = round($valorImovel);
$financiamentoMaximo = $this->formatoReal($financiamentoMaximo);
$resultado['financiamentoMaximo'] = $financiamentoMaximo;
$resultado['financiamentoMaximoFormatado'] = $this->formatoReal($financiamentoMaximo, false);
//Calcula a primeira parcela
$primeiraParcelaSemTaxas = $primeiraParcela - $this->custoMensal($valorImovel,$valorEntrada);
$primeiraParcelaSemTaxas = round($primeiraParcelaSemTaxas, 2);
$primeiraParcelaSemTaxas = $this->formatoReal($primeiraParcelaSemTaxas);
$resultado['primeiraParcela'] = $primeiraParcelaSemTaxas;
$resultado['primeiraParcelaFormatado'] = $this->formatoReal($primeiraParcelaSemTaxas, false);
$primeiraParcelaTotal = $primeiraParcela;
$primeiraParcelaTotal = round($primeiraParcelaTotal, 2);
$primeiraParcelaTotal = $this->formatoReal($primeiraParcelaTotal);
$resultado['primeiraParcelaTotal'] = $primeiraParcelaTotal;
$resultado['primeiraParcelaTotalFormatado'] = $this->formatoReal($primeiraParcelaTotal, false);
$valorUltimaParcela = ((($valorImovel - $valorEntrada) - ((($valorImovel - $valorEntrada) / $tempoMeses) * ($tempoMeses - 1))) + ((($valorImovel - $valorEntrada) - ((($valorImovel - $valorEntrada) / $tempoMeses) * ($tempoMeses - 1)))) * $taxaMensal) + $this->custoMensal($valorImovel,$valorEntrada);
$ultimaParcelaSemTaxas = $valorUltimaParcela - $this->custoMensal($valorImovel,$valorEntrada);
$ultimaParcelaSemTaxas = round($ultimaParcelaSemTaxas, 2);
$ultimaParcelaSemTaxas = $this->formatoReal($ultimaParcelaSemTaxas);
$resultado['ultimaParcela'] = $ultimaParcelaSemTaxas;
$resultado['ultimaParcelaFormatado'] = $this->formatoReal($ultimaParcelaSemTaxas, false);
$primeiraParcelaTotal = $valorUltimaParcela;
$primeiraParcelaTotal = round($primeiraParcelaTotal, 2);
$primeiraParcelaTotal = $this->formatoReal($primeiraParcelaTotal);
$resultado['ultimaParcelaTotal'] = $primeiraParcelaTotal;
$resultado['ultimaParcelaTotalFormatado'] = $this->formatoReal($primeiraParcelaTotal, false);
$resultado['taxa'] = number_format(round(($taxaMensal * 100), 2), 2);
$resultado['periodo'] = $tempoMeses;
$this->incluiCustoMensal($resultado);
return $resultado;
}
//The function:
private function custoMensal($valorImovel, $entradaImovel)
{`enter code here`
$taxa_dfi = ($this->dfi / 100) * $valorImovel;
$taxa_mip = ($this->mip / 100) * ($valorImovel- $entradaImovel);
return $taxa_dfi + $taxa_mip + $this->tac;
}
Where is the problem:
$valor = $amortizacao + $juros + $this->custoMensal($valorImovel,$valorEntrada);
In this line I need to pass $valorImovelto the function custoMensal(), what happens is that the variable $valorImovel has not yet been created because it depends on the variable $valor.

Improve PHP execution speed

Please how do i improve the speed of php code. I have over 1580 lines of code that needs to be process about 103,000 data set. Presently this code takes more than 5 minutes to process 1 data set. It contains 30 for loops and 168 conditional statement.
$tnu =0; $tcp=0; $tnup = 0;
if($SsNoCourses[$sessionCounter] != 0){
//echo $SsNoCourses[$sessionCounter]."</br>";
$secondSemesterExist = "True";
$actualSemesterCounter = $actualSemesterCounter + 1;
$lastComputedSemesterId = $actualSemesterCounter;
for($i = 0; $i<$noOfCoursesS; $i++){
if ($sessionCounter == 1){
$tnu = $tnu + $SsY1Courses[$i][2];
$tcp = $tcp + $SsY1Courses[$i][2]*$SsY1Courses[$i][4];
if ($SsY1Courses[$i][4] != 0){$tnup = $tnup + $SsY1Courses[$i][2];}
}else if ($sessionCounter == 2){
$tnu = $tnu + $SsY2Courses[$i][2];
$tcp = $tcp + $SsY2Courses[$i][2]*$SsY2Courses[$i][4];
if ($SsY2Courses[$i][4] != 0){$tnup = $tnup + $SsY2Courses[$i][2];}
}else if ($sessionCounter == 3){
$tnu = $tnu + $SsY3Courses[$i][2];
$tcp = $tcp + $SsY3Courses[$i][2]*$SsY3Courses[$i][4];
if ($SsY3Courses[$i][4] != 0){$tnup = $tnup + $SsY3Courses[$i][2];}
}else if ($sessionCounter == 4){
$tnu = $tnu + $SsY4Courses[$i][2];
$tcp = $tcp + $SsY4Courses[$i][2]*$SsY4Courses[$i][4];
if ($SsY4Courses[$i][4] != 0){$tnup = $tnup + $SsY4Courses[$i][2];}
}else if ($sessionCounter == 5){
$tnu = $tnu + $SsY5Courses[$i][2];
$tcp = $tcp + $SsY5Courses[$i][2]*$SsY5Courses[$i][4];
if ($SsY5Courses[$i][4] != 0){$tnup = $tnup + $SsY5Courses[$i][2];}
}else if ($sessionCounter == 6){
$tnu = $tnu + $SsY6Courses[$i][2];
$tcp = $tcp + $SsY6Courses[$i][2]*$SsY6Courses[$i][4];
if ($SsY6Courses[$i][4] != 0){$tnup = $tnup + $SsY6Courses[$i][2];}
}else if ($sessionCounter == 7){
$tnu = $tnu + $SsY7Courses[$i][2];
$tcp = $tcp + $SsY7Courses[$i][2]*$SsY7Courses[$i][4];
if ($SsY7Courses[$i][4] != 0){$tnup = $tnup + $SsY7Courses[$i][2];}
}else if ($sessionCounter == 8){
$tnu = $tnu + $SsY8Courses[$i][2];
$tcp = $tcp + $SsY8Courses[$i][2]*$SsY8Courses[$i][4];
if ($SsY8Courses[$i][4] != 0){$tnup = $tnup + $SsY8Courses[$i][2];}

Show lowest value in a select

I am trying to get 1 result of the lowest value from a set. So I have a set of number (averages) i just need to show the lowest one of the set:
if ($danresult->num_rows > 0) {
while($row = $danresult->fetch_assoc()) {
$score1 = $row["score_1"];
$score2 = $row["score_2"];
$score3 = $row["score_3"];
$score4 = $row["score_4"];
$score5 = $row["score_5"];
$score6 = $row["score_6"];
$score7 = $row["score_7"];
$score8 = $row["score_8"];
$sum = $score1 + $score2 + $score3 + $score4 + $score5 + $score6 + $score7 + $score8;
$totalAverage = $sum / 8;
$totalAverages[] = $totalAverage;
$index = min($totalAverages);
echo $index . '<br>';
}
}
I have been trying to use the php min() function to no avail!
My results would typically be like 2, 2.75, 3, 3.35 etc.. so I just need to show the lowest of the set. so like 2 from the set above.
you could add all values to an array and use the array functions to determine the winner
if ($danresult->num_rows > 0) {
while($row = $danresult->fetch_assoc()) {
$scores=array(
$row["score_1"],
$row["score_2"],
$row["score_3"],
$row["score_4"],
$row["score_5"],
$row["score_6"],
$row["score_7"],
$row["score_8"]
);
$sum= array_sum ($scores);
$totalAverage = $sum / count($scores);
$min=min($scores);
$winner=array_search($min, $scores);
}
}
Try this, you need to close foreach...then get a lowest value from array
if ($danresult->num_rows > 0) {
while($row = $danresult->fetch_assoc()) {
$score1 = $row["score_1"];
$score2 = $row["score_2"];
$score3 = $row["score_3"];
$score4 = $row["score_4"];
$score5 = $row["score_5"];
$score6 = $row["score_6"];
$score7 = $row["score_7"];
$score8 = $row["score_8"];
$sum = $score1 + $score2 + $score3 + $score4 + $score5 + $score6 + $score7 + $score8;
$totalAverage = $sum / 8;
$totalAverages[] = $totalAverage;
}
$index = min($totalAverages);
echo $index . '<br>';
}

How to sum mysql values in multple while PHP

i like to sum all row values from the $orderfeed_query. But when i echo the $sum, i get just the sum from the last loop.
how can can i add the sum of all following loop on the $sum variable? I dont know how many loops a order have.
$checkorder = mysql_query("SELECT * FROM orders WHERE `email` = '$email' ") or die(mysql_error());
while ($row = mysql_fetch_assoc($checkorder)) {
$orderid = $row["orderid"];
$check_order = $row["check_order"];
if($check_order[0] == 1){
$orderfeed_query = mysql_query("SELECT * FROM orderfeed WHERE `orderid` = '$orderid' AND `product` = '1'") or die(mysql_error());
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = $row["signaturewiz"];
$flurstueckwiz = $row["flurstueckwiz"];
$uploadwiz = $row["uploadwiz"];
$exsignaturewiz = $row["exsignaturewiz"];
$ibanwiz = $row["ibanwiz"];
$sum = $signaturewiz+$flurstueckwiz+$uploadwiz+$exsignaturewiz+$ibanwiz;
echo $sum;
}
}
}
}
This code
$sum = $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;
overwrites sum each time. You must add to sum not to overwrite
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;
And declare $sum = 0; before main loop
Several people have pointed out that $sum should be outside the loop, and that is indeed correct. However, MySQL can do this all for you:
$orderfeed_query = mysql_query("SELECT SUM(signaturewiz + flurstueckwiz + uploadwiz + exsignaturewiz + ibanwiz) FROM orderfeed WHERE orderid = '$orderid' AND product = '1'") or die(mysql_error());
if ($row = mysql_fetch_row($orderfeed_query)) {
echo $row[0];
}
Move the echo of $sum outside the loop
Use += rather than + to accumulate a total over multiple iterations
Initialize your variable before using += as if $sum has an undefined value it can mess up the count when using +=
$sum = 0; // init variable
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = $row["signaturewiz"];
$flurstueckwiz = $row["flurstueckwiz"];
$uploadwiz = $row["uploadwiz"];
$exsignaturewiz = $row["exsignaturewiz"];
$ibanwiz = $row["ibanwiz"];
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz +
$exsignaturewiz + $ibanwiz;
}
echo $sum;
ADDITIONAL INFO:
You see 12 and not 3 so the data from your table I assume is text and not numeric so do this to convert text numbers to integers
$sum = 0; // init variable
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = (int)$row["signaturewiz"];
$flurstueckwiz = (int)$row["flurstueckwiz"];
$uploadwiz = (int)$row["uploadwiz"];
$exsignaturewiz = (int)$row["exsignaturewiz"];
$ibanwiz = (int)$row["ibanwiz"];
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz +
$exsignaturewiz + $ibanwiz;
}
echo $sum;

Categories