How can I make this query dynamic, it is already working only that it only lists one semester of each year and wanted it to list two, type when I select the first it lists the first and when I request the second it lists the second.
public function RetornaIndoperacionais($ano){
if($ano == "2016"){
$_base = "pla_est_2016";
}else{
$_base = "pla_est";
}
$Query = ("SELECT i.codiniciativa,
te.nome as nome_tema,
te.codtemaestrategico as cod_te,
p.observacoes as per_obs,
te.sequencia as tem_sequencia,
p.sigla as per_sigla,
p.nome as nome_perspectiva,
p.codperspectiva as cod_perspectiva,
o.sequencia as sequencia_obj,
o.codobjetivo as cod_obj,
o.nome as nome_obj,concat(p.sigla, ' ', te.sequencia,
'.', o.sequencia, '.',i.sequencia) as codigo,i.nome as nom_iniciativa,
i.sequencia as iniciativa_sequencia,
i.metas,i.responsavel,i.indicadores,
(SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 1 ORDER BY a.data DESC LIMIT 1),
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 1 AND curdate() >= '2016-01-01' ORDER BY a.data DESC LIMIT 1), 0)) as '01',
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 2 ORDER BY a.data DESC LIMIT 1),
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 2 AND curdate() >= '2016-02-01' ORDER BY a.data DESC LIMIT 1), 0)) as '02',
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 3 ORDER BY a.data DESC LIMIT 1),
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 3 AND curdate() >= '2016-03-01' ORDER BY a.data DESC LIMIT 1), 0)) as '03',
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 4 ORDER BY a.data DESC LIMIT 1),
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 4 AND curdate() >= '2016-04-01' ORDER BY a.data DESC LIMIT 1), 0)) as '04',
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 5 ORDER BY a.data DESC LIMIT 1),
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 5 AND curdate() >= '2016-05-01' ORDER BY a.data DESC LIMIT 1), 0)) as '05',
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 6 ORDER BY a.data DESC LIMIT 1),
ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 6 AND curdate() >= '2016-06-01' ORDER BY a.data DESC LIMIT 1), 0)) as '06'
FROM $_base.perspectivas p
LEFT JOIN $_base.temasestrategicos te ON p.codperspectiva = te.codperspectiva
LEFT JOIN $_base.objetivos o ON te.codtemaestrategico = o.codtemaestrategico
LEFT JOIN $_base.iniciativas i ON o.codobjetivo = i.codobjetivo
WHERE p.codperspectiva = '5' AND p.status = 'A' AND te.status = 'A' AND o.status = 'A' AND i.status = 'A'ORDER BY p.sequencia ASC,te.sequencia ASC,
o.sequencia ASC,CAST(i.sequencia AS DECIMAL) ASC" );
//echo "</pre>"; print_r($Query); exit;
//while ($_indoperacionais = mysql_fecth_array($Query));
try {
$this->search = $this->MySql->prepare($Query);
//$this->search->bindParam(":nome", $this->indoperacionais);
$this->search->execute();
} catch(PDOException $e) {
die($e->getMessage());
}
$_result = $this->search->fetchAll(PDO::FETCH_ASSOC);
return $_result;
And this is the code in PHP to understand how I want the call to work.
/* codigo para gerar os paramentros */
$ano = $_POST["ano"];
$_perspectiva = $_POST["perspectiva"];
//echo "<pre>"; print_r ($_POST);exit;
if ($_POST["semestre"] == "primeiro") {
$_parametros = array (
"ano" => $ano,
"mes1" => 1, "mes2" => 2, "mes3" => 3,
"mes4" => 4, "mes5" => 5, "mes6" => 6,
"nomeMes1" => "Janeiro", "nomeMes2" => "Fervereiro", "nomeMes3" => "Março",
"nomeMes4" => "Abril", "nomeMes5" => "Maio", "nomeMes6" => "Junho",
"perspectiva" => $_perspectiva);
}else if ($_POST["semestre"] == "segundo") {
$_parametros = array (
"ano" => $ano,
"mes1" => 7, "mes2" => 8, "mes3" => 9,
"mes4" => 10, "mes5" => 11, "mes6" => 12,
"nomeMes1" => "Julho", "nomeMes2" => "Agosto", "nomeMes3" => "Setembro",
"nomeMes4" => "Outubro", "nomeMes5" => "Novembro", "nomeMes6" => "Dezembro",
"perspectiva" => $_perspectiva);
}else {
$error = "ERRO";
}
//echo "<pre>"; print_r ($_parametros);exit;
//Instanciando a classe
$_model = new model_Indoperacionais();
$_indoperacionais = $_model->RetornaIndoperacionais($ano,$_parametros);