Help in a PHP logic

3

I have a big problem for me *

ini_set('display_errors', true);
error_reporting(E_ALL);
header('Access-Control-Allow-Origin: *');

#
header('Content-Type: application/json');
header('Character-Encoding: utf-8');
define( 'MYSQL_HOST', 'localhost' );
define( 'MYSQL_USER', 'root' );
define( 'MYSQL_PASSWORD', '' );
define( 'MYSQL_DB_NAME', 'sistema' );
//$PDO = new PDO( 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASSWORD );
try
{
  $PDO = new PDO( 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASSWORD );
}
catch ( PDOException $e )
{
    echo 'Erro ao conectar com o MySQL: ' . $e->getMessage();
}


$sql = "SELECT tblinvoices.id, tblinvoices.clientid, tblinvoices.status, tblinvoices.duedate, tblclients.company, tblclients.website from tblinvoices INNER JOIN tblclients ON tblinvoices.clientid = tblclients.userid where tblclients.id = 1 ";


$result = $PDO->query( $sql );
$rows = $result->fetchAll(PDO::FETCH_OBJ);


 $json_str = json_encode($rows, JSON_PRETTY_PRINT);


echo $json_str;

I have a return in JSON. this one below .. I will display it to explain my situation;

[
{
    "id": "1",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-09-05",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "8",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-10-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "15",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "16",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "17",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "24",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-12-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "31",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-01-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "40",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-02-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "47",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-03-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "60",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-04-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "61",
    "clientid": "1",
    "status": "4",
    "duedate": "2018-05-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "68",
    "clientid": "1",
    "status": "4",
    "duedate": "2018-05-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
}

]

Beauty up there! What I want to do and get status and make a condition; example: if the status is equal to 4 informo look this delayed .. if the status is equal to 2, that is OK.

So I did so basic :   This CURL places it inside the client code. so to give the message there;

$cr = curl_init();
curl_setopt($cr, CURLOPT_URL, "http://localhost/clientes/"); 
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true);
//definindo uma variável para receber o conteúdo da página...
$retorno = curl_exec($cr);
//fechando-o para liberação do sistema.
curl_close($cr); //fechamos o recurso e liberamos o sistema...


$dados = json_decode($retorno, true);

if ($dados[0]['status'] == 4) {
echo "Olha Vencido";
}elseif ($dados[0] == 2) {
  echo "Que Beleza você esta em dia.";
}else{
  echo "tem alguma coisa errado.";
}

As you can see, there are several returns with the same status, so my problem starts, I want to get only those statuses (4), if I make this change from my bank, doing a where status = 4, I will not be able to validate, if it is status status (2) or not. Another issue is to get the id, plus I found another problem if the ID changes what will occur since the invoice is generated all month. I can not get into a logic.

Where I get the status of the last invoices, and I make my condition;

Sorry for the long test. and that I wanted to explain the maximum, if anyone can give me a light.

    
asked by anonymous 10.05.2018 / 22:04

2 answers

1

From the data you get through JSON, you can get only the records you want using the array_filter function. To increase the readability of the code, we can define an enumerator with the existing statuses:

abstract class Status {
    const ATRASADO = 4;
    const EM_DIA = 2;
}

So, to get backlogs, you can do:

$atrasados = array_filter($data, function ($it) {
    return $it->status == Status::ATRASADO;
});

See working at Repl.it | Ideone | GitHub GIST

In this way, you can walk through $atrasados knowing that all of these are with status equal to 4. The same can be done with status equal to 2 or any other possible value.

    
11.05.2018 / 14:16
1

Look, if I understand you correctly, you are having difficulty accessing the data, one by one, from your array created from the JSON object. I'll suggest the example below using PHP's foreach

<?php
$ret = '[
{
    "id": "1",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-09-05",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "8",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-10-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "15",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "16",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "17",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "24",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-12-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "31",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-01-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "40",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-02-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "47",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-03-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "60",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-04-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "61",
    "clientid": "1",
    "status": "4",
    "duedate": "2018-05-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "68",
    "clientid": "1",
    "status": "4",
    "duedate": "2018-05-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
}
]';

$dados = json_decode($ret, true);


foreach ($dados as $transacao){
    echo 'A transação id: '.$transacao["id"].', referente a '.$transacao["duedate"].' está ';
    if($transacao["status"] == "4")
        echo 'vencida';
    elseif($transacao["status"] == "2")
        echo 'em dia';
    else
        echo 'aprensentando problemas';
    echo '<br>';
}

will have the following return:

A transação id: 1, referente a 2017-09-05 está em dia
A transação id: 8, referente a 2017-10-06 está em dia
A transação id: 15, referente a 2017-11-06 está em dia
A transação id: 16, referente a 2017-11-03 está em dia
A transação id: 17, referente a 2017-11-03 está em dia
A transação id: 24, referente a 2017-12-06 está em dia
A transação id: 31, referente a 2018-01-06 está em dia
A transação id: 40, referente a 2018-02-06 está em dia
A transação id: 47, referente a 2018-03-06 está em dia
A transação id: 60, referente a 2018-04-06 está em dia
A transação id: 61, referente a 2018-05-06 está vencida
A transação id: 68, referente a 2018-05-03 está vencida

EDIT

or to return the id's of those with status = 4

<?php
$ret = '[
{
    "id": "1",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-09-05",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "8",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-10-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "15",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "16",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "17",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-11-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "24",
    "clientid": "1",
    "status": "2",
    "duedate": "2017-12-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "31",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-01-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "40",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-02-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "47",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-03-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "60",
    "clientid": "1",
    "status": "2",
    "duedate": "2018-04-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "61",
    "clientid": "1",
    "status": "4",
    "duedate": "2018-05-06",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
},
{
    "id": "68",
    "clientid": "1",
    "status": "4",
    "duedate": "2018-05-03",
    "company": "Cliente Exemplo",
    "website": "clienteexemplo.com"
}
]';

$dados = json_decode($ret, true);

$ids = "";
foreach ($dados as $transacao)
    if($transacao["status"] == "4")
        $ids .= $transacao["id"].', ';

if($ids)
    echo 'A(s) transação(ões) com o(s) id(s): '.substr($ids,0,-2).' estão vencidas';
else
    echo 'Não há transações vencidas';

returns:

A(s) transação(ões) com o(s) id(s): 61, 68 estão vencidas 
    
10.05.2018 / 22:35