if with array listing

0

I'm picking up using arrays, I need to check values and make decisions depending on what I find in the listing.

id | volume_total | volume_entregue | volume_restante
 1      15               10                 5
 2      10               10                 0

My problem is the following in this listing:

  • If all of the extant lines are (zero), then

    $status = 'Todos Entregues'
    
  • If all of the extant lines are equal to full_volume, then

    $status = 'Nenhum entregue' 
    
  • If on one line, the volume of the total is (zero), and on another line, the amount of the volume is equal to or less than the total_ volume, then

    $status ='Entrega Parcial'
    

I can not formulate if's in this schema.

<?php
include "mysql.php";

$id = 13;
$sql = mysql_query ("SELECT * FROM ped_vendas_item WHERE id = '$id' ");

while ($r = mysql_fetch_array($sql)){  
    $volume_entregue[] =$r['volume_entregue'];
    $volume_total[] = $r['volume_total'];
    $volume_restante[] = $r['volume_restante'];
}

if ($volume_restante > 0 and $volume_restante == $volume_total) {
    $status = "Todos Entregues";
} elseif ($volume_restante == 0){
    $status = "Nenhum";
} elseif ($volume_restante > 0 and $volume_restante < $volume_total){
    $status = "Entrega Parcial";
}

Image below, to get a sense of what the listing looks like.

Type: There can be multiple items where some of these can be delivered to the customer in whole or in part or even a list item can not be delivered. I need to take control of it.

    
asked by anonymous 19.09.2018 / 20:56

2 answers

0

Variables within your while are not declared out of scope of while , so when you do the comparisons they are initialized and have null value.

Even if you declare them out, you are passing values to it in the form of array using [] but in your if you end up comparing with int without indicating the index of array using $volume_entregue[0] .

That's right, you can check if status within while is much easier, it would look like this:

$status = "Sem informações";
while ($r = mysql_fetch_array($sql)){  
    if $r['volume_restantes'] == 0) {
        $status = "Todos Entregues";
    } else if ($r['volume_total'] == $r['volume_restante']){
        $status = "Nenhum";
    } else {
        $status = "Entrega Parcial";
    }
}

There is also the possibility of you doing this check directly from mysql , query would look like this:

SELECT pdi.id, IF(pdi.volume_restante == 0, 'Todos Entregues', IF(pdi.volume_total == pdi.volume_restante, 'Nenhum entregue', 'Entrega parcial')) as 'status' FROM ped_vendas_item pdi WHERE pdi.id = '$id'

And then within while , you would replace if with:

$status = $r['status'];
Particularly, as there are several cases, I would suggest using this query by adding the rest of the parameters of the ped_vendas_item table, so you would only have the job of listing the result of query instead of putting several if to find status .

    
19.09.2018 / 21:28
0

I am doing so. I just do not know how to rescue it.

include "mysql.php";

$ped_venda_id = 13;


$sql = mysql_query ("SELECT pvi.ped_venda_id, IF(pvi.volume_restante ==0, 'T', IF(pvi.volume_total == pvi.volume_restante, 'N', 'P')) as 'status' FROM ped_vendas_item.pvi  WHERE ped_venda_id = '$ped_venda_id' ");  



while($r = mysql_fetch_array($sql)){ 

    $status = $r['status'];

}

echo $status;
    
19.09.2018 / 22:10