I have two functions that I did to display data in a table. But I was not able to create this function in a single function, so I created two, but when I call the two in the foreach it is displayed 4 times for each die.
Here are both functions:
public function getStockName()
{
$query = "SELECT * FROM 'produtos'";
$results = mysqli_query($this->_con, $query) or die(mysqli_error());
$stocks1 = array();
while ( $result = mysqli_fetch_assoc($results) ) {
$stocks1[$result['idProdutos']] = $result['nome'];
}
mysqli_close($this->_con);
return $stocks1;
}
public function getStockQuant()
{
$query = "SELECT * FROM 'produtos'";
$results = mysqli_query($this->_con, $query) or die (mysqli_error());
$stocks2 = array();
while ( $result = mysqli_fetch_assoc($results) ) {
$stocks2[$result['idProdutos']] = $result['quantidade'];
}
mysqli_close($this->_con);
return $stocks2;
}
The first takes the "name" of the "products" table and the second takes the "quantity" from the "products" table. But I end up having to create 2 "foreach" to call the 2 functions, and displays the data 4 times in the table.
I would like to know how to concatenate this function, or if there is some way to call the two functions in a foreach.
My php code looks something like this:
<?php
try {
$user_A = new Cl_User();
$stocks1 = $user_A->getStockName();
if(empty($stocks1)){
$_SESSION['error'] = NO_DATA;
}
} catch (Exception $e) {
$_SESSION['error'] = $e->getMessage();
header('Location: home.php');exit;
}
?>
<?php
try {
$user_B = new Cl_User();
$stocks2 = $user_B->getStockQuant();
if(empty($stocks2)){
$_SESSION['error'] = NO_DATA;
}
} catch (Exception $e) {
$_SESSION['error'] = $e->getMessage();
header('Location: home.php');exit;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="form-estoque" role="form" id="estoque-produto">
<center><h2>Estoque</h2></center>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Produto" title="Type in a name">
<table id="myTable">
<thead>
<tr class="header">
<th style="width:20%;"><center>Nome</center></th>
<th style="width:20%;"><center>Quantidade</center></th>
</tr>
</thead>
<tbody>
<?php foreach ($stocks1 as $key=>$nome) { ?>
<?php foreach ($stocks2 as $key=>$quantidade) { ?>
<tr>
<td><center><?php echo $nome; ?></center></td>
<td><center><?php echo $quantidade; ?></center></td>
</tr>
<?php } ?> <?php } ?>
</tbody>
</table>
</form>