Search data by selected month php mysql

3

I have a table in mysql with three fields idPago value mespago. In it I have some data and I want to make a query by mespago, that when selecting the month in the datapicker I show in a table the sum of value only of those that belong to the selected month. I have this.

//Acá seleccione el mes

<input type="text" id="mespago" name="mespago" class="form-control" 
  <?php $sumapormes = pagoData::pormes("valor");
      echo $sumapormes->sum;?>/>
      
//Esta es la tabla donde quiero mostrar la suma del valor del mes seleccionado

<div class="box-body">
  <?php if (count($reporte)>0): ?>
    <table class="table table-bordered table-hover">
      <thead id="cabe">
        <th>Suma por mes</th>
      </thead>   
<tr>
<td><?php $apostadoSuma = pagoData::sumar("valor");
      echo $apostadoSuma->suma;?>
</td>                      
</tr>   
</table>
  <?php else: ?>
    <p class="alert alert-warning">No Se Encontraron Datos</p>
  <?php endif; ?>

</div>

//La función que tengo para que me busque por mes

	public static function pormes($valor) {
    	$sql="select SUM($valor) AS sum FROM ".self::$tablename." where mespago=$mespago";
		$query = Executor::doit($sql);
		return Model::one($query[0],new pagoData());
	}

But it does not show me anything when selecting somemes

    
asked by Horus 25.02.2018 в 01:23
source

3 answers

0

I have noticed several errors in your query, the first is that you make SUM of a static value ($ value), that is already weird and the other is that you have another variable $ mespago that is not anywhere, you were not bad , it would be something like this:

public static function pormes($mesPago) {
        $sql="select SUM(valor) AS sum FROM ".self::$tablename." where mespago=$mesPago";
        $query = Executor::doit($sql);
        return Model::one($query[0],new pagoData());
    }

Noting that all the rest of the interaction with the database is fine, I think it should work.

Greetings

    
answered by 03.04.2018 в 12:35
0
public static function pormes($mesPago) {
    $sql="select SUM(valor) AS sum FROM ".self::$tablename." where MONTH(campoFecha)=$mesPago";
    $query = Executor::doit($sql);
    return Model::one($query[0],new pagoData());
}
    
answered by 03.04.2018 в 15:49
0

The problem could be that you are passing "value" to the method that executes the query. You also have an error in that you are not setting the value of the input where you want it to be displayed. The input would be as follows:

<?php $sumapormes = pagoData::pormes($mes); ?>
<input type="text" id="mespago" name="mespago" class="form-control" 
  value ="<?php echo $sumapormes ?>" /> 
    
answered by 11.07.2018 в 20:48