Add data from database

Asked

Viewed 79 times

0

I am using this function to search for a field in a table:

function rendimentoDia($conexao, $dia) {
$rendimentos = array();
$resultado = mysqli_query($conexao, "select valor from hospedes where dia = 
$dia");
while($rendimento = mysqli_fetch_assoc($resultado)) {
    array_push($rendimentos, $rendimento);
}
return $rendimentos;
}

then I use this code to display the data on the screen:

<table class="container">

<?php
    $rendimentos = rendimentoDia($conexao, $dia);

    foreach($rendimentos as $rendimento) :
?>
<tr>
    <td><?= $rendimento['valor']?></td>
</tr>
<?php

   endforeach
?>
</table>

This field brings the values entered in a day, I would like to add these values and I am not getting, I tried to add using array_sum in a variable that saved the value yield, but it did not work.. the function is bringing everything right and displaying, I just can’t add even. I thank anyone who can help

  • If you do the direct sum in SQL that brings the information not right ? For example, do the following: select SUM(valor) from hospedes where dia = &#xA;$dia

  • You need all the separate values as well or just the sum?

  • hello Anderson, I will try here by mysql, I just need the same result, thanks

1 answer

0


Well, as you said in the comments that you only need the total value and not the separate values, you can change the functionality directly in your query that will work, just leave your code this way:

$rendimentos = mysqli_query($conexao, "select SUM(valor) from hospedes where dia = $dia");
$rendimento = mysqli_num_rows($resultado);

<table>

<?php
while($rendimento = mysqli_fetch_array($resultado)) {
?>

<tr>
    <td><?= $rendimento['sum(valor)']; ?></td>
</tr>
        <?php
    }
?>
</table>

Thinking about the chance you said you get the $conexaothrough a include and the $dia through the date you declare ( I believe in the same file ), these variables will be accessible, so you do not need to declare a function and call it right after. You can simply mount your Query and just below do the while together to your table to print the column sum value. You can put the variables responsible for your query at the top of your file or even before your table, just be sure to put it between PHP tags.

  • It’s not working here, when I try to display it says it’s an array... I need it to give me only the number, it’s like?

  • Well, you can search the values without declaring that these are Arrays. I will edit the answer.

  • Only one question before, the connection variable you receive via includeof some file conexao.php for example ? And the variable dia, you receive via POST form or manually declares ?

  • I create the variable with the date

  • Got it, good I’ll edit here

  • a, connection comes from a include yes

  • Try the way I put it, take the test.

  • me devolved esses erros: Notice: Array to string Conversion in C: xampp htdocs oni rendimento_dia_v.php on line 9 Array Warning: Invalid argument supplied for foreach() in C: xampp htdocs oni rendimento_dia_v.php on line 19

  • I swapped the fetch_assoc for the array, try the way I put it now. If not, I have another solution.

  • Francis, now that I realize it. You were making a foreachin your table that was unnecessary, because you just need to print the full value of the column, and not make a looping to put all the values of this. With this, you just need to declare your Function and print the value in the table.

  • I tried with another function, but also could not....

  • Well, I’ll do it another way and explain in the answer.

  • didn’t work out that way?

  • I edited the answer already, take a look.

Show 9 more comments

Browser other questions tagged

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