Help in a PHP logica

Asked

Viewed 101 times

3

I’m in big trouble for me*

follows below as is being made select in the bank.

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 . that 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"
}

]

All right, up you go! What I want to do is take the status and make a condition; example : if the status is equal to 4 informo look at this late.. if the status is equal to 2 let me know that it is OK.

so I did like this basic: That CURL put there inside the customer 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.";
}

More as you can see there are several returns with equal status, so my problem begins, I want to get only the status(4) loser, if I make this change that’s coming from my bank, doing a Where status = 4 , I won’t be able to validate, whether it’s status(2) or not. Another issue is to pick by the id, plus I found another problem, if the id changes what will occur since the invoice is generated all month. I’m not getting anywhere on logic.

Where I take the status of the latest invoices, and make my condition;

Sorry about the long test. and I wanted to explain as much, if someone can give me a light.

  • So summarizing what you want: scan all the data and return the ones that have status 4 to make some logic following, correct?

  • What you want to do is not very clear in your explanation. But in a summarized way. You should only bring from the database what you need. You should not bring other values to delete them via PHP. Unless you need these values.

2 answers

1


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

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

So, to get the late records, can do:

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

See working on Repl.it | Ideone | Github GIST

This way you can go through $atrasados knowing that all these are with the status equal to 4. The same can be done with the status equal to 2 or any other possible value.

1

Look, if I understand correctly, you’re having trouble accessing the data, one by one, from your array created from the JSON object. I will suggest you the example below using the function foreach of PHP

<?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 the 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 

Browser other questions tagged

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