Concatenate two functions! (Mysql/PHP)

Asked

Viewed 179 times

1

I have two functions I’ve done to display data in a table. However I was not able to create this function in a single function, so I created two, however, when I call both in the "foreach" it is displayed 4 times for each data.

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" of the "products" table. However I end up having to create 2 "foreach" to call the 2 functions, and displays the data 4 times in the table.

I wanted to know how to concatenate this function, or if there is any 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>

1 answer

2


Could simplify using associative array, this just:

public function getStockName()            
{
    $query = "SELECT * FROM `produtos`";
    $results = mysqli_query($this->_con, $query)  or die(mysqli_error());
    $stocks = array();

    while ( $result = mysqli_fetch_assoc($results) ) {
        $stocks[$result['idProdutos']] = array(
            'nome' => $result['nome'],
            'quantidade' => $result['quantidade']
        );
    }

    mysqli_close($this->_con);
    return $stocks;
}

And to use it would be this:

<tbody>
    <?php foreach ($stocks as $id => $stock) { ?>
        <tr>

            <td><center><?php echo $stock['nome']; ?></center></td>
            <td><center><?php echo $stock['quantidade']; ?></center></td>

        </tr>
    <?php } ?>
</tbody>
  • Thank you, that’s exactly what I was looking for, will help me in the next tables I’ll do. Just one question, why in "foreach" did you change the "$key" for "$id"? I honestly don’t know what "$key" was for, too, if you can explain thank you.

  • @Anonn the $key in your original code is the ID of each row of your bank, so name it $id to make it more intuitive.

Browser other questions tagged

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