Está en la página 1de 13

Pablo Jiménez

Tarea 5
Con la finalidad de llevar a cabo un sistema que permita obtener información de los clientes que visitan el
portal de una librería, se debe crear una aplicación en el lenguaje PHP, la cual permita obtener el nombre,
apellido, y RUT del cliente que ejecuta la visita. Para cumplir dicho requerimiento te han contratado como
técnico en informática y te han pasado la siguiente lista de requerimientos.

DESARROLLO

A continuación, elabora lo siguiente:


1. A partir de lo planteado, desarrolla un programa en PHP donde se crea una sesión y se registre como
variables de sesión: nombre, apellido y RUT. Agregar los captures a la tarea.

Código:

<?php
session_start();
?>

<form action="save.php" method="POST">


<label for="nombre">Nombre:</label><br>
<input type="text" id="nombre" name="nombre" required><br>
<label for="apellido">Apellido:</label><br>
<input type="text" id="apellido" name="apellido" required><br>
<label for="rut">RUT:</label><br>
<input type="text" id="rut" name="rut" required><br>
<input type="submit" value="Submit">
</form>
Evidencia:

2. Desarrolla un programa adicional en PHP, donde se pueda imprimir las variables de sesión
determinadas en la solicitud anterior. Agregar los captures a la tarea.

Código:

<?php

$_SESSION["nombre"] = $_POST["nombre"];
$_SESSION["apellido"] = $_POST["apellido"];
$_SESSION["rut"] = $_POST["rut"];
echo "nombre: ".$_SESSION["nombre"]."<br>";
echo "apellido: ".$_SESSION["apellido"]."<br>";
echo "rut: ".$_SESSION["rut"]
?>
Evidencia:

3. Desarrolla un programa en el cual se implemente una tienda virtual de la librería, no más de 5 libros
aplicando el uso de sesiones y carrito de compra. Agregar los captures a la tarea.
Código:

index.php

<?php
session_start();
require("includes/connection.php");
if(isset($_GET['page'])){
$pages=array("products", "cart");
if(in_array($_GET['page'], $pages)) {
$_page=$_GET['page'];
}else{
$_page="productos";
}
}
else{
$_page="productos";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
</head>
<body>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div><!--end of main-->
<div id="sidebar">
<h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($query)){
?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php
}
?>
<hr />
<a href="index.php?page=cart">Go to cart</a>
<?php
}else{
echo "<p>Your Cart is empty. Please add some products.</p>";
}
?>
</div><!--end of sidebar-->
</div><!--end container-->
</body>
</html>

productos.php

<?php
session_start();
require("includes/connection.php");
if(isset($_GET['page'])){
$pages=array("products", "cart");
if(in_array($_GET['page'], $pages)) {
$_page=$_GET['page'];
}else{
$_page="productos";
}
}
else{
$_page="productos";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
</head>
<body>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div><!--end of main-->
<div id="sidebar">
<h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($query)){
?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php
}
?>
<hr />
<a href="index.php?page=cart">Go to cart</a>
<?php
}else{
echo "<p>Your Cart is empty. Please add some products.</p>";
}
?>
</div><!--end of sidebar-->
</div><!--end container-->
</body>
</html>

cart.php

<?php
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
?>

<h1>View cart</h1>
<a href="index.php?page=productos">Go back to the products page.</a>
<form method="post" action="index.php?page=cart">
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Items Price</th>
</tr>
<?php
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";

}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysqli_query($conn, $sql);
$totalprice=0;
while($row=mysqli_fetch_array($query)){
$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
$totalprice+=$subtotal;
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo
$_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td>
<td><?php echo $row['price'] ?>$</td>
<td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>$</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4">Total Price: <?php echo $totalprice ?></td>
</tr>
</table>
<br />
<button type="submit" name="submit">Update Cart</button>
</form>
<br />
<p>To remove an item set its quantity to 0. </p>

includes/connection.php

<?php
$server="127.0.0.1";
$user="root";
$pass="root";
$db="libreria";
// connect to mysql
$conn = mysqli_connect($server, $user, $pass, $db, "3307") or die("Sorry, can't connect to the
mysql.");
// select the db
//mysqli_select_db($db) or die("Sorry, can't select the database.");
?>

Evidencia:
REFERENCIAS BIBLIOGRÁFICAS

Canal J&G proyectos web. (5 de Octubre de 2017). Sesiones en PHP($_SESSION) []. youtube. 9. Sesiones
en PHP($_SESSION)

También podría gustarte