if with array listing

Asked

Viewed 57 times

0

I’m getting beaten using arrays, I need to check values and make decisions depending on what I find in the list.

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

My problem is the following on this list:

  • If all the remaining volume_lines are (zero), then

    $status = 'Todos Entregues'
    
  • If all the remaining volume_lines are equal to full volume_then

    $status = 'Nenhum entregue' 
    
  • If in one volume_remaining line you have (zero) and in another volume_remaining line you have a value equal to or less than volume_total, then

    $status ='Entrega Parcial'
    

I can’t formulate if s in this scheme.

<?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, is to get a sense of what the listing is like.

Type: There may be multiple items, where some of these may be delivered to the customer wholly or partially or even an item on the list may not be delivered. I need to control this.

inserir a descrição da imagem aqui

  • That one id in query is the order id or order item id? You want to know if the whole order has been delivered or separately from each item?

  • Enjoy and tell us what’s going wrong. Your code isn’t getting the status right?

2 answers

0

The variables within your while are not stated out of scope of while, soon, when you make the comparisons they are initialized and have value null.

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

This put, you can make the check if status within the while that is much easier, would be 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 for you to do this direct check of mysql, to query would be so:

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 inside the while, you would replace the if for:

$status = $r['status'];

Particularly, as there are several cases, I would suggest using the query adding the rest of the table parameters ped_vendas_item, so you would only have the trouble of listing the result of query instead of putting several if to discover the status.

0

I’m doing it like this. I just don’t know how to salvage 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;
  • Use the edit link of the question, this is an answer.

  • Can someone help me with this code above?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.