Ver Mensaje Individual
  #6 (permalink)  
Antiguo 07/05/2011, 18:10
angelfcm
(Desactivado)
 
Fecha de Ingreso: abril-2011
Mensajes: 153
Antigüedad: 13 años
Puntos: 236
Respuesta: Carrito de compras básico

Que tal!,

Para tu suerte ahorita ando prácticando casi todo el día hehe, por lo que tú tarea ya la he hecho haha, espero y te agrede, te dejo el código fuente:
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<script src="jquery.js"></script>
<style type="text/css">
</style>
</head>
<body>
<div style="width:420px;background:#adf;border:2px solid #00f">
<?php
 $patron
="/ -> Cantidad: [0-9]*/i";
 
$IP=$_SERVER["REMOTE_ADDR"];
 
$ruta="productos/".$IP.".txt";
 
$nItem=rand(10000,99999);
 
$reload="<script>location.href='html.php'</script>";
 if(!
file_exists("productos"))
  
mkdir("productos");
 if(!
file_exists($ruta)){
  
$f=fopen($ruta,"w");
  
fclose($f);
  echo 
"<script type='text/javascript'>if(!(usuario=prompt(\"Bienvenido!\\r\\n¿Cuál es su nombre?\")))usuario='desconocido';location='?nombre='+usuario;</script>";
 }
 elseif(
filesize($ruta)>0){
  
$divArticulos="<div style='margin:0px auto;font-weight:bold;width:400px;background:orange;border:1px solid black'>";
  
$f=fopen($ruta,"r");$i=0;
  while(!
feof($f)){
    
$linea=fgets($f);
    
$item=str_replace(" #",",",preg_replace($patron,"",$linea));
    
$spanQuitar="<a href='?quitar=".$item."&ruta=".urlencode($ruta)."'>Quitar</a>";
    if(!
$i)
      echo 
"<h2>Bienvenido ".$linea."</h2>";
    elseif(
$i===1)$divArticulos.="Tu lista de articulos es:<ol><li style='border-bottom:1px solid black'>".$linea.$spanQuitar."</li>";
    else 
$divArticulos.="<li style='border-bottom:1px solid black'>".$linea.$spanQuitar."</li>";
    
$i++;
  }
  if(
$i>=2)$divArticulos.="</ol></div>";
  else 
$divArticulos.="Aún no tienes articulos</div><br />";
  
fclose($f);
 }
 else{
   
$i=1;
   
$nombre=$_GET[nombre];
   
$f=fopen($ruta,"w");
   
fputs($f,$nombre);
   
fclose($f);
   echo 
$reload;
 }

 if(isset(
$_POST["agregar"])){
    
$ruta=$_POST["ruta"];
    
$f=fopen($ruta,"a");
    if(!
$f)echo $reload;
    
fputs($f,"\r\n".$_POST['ID']." -> Cantidad: ".$_POST['cantidad']);
    
fclose($f);
    echo 
$reload;
 }
 else{ 
?>
<form action="#" method="post">
<div style="background:white;font-weight:bold">
<input type="hidden" name="ruta" value="<?=$ruta?>" />
<input type="hidden" name="ID" value="Producto #<?=$nItem?>" />
Articulo #<?=$nItem?> | Cantidad <input type="text" name="cantidad" onkeyup="if(!(this.value>=0)){alert('Sólo números');this.value='';return false;}" />
<input type="submit" name="agregar" value="Agregar!">
</div>
</form>
<?php
 
}
echo 
$divArticulos;
 if(isset(
$_GET["quitar"])){
   
$ruta=urldecode($_GET["ruta"]);
    
$item=$_GET["quitar"];
    
$f=fopen($ruta,"r");
    
$l=fread($f,filesize($ruta));
    
fclose($f);
    if(!
$f)echo $reload;$x=0;
    
$item=$item." -> Cantidad:";
    
$item=str_replace(","," #",$item);
    
$l=preg_replace("/(\r\n)?".$item." [0-9]*/i","",$l);
    
$f=fopen($ruta,"w");
    
fwrite($f,$l);
    
fclose($f);
    echo 
$reload;
 }
?>
</div>
</body>
</html>
Sólo guardalo con extensión .php

Saludos!