Skip to content

Instantly share code, notes, and snippets.

@fernando-basso
Last active December 22, 2015 14:18
Show Gist options
  • Save fernando-basso/6484721 to your computer and use it in GitHub Desktop.
Save fernando-basso/6484721 to your computer and use it in GitHub Desktop.
<?php
public function getDepartamentos() {
$sql = 'SELECT id, nome, descricao FROM departamento';
if ( $this->pesq != NULL && $this->pesq != 'off' ) {
$sql .= " WHERE nome ILIKE '%:pesq%'"; //OR descricao ILIKE '%:pesq%'";
}
// There will always be a default ord, even if no one was provided.
$sql .= " ORDER BY {$this->ord}";
if ( $this->offset && $this->limite ) {
$sql = " OFFSET :offset LIMIT :limite";
}
//echo $sql;
try {
$pstmt = $this->db->prepare( $sql );
if ( $this->pesq != NULL && $this->pesq != 'off' ) {
$pstmt->bindParam( ':pesq', $this->pesq, PDO::PARAM_INT );
}
if ( $this->offset && $this->limite ) {
$pstmt->bindParam( ':offset', $this->offset, PDO::PARAM_INT );
$pstmt->bindParam( ':limite', $this->limite, PDO::PARAM_INT );
}
$pstmt->execute();
if ( $pstmt->rowCount() > 0 ) {
$res = array();
while( $row = $pstmt->fetch() ) {
$dpt = new Departamento() ;
$dpt->setId( $row[ 'id' ] );
$dpt->setNome( $row[ 'nome' ] );
$dpt->setDescricao( $row[ 'descricao' ] );
array_push( $res, $dpt );
}
return $res;
}
return FALSE;
}
catch( PDOException $err ) {
Util::setErrorMsg( 'Erro a buscar departamentos. ' . $err );
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment