Código PHP:
class Carrito {
var $conn;
public function __construct() {
$this->Conectar();
}
private function Conectar() {
$this->conn = mysql_connect(HOST, USER, PASS);
mysql_select_db(DB);
return $this->conn;
}
private function EjecutarQuery($sql) {
return mysql_query($sql);
}
private function NumeroFilas($rs) {
return mysql_num_rows($rs);
}
private function RetornarFilas($rs) {
return mysql_fetch_array($rs);
}
/*********************************************/
/* */
/* Funciones para carrito */
/* */
/*********************************************/
public function ConseguirDatosProducto($producto) {
$str = sprintf("SELECT A.codigo, A.nombre, A.foto_chica, B.marca FROM producto A, marca B WHERE A.codigo ='%s' AND A.id_marca = B.id_marca",
mysql_real_escape_string($producto));
$rs = $this->EjecutarQuery($str);
return $this->RetornarFilas($rs);
}
public function AgregarCarrito($id, $pro, $nom, $fot, $can, $mar) {
$str = sprintf("SELECT car_id, car_prd, car_can FROM carrito WHERE car_id = '%s' AND car_prd = '%s'",
mysql_real_escape_string($id),
mysql_real_escape_string($pro));
$rs = $this->EjecutarQuery($str);
$nr = $this->NumeroFilas($rs);
if ($nr > 0) {
$arr1 = $this->RetornarFilas($rs);
$can = $arr1[2];
$ncan = $can++;
$str = sprintf("UPDATE carrito SET car_can = %d WHERE car_id = '%s'",
mysql_real_escape_string($ncan),
mysql_real_escape_string($id));
}
else {
$str = sprintf("INSERT INTO carrito VALUES ('%s', '%s', '%s', '%s', %d, '%s')",
mysql_real_escape_string($id),
mysql_real_escape_string($pro),
mysql_real_escape_string($nom),
mysql_real_escape_string($fot),
mysql_real_escape_string($can),
mysql_real_escape_string($mar));
}
if ($this->EjecutarQuery($str)) {
header("location: vercarrito.php?id=$id");
}
}
public function VerCarrito($id) {
$str = sprintf("SELECT * FROM carrito WHERE car_id = '%s'",
mysql_real_escape_string($id));
$rs = $this->EjecutarQuery($str);
$nr = $this->NumeroFilas($rs);
echo "<table>";
echo "<tr>";
echo "<th>Producto</th>";
echo "<th>Descripcion</th>";
echo "<th>Marca</th>";
echo "<th>Cantidad</th>";
echo "<th>Foto</th>";
echo "</tr>";
if ($nr > 0) {
while ($arr = $this->RetornarFilas($rs)) {
echo "<tr class='contenido'>";
echo "<td class='contenido'>$arr[1]</td>";
echo "<td class='contenido'>$arr[2]</td>";
echo "<td class='contenido'>$arr[5]</td>";
echo "<td class='contenido'>$arr[4]</td>";
echo "<td class='contenido'><img src='intranet/imagenes/foto/".$arr[3]."' alt='' /></td>";
echo "<td class='contenido'><a href=\"javascript:EliminarItem('".$arr[0]."', '".$arr[1]."');\">Eliminar</a></td>";
echo "</tr>";
}
}
else {
echo "<tr>";
echo "<td>No hay productos seleccionados.</td>";
echo "</tr>";
}
echo "</table>";
}
public function EliminarItemCarrito($id, $producto) {
$str = sprintf("DELETE FROM carrito WHERE car_id = '%s' AND car_prd = '%s'",
mysql_real_escape_string($id),
mysql_real_escape_string($producto));
if ($this->EjecutarQuery($str)) {
header("location: vercarrito.php?id=$id");
exit();
}
else {
echo "<script type='text/javascript'>";
echo "alert('No se puede eliminar el item.');location.href='vercarrito.php?id='".$id.";";
echo "</script>";
}
}
public function EnviarPedido($id, $email) {
//Contenido del correo
$to = "whonores@57sac.com";
$subject = "Pedido de usuario";
$contenido = "Correo enviado el " . date("d/m/Y") . " a las " . date("H:i:s");
$contenido .= "\r\n\r\n";
$contenido .= "Contenido del pedido" . "\r\n\r\n";
$contenido .= "CODIGO / PRODUCTO / CANTIDAD / MARCA" . "\r\n\r\n";
$str = sprintf("SELECT * FROM carrito WHERE car_id = '%s'",
mysql_real_escape_string($id));
$rs = $this->EjecutarQuery($str);
while ($arr = $this->RetornarFilas($rs)) {
$contenido .= $arr[1] . " - " . $arr[2] . " - " . $arr[4] . " - " . $arr[5] . "\r\n";
}
$contenido .= "\r\n\r\n" . "Enviado por: $email" . "\r\n\r\n";
if (mail($to, $subject, $contenido, "From: ".$email)) {
$str = sprintf("DELETE FROM carrito WHERE car_id = '%s'",
mysql_real_escape_string($id));
$this->EjecutarQuery($str);
}
}
/*********************************************/
/* */
/* Funciones para noticias */
/* */
/*********************************************/
public function MostrarNoticias() {
$str = "SELECT * FROM noticias ORDER BY not_fecha DESC";
$rs = $this->EjecutarQuery($str);
$nr = $this->NumeroFilas($rs);
$res = "<table>";
$res .= "<tr>";
$res .= "<th>Código</th>";
$res .= "<th>Fecha</th>";
$res .= "<th>Tema</th>";
$res .= "<th>Resumen</th>";
$res .= "<th>Activo</th>";
$res .= "</tr>";
if ($nr > 0) {
while ($arr = $this->RetornarFilas($rs)) {
$res .= "<tr>";
$res .= "<td>".$arr[0]."</td>";
$res .= "<td>".date("d/m/Y", strtotime($arr[1]))."</td>";
$res .= "<td>".$arr[2]."</td>";
$res .= "<td>".$arr[3]."</td>";
$res .= "<td>".$arr[6]."</td>";
$res .= "</tr>";
}
}
else {
$res .= "<tr>";
$res .= "<td colspan='5'>No hay noticias publicadas.</td>";
$res .= "</tr>";
}
$res .= "</table>";
echo $res;
}
}