0

Muy buenos dias estoy haciendo una tienda virtual con php sin bases de datos y tengo un problema. Como ven tengo una tabla con los productos. Al no haber base de datos he de almacenar estos datos en un fichero como array y no se como hacerlo. Adjunto código. Tambien utilizo sesiones en este caso estoy dentro de la sesion ya osea que ese aspecto no importa. Gracias

<?php session_start(); ?>
    <HTML>

    <HEAD>
        <meta content="text/html; charset=UTF-8" http-equiv="content-type">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta name="robots" content="noindex, nofollow">
        <meta name="googlebot" content="noindex, nofollow">
        <link rel="stylesheet" type="text/css" href="/css/result-light.css">
        <style type="text/css">
        </style>
        <TITLE>Accés a la sessió com a javi</TITLE>
    </HEAD>
    <BODY>
        <?php
	//session_start();
	$valor = $_SESSION['acces'];
	if ($_SESSION['acces']!="2"){
		//echo "No s'ha iniciat correctament aquesta sessió";
		echo "No heu accedit correctament";
		print '<META HTTP-EQUIV="refresh" CONTENT="2;URL=./index.html">';
	}else{
		//echo "Valor de la sessió : $valor<BR>\n";
		//echo "Benvingut a la pàgina privada<BR><BR>\n";
		//echo "<a href=surt.php>Surt de la sessió</a>\n";
	?>
            <h1 style="text-align:center">Tienda virtual de XiaomisiOsi</h1>
            <html>

            <head>
                <meta content="text/html; charset=UTF-8" http-equiv="content-type">
                <title>Tienda virtual de XiaomiManda</title>
            </head>

            <body>
                <br>
                <form method='POST' action='r03c.php'>
                    <table border="1" align="center">
                        <tr align="center">
                            <td>Imagen del producto</td>
                            <td>Nombre</td>
                            <td>Precio</td>
                            <td>Unidades totales</td>
                            <td>Comprar unidad</td>
                        </tr>
                        <tr align="center">
                            <td><img src="imatges/xiaomi.jpg" width="300" style="300" alt=""></td>
                            <td>Xiaomi</td>
                            <td>100</td>
                            <td>10</td>
                            <td><input type="checkbox" name="mobil1" value="mobil1" />Si<br /></td>                       
                        <tr align="center">
                            <td><img src="imatges/xiaomimi8.jpg" width="300" style="300" alt=""></td>
                            <td>Xiaomimi8</td>
                            <td>200</td>
                            <td>10</td>
                            <td><input type="checkbox" name="mobil2" value="mobil2" />Si<br /></td>   
                        </tr>
                        <tr align="center">
                            <td><img src="imatges/xiaomimix2.jpg" width="300" style="300" alt=""></td>
                            <td>Xiaomimix2</td>
                            <td>300</td>
                            <td>10</td>
                            <td><input type="checkbox" name="mobil3" value="mobil3" />Si<br /></td>   
                            <tr>
                    </table>
                    <input style="margin-left:650px" type="submit" name="submit" value="Endavant" />
                </form>
            </body>
            </html>
            <a style="float:right" href="carrito.php" width="300" style="300" alt="">Acceso carrito</a>
            <BR>

            <BR>
            <BR><a style="float:right" href=surt.php>Surt de la sessió</a>
            <?php
	        }
	        ?>
    </BODY>
    </HTML>

1
  • Si solo tienes productos, una linea por producto y separados los campos por el caracter que prefieras.
    – JDev
    el 29 nov. 2018 a las 11:11

1 respuesta 1

1

Podrias usar las funciones json_encode() y json_decode() para trabajar con textos.

Quedaria algo así:

function guardar_productos($productos){
    $txt = json_encode($productos);    
    $file = fopen('productos.txt', 'w');    
    fwrite($file, $txt);
    fclose($file);    
}

function cargar_productos(){
    $txt = file_get_contents('productos.txt');
    $productos = json_decode($txt);
    return $productos;
}

y llamas a la función correspondiente cuando sea necesario.

el fichero tendría ese aspecto:

{
    "productos": [{
            "id": "1",
            "nombre": "cosa 1",
            "color": "rojo",
            "precio": "10"
        },
        {
            "id": "2",
            "nombre": "cosa 2",
            "color": "azul",
            "precio": "55.7"
        },
        {
            "id": "3",
            "nombre": "cosa 3",
            "color": "",
            "precio": "25"
        }
    ]
}

Lo puedes validar con alguna herramienta online como https://jsonlint.com/

4
  • Pero esto en otro archivo aparte?
    – Javichu
    el 29 nov. 2018 a las 11:39
  • Es indiferente, son funciones php, las puedes añadir al fichero actual o incluirlas desde un archivo aparte. php.net/manual/es/function.require.php
    – Fly
    el 29 nov. 2018 a las 11:41
  • Entonces el fichero txt. como lo preparo en mi caso, del palo la imagen, el nombre, el precio y las unidads totales de cada producto ?
    – Javichu
    el 29 nov. 2018 a las 11:43
  • Edito la respuesta y te pego un ejemplo.
    – Fly
    el 29 nov. 2018 a las 11:52

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.