Store products that appear in a php file in a .scv or a .txt separated by commas

0

Good morning, I am doing a virtual store with php without databases and I have a problem. As you can see I have a table with the products. Since there is no database, I have to store this data in a file as an array and I do not know how to do it. I attach code. I also use sessions in this case I'm in session and I mean that aspect does not matter. Thanks

<?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>
    
asked by Javichu 29.11.2018 в 11:47
source

1 answer

1

You could use the functions json_encode () and json_decode () to work with texts.

It would be something like this:

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;
}

and call the corresponding function when necessary.

the file would look like this:

{
    "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"
        }
    ]
}

You can validate it with an online tool such as link

    
answered by 29.11.2018 в 12:31