Ver Mensaje Individual
  #3 (permalink)  
Antiguo 27/10/2014, 12:14
Avatar de andres_15_
andres_15_
 
Fecha de Ingreso: septiembre-2008
Ubicación: Cali
Mensajes: 232
Antigüedad: 16 años, 4 meses
Puntos: 7
Respuesta: Problema con carrito de compras php

Tu codigo PHP mas bonito: (¡Utiliza highlight!)

Código PHP:
Ver original
  1. <?Php
  2. if ($_POST['cambio'] == "Modificar")
  3. {
  4. $p = 0;
  5. $g = 0;
  6. $ver = file($_SESSION['cliente']);
  7. $m = count($ver);
  8. $c = $_POST['cantidad'];
  9. $valor = $_POST['n'];
  10. while ($p < $m)
  11. {
  12. $sep = explode(";", $ver[$p]);
  13. if ($c[$p] <> 0)
  14. {
  15. $pedido[$g] = $sep[0] . ";" . $sep[1] . ";" . $sep[2] . ";" . $sep[3] . ";" . $sep[4] . ";" . $c[$p];
  16. $g = $g + 1;
  17. }
  18. $p = $p + 1;
  19. }
  20.  
  21. $h = 0;
  22. unlink($_SESSION['cliente']);
  23. $fp = fopen($_SESSION['cliente'], "a");
  24. while ($h < $g)
  25. {
  26. fputs($fp, $pedido[$h]);
  27. fputs($fp, chr(13) . chr(10));
  28. $h = $h + 1;
  29. }
  30. fclose($fp);
  31. }
  32.  
  33.  
  34. $servidor = "localhost";
  35. $usuario_bd = "root";
  36. $password_bd = "admin";
  37. $basedatos = "carrete";
  38.  
  39. $conexion = mysql_connect($servidor, $usuario_bd, $password_bd);
  40. if (!$conexion)
  41. {
  42. echo "Error conectando a la base de datos.";
  43. exit();
  44. }
  45.  
  46. $resultado = mysql_select_db($basedatos, $conexion);
  47. if (!$resultado)
  48. {
  49. echo "Error seleccionando la base de datos.";
  50. exit();
  51. }
  52. $dato = $_POST['nro'];
  53. if ($dato[0] <> "")
  54. {
  55. $sql = "select * from libros where Nro_catalogo='$dato[0]'";
  56. $res = mysql_query($sql);
  57. $archivo = $_SESSION['cliente'];
  58. $fp = fopen($archivo, "a");
  59. while ($row = mysql_fetch_array($res))
  60. {
  61. $nro = $row['Nro_catalogo'];
  62. $titulo = $row['Titulo'];
  63. $autor = $row['Autor'];
  64. $genero = $row['Genero'];
  65. $precio = $row['Precio'];
  66. $cantidad = 1;
  67. }
  68.  
  69. $pedido = $nro . ";" . $titulo . ";" . $autor . ";" . $genero . ";" . $precio . ";" . $cantidad;
  70. if ($_SESSION['cliente']<>"")
  71. {
  72. fputs($fp, $pedido);
  73. fputs($fp, chr(13) . chr(10));
  74. fclose($fp);
  75. }
  76. }
  77.  
  78. if (file_exists($_SESSION['cliente']))
  79. {
  80. $ver = file($_SESSION['cliente']);
  81. $totales = 0;
  82. $j = 0;
  83. $m = count($ver);
  84.  
  85. while ($j < $m)
  86. {
  87. $sep = explode(";", $ver[$j]);
  88. $nr[$j] = $sep[0];
  89. $tit[$j] = $sep[1];
  90. $aut[$j] = $sep[2];
  91. $gen[$j] = $sep[3];
  92. $prec[$j] = $sep[4];
  93. $can[$j] = $sep[5];
  94. $totales = $totales + $prec[$j] * $can[$j];
  95. $j = $j + 1;
  96.  
  97. }
  98. }
  99.  
  100. ?>