Send Table Data MYSQL-PHP to Another Table with FORM - $ _POST - foreach

0

I have a php table that is filled with a mysql database ...

<table class="table table-striped">
<thead>                                                     
    <tr>
        <th class="text-center" colspan="24"><input type="submit" name="txtEnviar" value="Enviar" class="form-control"></th>
    </tr>
    <tr>
        <th class="text-center">ODC</th>
        <th class="text-center">Pedido</th>
        <th class="text-center">Modelo</th>
        <th class="text-center">Select</th>
    </tr>
</thead>
<tbody>
    <? foreach ($arrPedido2 as $rowPedido2) { ?>
    <tr class="gradeX">
        <td class="text-center"><input type="hidden" name="txtNoOdc" value="<? echo $rowPedido2['noOdc'];?>"> <? echo utf8_encode($rowPedido2['noOdc']);?></td>
        <td class="text-center"><input type="hidden" name="txtPedido" value="<? echo $rowPedido2['pedido'];?>"><? echo utf8_encode($rowPedido2['pedido']);?> </td>          
        <td class="text-center"><input type="hidden" name="txtModelo" value="<? echo $rowPedido2['modelo'];?>"><? echo utf8_encode($rowPedido2['modelo']);?></td>
        <td class="text-center"><input type="checkbox" name="txtIdPedido[]" value="<? echo utf8_encode($rowPedido2['idPedido']);?>"></td>
    </tr>
    <? }; ?>
</tbody>

And I want to send the selected ones through a checkbox to another table

<table class="table table-striped">
<thead>                                                     
    <tr>
        <th class="text-center">ODC</th>
        <th class="text-center">Pedido</th>
        <th class="text-center">Modelo</th>
        <th class="text-center">Select</th>
    </tr>
</thead>
<tbody>
    <tr class="gradeX">
        <td class="text-center"> </td>
        <td class="text-center"> </td>          
        <td class="text-center"> </td>
        <td class="text-center"> </td>
    </tr>
</tbody>

Eat serious?

    
asked by Tony Muñoz 24.08.2016 в 22:50
source

3 answers

1

Hello friend if it's just with PHP

Add a form to your table, do not forget a button to submit.

<form method="post" action="paimprimir.php">
  <table>
    <tr>
      <td><input type="checkbox" name="algo[]" value="algo">
    </tr>
  </table>

  <button> Enviar </button>
</form>

In the other file (for example paimprimir.php), you will get the ones selected in the%% of%

array

However, only the values you entered at $_POST['algo'] of the checkbox will arrive. (in my example the string "something").

If you would like to take all the data from the table.

Well there are 2 methods.

1. The Cleanest and best for just PHP.

Based on the IDs sent in the $ _POST array, make a new query that will bring you the data of the selected ones. value

SELECT * FROM table WHERE id IN $_POST['algo']

After that you already do the same thing that you did in your first table and to print.

2. The easy, but complicated.

Ammm the truth is that it is to put all the data in a Javascript fix and send that arrangement to the new page, where it will be processed as such.

The truth is that the process is slower but more efficient (in theory) because you do not have to make another query to MySQL.

However, I will not give more details on this, because believe me if PHP was complicated, understanding the logic of the JS is going to be a problem.

Greetings

    
answered by 25.08.2016 / 16:23
source
0

Can not use javaScript? That is, the solution should be given only with Php? and, are the tables on the same page or different pages?

    
answered by 24.08.2016 в 23:22
0
  

Look here this, what you could do is just leave an ID in the select and not send anything by hidden as you had previously, then send the IDs by POST and get to table 2 where the query is made to the BD with those IDs and the table is filled. I hope you serve, any concern, you let me know.   Greetings.

TABLE 1

<?php
/************simulador de tabla de mysql***********************************/
	$arrPedido2 = array(
						array("noOdc" => "noOdc uno", "pedido" => "pedido uno", "modelo" => "modelo uno", "idPedido" => "1"),
						array("noOdc"=>"noOdc dos", "pedido"=>"pedido dos", "modelo"=>"modelo dos", "idPedido"=>"2"),
						array("noOdc"=>"noOdc dos", "pedido"=>"pedido dos", "modelo"=>"modelo dos", "idPedido"=>"3")
						);

 ?>
 <html>
 <body>
<table class="table table-striped" border=1>
<thead>                                                     
  
    <tr>
        <th class="text-center">ODC</th>
        <th class="text-center">Pedido</th>
        <th class="text-center">Modelo</th>
        <th class="text-center">Select</th>
    </tr>
</thead>
<form action='tabla2.php' method='POST'>
<tbody>
    <?php foreach ($arrPedido2 as $rowPedido2) { ?>
    <tr class="gradeX">
        <td class="text-center"><?php echo utf8_encode($rowPedido2['noOdc']);?></td>
        <td class="text-center"><?php echo utf8_encode($rowPedido2['pedido']);?> </td>          
        <td class="text-center"><?php echo utf8_encode($rowPedido2['modelo']);?></td>
        <td class="text-center"><input type="checkbox" name="txtIdPedido[]" value="<?php echo utf8_encode($rowPedido2['idPedido']);?>"></td>
    </tr>
    <?php }; ?>
	  <tr>
        <th class="text-center" colspan="24"><input type="submit" name="txtEnviar" value="Enviar" class="form-control"></th>
    </tr>
</tbody>
</form>
</body>
</html>

TABLE 2

<?php

$array_post = array();

foreach($_POST as $valor){
   array_push($array_post, $valor);
} 

echo 'Asi quedaria el arreglo de las filas seleccionadas: <br>';
print_r($array_post);
echo '<hr>';

/*Crear el IN para el sql */
$ids_Pedidos = '';

foreach ($array_post[0] as $rowPedido2){
	
	$ids_Pedidos = $ids_Pedidos."'".$rowPedido2."'," ;
}
 
 $ids_Pedidos = substr($ids_Pedidos, 0, -1); //uitar ultima coma

echo 'Debo hacer la consulta a la base de datos para sacar las ID que se seleccionaron anteriormente <br>';
echo  ' la consulta seria: <br><br>';

$CONSULTA = 'SELECT * FROM pedidos WHERE idPedido IN ('.$ids_Pedidos.');';

echo $CONSULTA;
	
/* LLENAR LA TABLA NORMAL CON EL RESULTADO DE LA CONSULTA ANTERIOR*/

?>
    
answered by 25.08.2016 в 19:15