Ejemplo de clase para operaciones con registros de una Base de Datos, insertar, editar, listar…
class noticias{
var $bbdd;
function noticias($mysql){
$this->bbdd = $mysql;
}
function guardarNoticias($arrNoticias){
$sql = "INSERT INTO `EDMSG_noticias` VALUES (
'',
'" . $arrNoticias['titulo'] . "',
'" . $arrNoticias['texto'] . "',
'" . $arrNoticias['imagen'] . "',
'" . $arrNoticias['dia_semana'] . "',
'" . $arrNoticias['fecha'] . "',
'" . $arrNoticias['activo'] . "',
'" . date("Y-m-d H:i:s") . "')";
$link = $this->bbdd->conectar();
$this->bbdd->consultar($sql,$link);
}
function editarNoticias($arrNoticias, $id){
$sql = "UPDATE `EDMSG_noticias` SET
titulo = '" . $arrNoticias['titulo']."',
texto = '" . $arrNoticias['texto']."',
imagen = '" . $arrNoticias['imagen']."',
dia_semana = '" . $arrNoticias['dia_semana'] . "',
fecha = '" . $arrNoticias['fecha'] . "',
activo = '" . $arrNoticias['activo'] . "'
WHERE id = " . $id;
$link = $this->bbdd->conectar();
$this->bbdd->consultar($sql,$link);
}
function borrarNoticias($id){
$sql = sprintf("DELETE FROM `EDMSG_noticias` WHERE id = %s", $id);
$link = $this->bbdd->conectar();
$this->bbdd->consultar($sql,$link);
}
function getPrimeraNoticia(){
$sql = "SELECT * FROM `EDMSG_noticias` WHERE activo LIKE 1 ORDER by fecha";
$link = $this->bbdd->conectar();
$this->bbdd->consultar($sql,$link);
$rs = mysql_fetch_array($this->bbdd->getResult());
$datos = array();
$datos['id'] = $rs['id'];
if($rs['titulo'] != "") $rs['titulo']; uot;";
if($rs['texto'] != "") $rs['texto']; uot;";
if($rs['imagen'] != "") '] ']; '] uot;";
if($rs['dia_semana'] != "") $datos['dia_semana'] a']; a'] uot;";
if($rs['fecha'] != "") $rs['fecha']; uot;";
if($rs['fecha_registro'] != "") $datos['fecha_registro'] = $rs['fecha_registro']; else $datos['fecha_registro'] = "";
return $datos;
}
function listarNoticias(){
$sql = "SELECT * FROM `EDMSG_noticias`";k= $this->bbdd->conectar();
$this->bbdd->consultar($sql,$link);
$this->total = $this->bbdd->getTotal();
$listNoticias = array();
$i=0;
while($rs=mysql_fetch_array($this->bbdd->getResult())){
$listNoticias[$i]['id'] oticias[$i]['titulo'] oticias[$i]['texto'] oticias[$i]['imagen'] '];
$listNoticias[$i]['dia_semana'] a'];
$listNoticias[$i]['fecha'] oticias[$i]['fecha_registro'] = $rs['fecha_registro'];
$i++; $listNoticias;
}
function getNoticia($id){
$sql = sprintf("SELECT * FROM `EDMSG_noticias` WHERE id = %s", $id);
$link = $this->bbdd->conectar();
$this->bbdd->consultar($sql,$link);
$rs = mysql_fetch_array($this->bbdd->getResult());
$datos = array();
$datos['id'] = $rs['id'];
if($rs['titulo'] != "") $rs['titulo']; uot;";
if($rs['texto'] != "") $rs['texto']; uot;";
if($rs['imagen'] != "") '] ']; '] uot;";
if($rs['dia_semana'] != "") $datos['dia_semana'] a']; a'] uot;";
if($rs['fecha'] != "") $rs['fecha']; uot;";
if($rs['fecha_registro'] != "") $datos['fecha_registro'] = $rs['fecha_registro']; else $datos['fecha_registro'] = "";
if($rs['activo'] != "") $datos['activo'] = $rs['activo']; else $datos['activo'] = "";
return $datos;
}
function getNumNoticias(){
$sql = "SELECT count(id) as total FROM `EDMSG_noticias`";mysql_fetch_array($this->bbdd->getResult()); $rs['total'];ction getTotalnoticias(){
return $this->total;
}
}

