I want to use a query to bring the sum of some records, but I want the records to be X element only.
What I want is to use a form with ajax to select a vehicle, which I have identified by id_vehicle, so that the query will return the sum of the services of that vehicle in specific.
This is the code:
include_once('conexion.php');
class Procesar extends Model{
public function __construct(){
parent::__construct();
}
public function build_report($year){
$total = array();
for($i=0; $i<12; $i++){
$month = $i+1;
$sql = $this->db->query("SELECT SUM(utilidad_servicio) AS total FROM servicios_vehiculos WHERE MONTH(fecha_servicio) = '$month' AND YEAR(fecha_servicio) = '$year' LIMIT 1");
$total[$i] = 0;
foreach ($sql as $key){ $total[$i] = ($key['total'] == null)? 0 : $key['total']; }
}
return $total;
}
}
if($_POST['year']){
$class = new Procesar;
$run = $class->build_report($_POST['year']);
exit(json_encode($run));
}