Tour an array with php

0

I have this code that generates an array automatically with the data of a query:

$Codigos = array();
while($row = mysqli_fetch_array($RPedidos)) { $Codigos[] = $row['id']; }
$Idpedidos = implode(',', $Codigos);

I convert it to a value separated by , because then I make a query with FIND_IN_SET . Now apart from that query I have to do another one with a loop to consult each of the codes that it generates separately.

and I'm not sure how to do it. I understand that I have an already created array that is called $ Codes but I do not know very well the syntax that it has since I can not do a print_r to show the data because I am generating a pdf and it does not let me do print_r.

To make a loop that goes through all the codes that I would have to use? the array $ Codes or the Implode separated by , ?

I've tried it this way:

foreach($Codigos as $DContPedidos) {
    $CInPedidos = mysqli_query($Conectar, 'SELECT * FROM Pedidos WHERE 'id' = '.$DContPedidos["id"].' ');
    $html .= 'Codigo Pedidos: '.$CInPedidos["codpedido"].'<br/>';
}

but I get error Warning: Illegal string offset 'id' in with what I understand that the fault I have in the query because $ ContEeds does not have a field called id.

If so, what data do I have to put in DContPedidos to read the content?

    
asked by Killpe 01.12.2017 в 15:34
source

1 answer

1
  

I can not make a print_r to show the data because I am generating a pdf and it does not let me print_r

You should deactivate the header that you set to indicate that the output is a PDF and that way you will be able to visualize the result of print_r .

  

To make a loop that goes through all the codes that I would have to use? the array $ Codes or the Implode separated by,?

The value returned by implode is a string , that is, $Idpedidos it is not a array .

  

What data do I have to give to DContPedidos to read the content?

The variables $Codigos is an array of ids , that is something like this:

[1,2,3,4]

So, when you iterate $Codigos the value of $DContPedidos , it would be 1 , 2 , etc.

Try to do foreach like this:

foreach($Codigos as $id) {
    $CInPedidos = mysqli_query($Conectar, 'SELECT * FROM Pedidos WHERE 'id' = '.$id);
    $pedido = mysqli_fetch_array($CInPedidos , MYSQLI_ASSOC);
    $html .= 'Codigo Pedidos: '.$pedido["codpedido"].'<br/>';
}
    
answered by 01.12.2017 / 16:13
source