$result =0;
if ($id_marca!=1) {
$result = $result + $vl_max*$pares; // id_marca = 22 [ 38 pares * 1.451 = 55.14 ]
}
elseif ($id_marca=='1'){
$result = $result + $vl_min*$pares; ///id_marca = 1 [ 36 pares * 1.392 = 50.15 ]
}
else {
$result ='';
}
$result;
Hello, I need Help.
with above script, I´m receiving the $result two values (55.14 & 50.15).
So, I want to the sum the $result = (105.29).
Thank you!!
hericles 289 Master Poster Featured Poster
If you want both values totalled then you don't want the IF/ELSE structure because that is the reason only one calculation is running (whether $id_marca equals 1 or not).
Depending on your exact requirements you would be better off to store each calculation into two different result variables and then add them.
$resultMax = $vl_max*$pares; // id_marca = 22 [ 38 pares * 1.451 = 55.14 ]
$resultMin = $vl_min*$pares; ///id_marca = 1 [ 36 pares * 1.392 = 50.15 ]
$result = $resultMax + $resultMin;
jorgeftz 0 Newbie Poster
Hi Hericles, thanks for the help.
Now, bring me 55.13 & 105.28
See the full code:
$tab_frete ='1001';
$query ="SELECT MIN(vl_capital) AS mincapital, MAX(vl_capital) AS maxcapital, MIN(vl_interior) AS mininterior, MAX(vl_interior) AS maxinterior FROM tab_frete_nacional WHERE cd_tabela='$tab_frete' AND cd_destino='$ufd'";
if( $result = sasql_query($conect, $query) ) {
}
while( $row = sasql_fetch_assoc($result) ) {
$vl_max = $row['maxinterior'];
$vl_min = $row['mininterior'];
$vl_maxc = $row['maxcapital'];
$vl_minc = $row['mincapital'];
if ($siglad=='SAO'){
$vl_min =$vl_minc;
$vl_max =$vl_maxc;
}
elseif ($siglad=='CWB'){
$vl_min =$vl_minc;
$vl_max =$vl_maxc;
}
else {
$vl_min =$vl_minc;
$vl_max =$vl_maxc;
}
//busca notas atraves da minuta
$sql = ("SELECT IDMARCA,SUM(VALORMERCADORIA) AS VLM,SUM(PESO) AS PESO, SUM(QTDEPARES) AS PARES FROM TMWS.ENTRANF WHERE TMWS.ENTRANF.MINUTA='$minuta' GROUP BY IDMARCA");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rs);
for ($iterate = 0; $iterate < $row; $iterate++){
$id_marca = $rs['IDMARCA'][$iterate];
$pares = $rs['PARES'][$iterate];
$result =0;
if ($id_marca!=1) {
$resultMax = $vl_max*$pares;
}
elseif ($id_marca=='1'){
$resultMin = $vl_min*$pares;
}
else {
$result ='';
}
echo $result = $resultMax + $resultMin;
hericles 289 Master Poster Featured Poster
You still have the if/else clause in there.
If $id_marca != 1 the resultMax is calculated otherwise it is 0.
If $id_marca = 1 then resultMin is calculated otherwise it is 0;
Take out that entire if/else block and just have the two calcualtion lines. Then $resultMax and $resultMin will both hold values and $result will be the sum of them both and not just one of them + 0;
jorgeftz 0 Newbie Poster
Hi, can´t take off if/else, because have many id_marca and the result depent on the id_marca to get the right rate. I need first, separed the id_marca and pares to make the calculation.
hericles 289 Master Poster Featured Poster
Sorry, I may not be understanding exactly what you need then. In your initial post you said you needed to get 105.29, the sum of 55.14 & 50.15 (the max and min calculation I'm assuming).
But if you're getting just one value OR the other then you never have two values to total... So what am I missing?
jorgeftz 0 Newbie Poster
Hi, sorry. I known it is dificult to understand.
But in this result from two keys, I want the total of each result.
MARCA21PARES18RESULT30.51MARCA2PARES54RESULT86.94 = 117,45
MARCA22PARES38RESULT55.138MARCA1PARES36RESULT50.148 = 105,26
http://global.stech.inf.br/rate.php
hericles 289 Master Poster Featured Poster
OK, I see now. In that case define $resultMax and $resultMin before the FOR loop, so they aren't reset/overwritten on subsequent loops, and use the if/else to tally up totals
$resultMax = $resultMax + ($vl_max x $pares);
That will give you a running total of $resultMax and $resultMin that can then be tallied at the end. I only just saw the FOR loop now.
jorgeftz 0 Newbie Poster
I leave just one key... I don´t know where is my mistake.
$tab_frete ='1001';
$query ="SELECT MIN(vl_capital) AS mincapital, MAX(vl_capital) AS maxcapital, MIN(vl_interior) AS mininterior, MAX(vl_interior) AS maxinterior FROM tab_frete_nacional WHERE cd_tabela='$tab_frete' AND cd_destino='$ufd'";
if( $result = sasql_query($conect, $query) ) {
}
while( $row = sasql_fetch_assoc($result) ) {
$vl_max = $row['maxinterior'];
$vl_min = $row['mininterior'];
//busca notas atraves da minuta
$sql = ("SELECT IDMARCA,SUM(VALORMERCADORIA) AS VLM,SUM(PESO) AS PESO, SUM(QTDEPARES) AS PARES FROM TMWS.ENTRANF WHERE TMWS.ENTRANF.MINUTA='$minuta' GROUP BY IDMARCA");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rs);
for ($iterate = 0; $iterate < $row; $iterate++){
echo "MARCA" . $id_marca = $rs['IDMARCA'][$iterate];
echo "PARES". $pares = $rs['PARES'][$iterate];
$result =0;
if ($id_marca!=1) {
$resultMax = $vl_max*$pares;
}
elseif ($id_marca=='1'){
$resultMin = $vl_min*$pares;
}
$resultMax = $resultMax + ($vl_max * $pares);
$resultMin = $resultMin + ($vl_max * $pares);
echo "TOTAL" . $result = $resultMax + $resultMin;
jorgeftz 0 Newbie Poster
Hi Hiricles, I put in way and now give two results (55.138 and 105.286). Now I need the cut off the 55.138.
if ($id_marca!=1) {
$resultMax = $vl_max*$pares;
}
elseif ($id_marca=='1'){
$resultMin = $vl_min*$pares;
}
$result = $resultMax + $resultMin;
hericles 289 Master Poster Featured Poster
In your code smaple you have the start of the FOR loop but I can't see where it ends. Is you final echo ouside of the FOR loop? If it is then you should only see one echo which should, hopefully, be the total.
jorgeftz 0 Newbie Poster
with below whith two keys (MINUTA), I have the follows results:
http://global.stech.inf.br/rate.php
MINUTA=0464459 ID_MARCA=21 PARES=18 RESULTS=30.51 ID_MARCA=2 PARES=54 RESULTS=86.94 Sum of Results = 117.45
MINUTA=0464615 ID_MARCA=22 PARES=38 RESULTS=55.138 ID_MARCA=1 PARES=36 RESULTS=50.148 Sum of Results = 222.736
The first key, the sum is ok, but the second its sum the keys + the fist key.
Please some can help me? I feel, I almost there.
$results =0;
if (($id_marca!=1) && ($id_marca!=2) && ($id_marca!=23) && ($id_marca!=24) && ($id_marca!=27)&& ($id_marca!=31) && ($id_marca!=52)) {
$results = $vl_max*$pares;
}
elseif ($id_marca=='1'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='2'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='23'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='24'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='27'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='31'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='52'){
$results = $vl_min*$pares;
}
$results;
$sumresults+=$results;
}
$sumresults;
diafol
That's way off.
$sumresults = 0;
//start loop
if(!in_array($id_marca, array(1,2,23,24,27,31,52)))
{
$sumresults += $vl_min * $pares;
}
//end loop
Not that I have a clue about what you're trying to do though :(
jorgeftz 0 Newbie Poster
appreciate your help
lets try to explain:
rate table there are:
min rate & max rate & id_marca. when I select TMWS.ENTRANF (keys MINUTA), the result brings arrays with pairs & id_marca. In my actual script (you can see), the result it´s ok by id_marca, but when I try to sum it with many records, the first result ok, but the sequence it sum with before key again.
MINUTA=0464459 ID_MARCA=21 PARES=18 RESULTS=30.51 ID_MARCA=2 PARES=54 RESULTS=86.94 Sum of Results = 117.45
MINUTA=0464615 ID_MARCA=22 PARES=38 RESULTS=55.138 ID_MARCA=1 PARES=36 RESULTS=50.148 Sum of Results = 222.736
you prior code not work at all
diafol
It would be much easier if you showed an sql dump of your relevant tables and some sample data in each.
jorgeftz 0 Newbie Poster
<?php
$table ='TMWS.ENTRADA';
$sql = ("SELECT * FROM $table WHERE (TMWS.ENTRADA.MINUTA='0464615' OR TMWS.ENTRADA.MINUTA='0464459')");
$stmt = oci_parse($conn, $sql);
oci_execute ($stmt);
//DISPLAY RESULTS.
while ($row = oci_fetch_assoc($stmt)) {
$minuta = $row['MINUTA'];
$siglad = $row['SIGLAD'];
looking for IDUF
$sql = ("SELECT IDUF FROM TMWS.CIDADES WHERE TMWS.CIDADES.CODIGO='$siglad'");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rss);
for ($iterate = 0; $iterate < $row; $iterate++){
$iduf = $rss['IDUF'][$iterate];
}
$sql = ("SELECT UFDESTINO,SUM(ID) AS ID FROM TMWS.ICMS WHERE TMWS.ICMS.UFDESTINO='$iduf' GROUP BY UFDESTINO");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rs);
for ($iterate = 0; $iterate < $row; $iterate++){
$ufdestino = $rs['UFDESTINO'][$iterate];
$ufd ="";
if($ufdestino=='1'){
$ufd='12';
}
if($ufdestino=='2'){
$ufd='27';
}
if($ufdestino=='4'){
$ufd='13';
}
if($ufdestino=='3'){
$ufd='16';
}
if($ufdestino=='5'){
$ufd='29';
}
if($ufdestino=='6'){
$ufd='23';
}
if($ufdestino=='7'){
$ufd='53';
}
if($ufdestino=='8'){
$ufd='32';
}
if($ufdestino=='10'){
$ufd='52';
}
if($ufdestino=='11'){
$ufd='21';
}
if($ufdestino=='12'){
$ufd='51';
}
if($ufdestino=='13'){
$ufd='50';
}
if($ufdestino=='14'){
$ufd='31';
}
if($ufdestino=='15'){
$ufd='15';
}
if($ufdestino=='16'){
$ufd='25';
}
if($ufdestino=='17'){
$ufd='21';
}
if($ufdestino=='18'){
$ufd='26';
}
if($ufdestino=='19'){
$ufd='22';
}
if($ufdestino=='21'){
$ufd='24';
}
if($ufdestino=='22'){
$ufd='43';
}
if($ufdestino=='20'){
$ufd='33';
}
if($ufdestino=='23'){
$ufd='11';
}
if($ufdestino=='9'){
$ufd='14';
}
if($ufdestino=='25'){
$ufd='42';
}
if($ufdestino=='26'){
$ufd='35';
}
if($ufdestino=='27'){
$ufd='28';
}
if($ufdestino=='24'){
$ufd='17';
}
}
$tab_frete ='1001';
$query ="SELECT MIN(vl_capital) AS mincapital, MAX(vl_capital) AS maxcapital, MIN(vl_interior) AS mininterior, MAX(vl_interior) AS maxinterior FROM tab_frete_nacional WHERE cd_tabela='$tab_frete' AND cd_destino='$ufd'";
if( $result = sasql_query($conect, $query) ) {
}
while( $row = sasql_fetch_assoc($result) ) {
$vl_max = $row['maxinterior'];
$vl_min = $row['mininterior'];
}
$sql = ("SELECT IDMARCA,SUM(VALORMERCADORIA) AS VLM,SUM(PESO) AS PESO, SUM(QTDEPARES) AS PARES FROM TMWS.ENTRANF WHERE TMWS.ENTRANF.MINUTA='$minuta' GROUP BY IDMARCA");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rs);
for ($iterate = 0; $iterate < $row; $iterate++){
$id_marca = $rs['IDMARCA'][$iterate];
$pares = $rs['PARES'][$iterate];
$results =0;
if (($id_marca!=1) && ($id_marca!=2) && ($id_marca!=23) && ($id_marca!=24) && ($id_marca!=27)&& ($id_marca!=31) && ($id_marca!=52)) {
$results = $vl_max*$pares;
}
elseif ($id_marca=='1'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='2'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='23'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='24'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='27'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='31'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='52'){
$results = $vl_min*$pares;
}
$results;
$sumresults+=$results;
}
$sumresults;
}
?>
jorgeftz 0 Newbie Poster
please, disregard my last message. I posted the wrong script.
jorgeftz 0 Newbie Poster
<?PHP
$table ='TMWS.ENTRADA';
$sql = ("SELECT * FROM $table WHERE (TMWS.ENTRADA.MINUTA='0464615' OR TMWS.ENTRADA.MINUTA='0464188')");
// $sql = ("SELECT * FROM $table WHERE TRUNC(TMWS.ENTRADA.DATAEMISSAO)='$date' AND TMWS.ENTRADA.CODIGOR='16590234001652' AND TMWS.ENTRADA.FRETECOMBINADO='N' AND TMWS.ENTRADA.CIA='TR' AND ROWNUM <= 100 order by MINUTA desc");
$stmt = oci_parse($conn, $sql);
oci_execute ($stmt);
//DISPLAY RESULTS.
while ($row = oci_fetch_assoc($stmt)) {
$minuta = $row['MINUTA'];
$ctrc = $row ['CTRC'];
$dt_emissao = $row['DATAEMISSAO'];
$cod_rem = $row['CODIGOR'];
$frete_tmws= $row['FRETETOTAL'];
$siglad = $row['SIGLAD'];
// busca IDUF
$sql = ("SELECT IDUF FROM TMWS.CIDADES WHERE TMWS.CIDADES.CODIGO='$siglad'");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rss);
for ($iterate = 0; $iterate < $row; $iterate++){
$iduf = $rss['IDUF'][$iterate];
}
// busca UF na tabela ICMS TMWS e convert para tabela do ecx16
$sql = ("SELECT UFDESTINO,SUM(ID) AS ID FROM TMWS.ICMS WHERE TMWS.ICMS.UFDESTINO='$iduf' GROUP BY UFDESTINO");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rs);
for ($iterate = 0; $iterate < $row; $iterate++){
$ufdestino = $rs['UFDESTINO'][$iterate];
$ufd ="";
if($ufdestino=='1'){
$ufd='12';
}
if($ufdestino=='2'){
$ufd='27';
}
if($ufdestino=='4'){
$ufd='13';
}
if($ufdestino=='3'){
$ufd='16';
}
if($ufdestino=='5'){
$ufd='29';
}
if($ufdestino=='6'){
$ufd='23';
}
if($ufdestino=='7'){
$ufd='53';
}
if($ufdestino=='8'){
$ufd='32';
}
if($ufdestino=='10'){
$ufd='52';
}
if($ufdestino=='11'){
$ufd='21';
}
if($ufdestino=='12'){
$ufd='51';
}
if($ufdestino=='13'){
$ufd='50';
}
if($ufdestino=='14'){
$ufd='31';
}
if($ufdestino=='15'){
$ufd='15';
}
if($ufdestino=='16'){
$ufd='25';
}
if($ufdestino=='17'){
$ufd='21';
}
if($ufdestino=='18'){
$ufd='26';
}
if($ufdestino=='19'){
$ufd='22';
}
if($ufdestino=='21'){
$ufd='24';
}
if($ufdestino=='22'){
$ufd='43';
}
if($ufdestino=='20'){
$ufd='33';
}
if($ufdestino=='23'){
$ufd='11';
}
if($ufdestino=='9'){
$ufd='14';
}
if($ufdestino=='25'){
$ufd='42';
}
if($ufdestino=='26'){
$ufd='35';
}
if($ufdestino=='27'){
$ufd='28';
}
if($ufdestino=='24'){
$ufd='17';
}
}
$tab_frete ='1001';
$query ="SELECT MIN(vl_capital) AS mincapital, MAX(vl_capital) AS maxcapital, MIN(vl_interior) AS mininterior, MAX(vl_interior) AS maxinterior FROM tab_frete_nacional WHERE cd_tabela='$tab_frete' AND cd_destino='$ufd'";
if( $result = sasql_query($conect, $query) ) {
}
while( $row = sasql_fetch_assoc($result) ) {
echo " " ."KEY=" . $key = $minuta;
echo " " . "VALOR_MAX" . $vl_max = $row['maxinterior'];
echo " " . "VALOR_MIN" . $vl_min = $row['mininterior'];
$vl_maxc = $row['maxcapital'];
$vl_minc = $row['mincapital'];
if ($siglad=='SAO'){
$vl_min =$vl_minc;
$vl_max =$vl_maxc;
}
elseif ($siglad=='CWB'){
$vl_min =$vl_minc;
$vl_max =$vl_maxc;
}
else {
$vl_min =$vl_minc;
$vl_max =$vl_maxc;
}
}
$sql = ("SELECT IDMARCA,SUM(VALORMERCADORIA) AS VLM,SUM(PESO) AS PESO, SUM(QTDEPARES) AS PARES FROM TMWS.ENTRANF WHERE TMWS.ENTRANF.MINUTA='$minuta' GROUP BY IDMARCA");
$result = ociparse($conn, $sql);
ociexecute($result);
$row = ocifetchstatement($result, $rs);
for ($iterate = 0; $iterate < $row; $iterate++){
echo " " ."ID_MARCA=" . $id_marca = $rs['IDMARCA'][$iterate];
echo " " ."PARES=". $pares = $rs['PARES'][$iterate];
$results =0;
if (($id_marca!=1) && ($id_marca!=2) && ($id_marca!=23) && ($id_marca!=24) && ($id_marca!=27)&& ($id_marca!=31) && ($id_marca!=52)) {
$results = $vl_max*$pares;
}
elseif ($id_marca=='1'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='2'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='23'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='24'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='27'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='31'){
$results = $vl_min*$pares;
}
elseif ($id_marca=='52'){
$results = $vl_min*$pares;
}
echo " " . "RESULTS=".$results;
$sumresults+=$results;
}
echo " " ."Sum of Results = ".$sumresults;
}
?>
jorgeftz 0 Newbie Poster
I need the sum of results by KEY. the first key the sum is ok, but the second sum it sum the KEY itself + the key before.
http://global.stech.inf.br/rate.php
diafol
It would be much easier if you showed an sql dump of your relevant tables and some sample data in each.
I meant the table structures and a couple of records for each...
e.g.
TABLE1
id | field1 | field2 | field3
1 | value1 | value2 | value 3
2 | value4 | value5 | value 6
3 | value7 | value8 | value 9
TABLE2
id | field1 | field2 | some_id
1 | value1 | value2 | 1
2 | value4 | value5 | 1
3 | value7 | value8 | 2
WHERE table2.some_id = table1.id
Something like that. Without this context, the code is pretty difficult to read.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.