Make php lists in a table

1

I have the following requirement I need to associate all the data of common expenses of the month with each user, besides that if you have not common expenses these are individual associate in a td to that user, and in the end it shows how much you owe here is the first table

Then you have to check this second one in case there is any other expense

for after associating this sum it is shown in the user in the last td that says the amount

here the users table

my progress so far is to show the expenses but I still can not associate to leave each user with a td associated with the expenses and their amount this is the code

<table>
 <tr>
  <th>Propietario</th>
  <th>Descripcion de gastos</th>
  <th>total a pagar</th>
 </tr>

<?php
   include 'conexion.php';
$sql = "SELECT descripcion,monto,factura,create_at FROM gasto_g";
  $res = mysqli_query($conexion,$sql);
while (list($descripcion,$monto,$factura,$create_at)=mysqli_fetch_array($res))  {
    echo " <tr>\n" .
    "  <td></a></td>\n" .
          "  <td>$descripcion</a></td>\n" .
          "  <td>$monto</td>\n" .
          " </tr>\n";
}

?>

</table>

What I expect is something like this, noting that the 2nd user has an individual expense that does not correspond to another

    
asked by Anderson Rey 30.11.2018 в 00:45
source

2 answers

1

With Joins I achieved the goal, thanks to both for their time

$sql = "SELECT relacion,relacionu,descripcion ,nombre,nombred,gasto 
        from gasto_g 
        RIGHT JOIN usuarios  ON usuarios.relacionu= gasto_g.relacion   
        LEFT JOIN gastonocmun   ON usuarios.nombre= gastonocmun.nombred 
        WHERE  MONTH( create_at) = '11'   ";
    
answered by 30.11.2018 / 06:35
source
0

There are several errors in the normalization of the database, but to just abstract to your question

You should also make a call to the customers table, to have the cut condition (per client, which is what you are asking for).

Then ... do not forget the Order By customer

 $res = mysqli_query($conexion,$sql);

 //tendrias que validar que traiga datos 
 $clienteActual = $res("cliente") #asigno cliente actual (el primero)

while (list($descripcion,$monto,$factura,$create_at)=mysqli_fetch_array($res))  {

//pregunto si el cliente es el mismo que tengo guardado

if ($clienteActual = $cliente) {

      //si es el mismo, listo sus  datos
      echo " <tr>\n" .
      "  <td></a></td>\n" .
      "  <td>$descripcion</a></td>\n" .
      "  <td>$monto</td>\n" .
      " </tr>\n";

      #acumulo los gastos de este cliente
      $sumatoria = $sumatoria + $monto
}else {

      //si no es el mismo, muestro la sumatoria 

      //si tengo que mostrar otros datos de gastos personales, hacer otra consulta

      echo " <tr>\n" .
      "  <td></a></td>\n" .
      "  <td>$descripcion</a></td>\n" .
      "  <td>$sumatoria</td>\n" .
      " </tr>\n";
      //inicializo $sumatoria
      $sumatoria = 0
      //pongo el nuevo cliente como el actual
      $clienteActual = $cliente
 }

I hope it serves you

other tips the table should have the date of the expense, to filter the data for the month, although an "imputation date" field is used for those last-minute invoices. The field January and months (the fields in the singular) does not make sense, if there are many you can create a totalizing table per month, to avoid calculations, but for a consortium liquidation it is not a problem.

The name of the table in the select does not match.

    
answered by 30.11.2018 в 01:27