How to merge several variables into one php and pass to Jquery in a link

Asked

Viewed 134 times

0

I consulted on the subject and even managed to find some ways but nothing that could solve my case, I will try to be direct to facilitate. I have a table where I list some visits of some companies, I have the visits of types RTV and ATV and I am adding the visits and putting in this table in the respective fields on the dates, each sum represents the visits made, in the example below the day 18/09 had 3 visits ATV.

inserir a descrição da imagem aqui

Every visit this one has one ID, that is to say, 3 IDs and what I’m trying to do is unite these ID´s in the same variable, I made the necessary selects and created this structure to store them, but not dynamically, I am making this select within a loop and he’s like, in the case of the ATV visit:

     $sqlMontaLinkATV = "SELECT supVisitaRtv.IdVisita FROM supVisitaRtv WHERE Tipo = 'ATV' AND supVisitaRtv.Data = ? AND IdEmpresa = ? ";   
     $arrayParamLinkATV = array($DataInc, $IdEmpresa);  
     $ResultadoLinkATV  = $crud->getSQLGeneric($sqlMontaLinkATV, $arrayParamLinkATV, TRUE); 

The result of this research I’m seeing like this with a print_r($ResultadoLinkATV):

Array ( [0] => stdClass Object ( [IdVisita] => 33 ) [1] => stdClass Object ( [IdVisita] => 34 ) [2] => stdClass Object ( [IdVisita] => 37 ) )

Where I have the visitor ID’s I need, I tried to insert them into a variable like this:

 $IdVisita = array();
 array_push($IdVisita, $ResultadoLinkATV[0]->IdVisita); 

I hope I have been able to explain satisfactorily.

  • In your bank '3' is a real value or you arrive at '3' adding up records of visits at time of consultation?

2 answers

3


Based on the question data, the result of your SQL query is as follows:

$ResultadoLinkATV = [
    (object) ["idVisita" => 33],
    (object) ["idVisita" => 34],
    (object) ["idVisita" => 37]
];

You can get the list of ids through the function array_column of PHP:

$ids = array_column($ResultadoLinkATV, "idVisita"); // [33, 34, 37]

Thus, $ids will be a array in the form [33, 34, 37]. If you specify the values as string, can convert to JSON:

$ids_string = json_encode($ids); // "[33,34,37]"

Or use the implode.

See working on Ideone.

  • Hi @Anderson Carlos Woss, I’m using PHP version 5.6.22 and I couldn’t see the result as exemplified, do I need any special permission? Thanks for the tip.

  • So much json_encode how much array_column are present in this version, so came no reason not to work. As tried to do, in fact?

  • I did it like this: $Idvisita = array_column($Resultadolinkatv, "Idvisita"); print_r($Idvisita); The result is this: Array ( )

  • And nothing was shown? The var_dump of $ResultadoLinkATV is the same as the question?

  • It was not a var_dump but so print_r($Resultadolinkatv);

  • What matters is the result. It’s the same?

  • Yes, it’s the same.

Show 3 more comments

0

This problem could be solved using the SQL COUNT function

SELECT COUNT(supVisitaRtv.IdVisita) AS qnt FROM supVisitaRtv WHERE Tipo = 'ATV' AND supVisitaRtv.Data = ? AND IdEmpresa = ?;

thus, the bank itself calculates how many visits were made of the ATV type by the company and at a certain date informed;

  • I don’t think that’s the problem. It needs to store in the same variable the values of the returned ids, not count the amount of records.

  • Hello @Igor Igor Martinelli, thanks for the suggestion, but it was as Anderson Carlos Woss commented, I do not need sum, this I already have.

Browser other questions tagged

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