Home » Php » php – Create new array only to print checked rows from table

php – Create new array only to print checked rows from table

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a table populated by a query, througt a simple foreach loop. Now, I want to print the checked rows of that table, but the table that is currently printable it’s on a another , because they got completly different formats and styles and the second table(the one that actually I am able to print) gets the data with the exact same query as a result_array.

I am trying to figure out how to convert the first table’s checked rows into a separate array and use that one instead of the query of the second one, so only the checked rows can be print.

I have been trying to print the 1st table itself but it doesnt show as I want and it uses a lot of color ink (wich is out of question).
i have been trying different ways for quite some time now, but i didnt realize that they were on diferent containers(totally a dumb mistake, ik) and it just hit the idea of making this new array to make it happen, but just have no idea how to proceed.

<div class="tab-pane" id="TLL">
<table class="table table-bordered" style="color: Black" id="table-3">
<?php
$control_info= $this->db->query("select * 
                                from controlsemanal 
                                where vendedora = 'Taller' 
                                and Indicador != '1' 
                                and Indicador !='100' 
                                ORDER BY FIELD (Id_Status,1,3,4,5,6,2,7,0) ");
?>
<thead> etc etc </thead>

<tbody>
<?php
$arr_loop = $control_info;
foreach ($arr_loop->result_array() as $row) {
<tr> <th>
<div>
<input type="checkbox" name="check[]" value="">
</div>
</th>
etc etc
</tr>
</tbody>
</table>

and this is the printable table

<div style="display:none" id="imprimeme3">
$control_info= $this->db->query("select * 
                                from controlsemanal 
                                where vendedora = 'Taller' 
                                and Indicador != '1' 
                                and Indicador !='100' 
                                ORDER BY FIELD (Id_Status,1,3,4,5,6,2,7,0) ");
<table>
foreach ($control_info->result_array() as $row) {
etc etc
}
</table>

<center>
<button style="border:0;background:#04998A;color:white;width:550px;height:50px;font-size: 30px" onclick="imprimir3();">
<b>IMPRIMIR</b>
</button>
</center>
</div>

the function to print the 2nd table

function imprimir3() {
        var objeto = document.getElementById('imprimeme3'); 
        var ventana = window.open('', '_blank'); 
        ventana.document.write(objeto.innerHTML);        
        ventana.document.close();
        ventana.print();
        ventana.close();
    }

i tried to make the code as short as posible here to dont be a bother, any help would be appreciate, thanks a lot.

How to&Answers:

Ok, creating the form in the first piece of code, use your primary key as the value for checkboxes:

<div class="tab-pane" id="TLL">
<table class="table table-bordered" style="color: Black" id="table-3">
<?php
$control_info= $this->db->query("select * 
                                from controlsemanal 
                                where vendedora = 'Taller' 
                                and Indicador != '1' 
                                and Indicador !='100' 
                                ORDER BY FIELD (Id_Status,1,3,4,5,6,2,7,0) ");
?>
<thead> etc etc </thead>

<tbody>
<?php
$arr_loop = $control_info;
foreach ($arr_loop->result_array() as $row) {
?>
<tr> <th>
<div>
<input type="checkbox" name="check[]" value="<?php echo $row['referencia']; ?>">
</div>
</th>
etc etc
</tr>
<?php
} // I guess foreach ends here
?>
</tbody>
</table>

Then in your second table (process $_POST form) get only marked items:

  1. check must be an array, containing reference for each marked item
  2. Unmarked checkboxes are not sent with form
  3. Make a list from them to create the filter “ref1, ref2, … refX”

I’m guessing your reference column is numeric, if don’t, comment and I’ll fix the query

$referencias = (isset($_POST['check'])) ? implode(', ', $_POST['check']) : '';

// Create your query, filtering by $references
$control_info= $this->db->query("select * 
                            from controlsemanal 
                            where vendedora = 'Taller'
                            and referencia IN ($referencias)
                            ORDER BY FIELD (Id_Status,1,3,4,5,6,2,7,0) ");
// Create your table here