Shopping cart type listing in PHP and mysqli

1

I am working on a list where the user will add items to the list shopping cart type, is what I can think of to explain it better, you can search and add articles and their details (key, description, quantity and cost ) but when I want to add another one, what it does is that it replaces the information in the previous article.

    <table border=7 ALIGN=CENTER width="100%" bgcolor=#ffffff>
<tr align=center>
    <td>
        <font size="3">CLAVE</font>
    </td> 
    <td>
        <font size="3">DESCRIPCION</font>
    </td> 
    <td>
        <font size="3">COSTO</font>
    </td>

<?php
if ($_SESSION[cont1]=="0"){
?>
    <td colspan="2">
        <font size="3">CANTIDAD</font>
    </td>

<?php
}else{
echo '2',$_SESSION[cont1];
?>
    <td>
        <font size="3">CANTIDAD</font>
    </td> 

    <td>
        <font size="3"></font>
    </td> 
<?php
}   
?>
</tr>                                                                          

<?php
echo '<br>Contador SESSION: ',$_SESSION[cont1];

if(isset($_POST['submit']))

{

$desc1 = htmlspecialchars($_POST["desc1"]);

$res1 = $mysqli->XXXXX

while ($registro = mysqli_fetch_assoc($res1)) {


$cont1=$cont1+1;
$_SESSION[cont1]=$_SESSION[cont1]+1;


$m=$_SESSION[cont1];

$r = array($registro['clv'],$registro['desc1'],$registro['cost1'],$registro['cant1']); 
session_start(); 
$_SESSION[$m] = $r;

echo "<br>Primer valor: ".$_SESSION[$m][0];
echo "<br>Segundo valor: ".$_SESSION[$m][1];  
echo "<br>Tercer valor: ".$_SESSION[$m][2];
echo "<br>Tercer valor: ".$_SESSION[$m][3];  

$total1=$total1+$registro['cost1'];


?>
<tr align=center>
    <td>
        <font size="3"><?php echo $registro['clv'];   ?></font>
    </td> 

    <td>
        <font size="3"><?php echo $registro['desc1'];  ?></font>
    </td> 

    <td>
        <font size="3">$<?php echo $registro['cost1'];  ?></font>
    </td> 

    <td>
        <font size="3"><?php echo $registro['cant1'];  ?></font>
    </td> 
</tr>







<?php
}

if ($total1=="") {


    Echo '<center><h1><br> No Existe información para mostrar </h1></center>';
    }else{







$promedio=$total1/$_SESSION[cont1];

}

    ?>
<tr align=center>
    <td colspan="5">
        <font size="3">Costo Total de la Herramienta: <br>$<?php echo $total1;  ?></font>
    </td> 
</tr>



<?php


if ($desc1==""){
$cont1=0;
echo '<br>Contador es cero: ',$cont1;
}else{
echo '<br>Descripcion: ',$desc1;
echo '<br>Contador diferente de cero: ',$cont1;
}


}


?>






<br><br>

<form method="post" name="desc1" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">


<TR ALIGN=CENTER> 
<TD colspan="5">
        <H3><label for="desc1">Seleccionar Herramienta para agregar: </label></H3>
<?php
        $result = $mysqli->query($query);

?>   
<select name="desc1">    
    <?php    
    while ( $registro = $res1->fetch_array() )    
    {   
        ?>  

        <option value="<?php echo $registro['desc1']; ?>" >
        <?php echo $registro['desc1']; ?>
        </option><br>

        <?php

    }  
    ?> 



</select>



<br><br>
<input type="submit" name="submit" value="Aceptar"><br>
<br><br>
</form>




<?php 

if(isset($_POST['submit2']))

{

$_SESSION[cont1] = "0"; 

}
?>

<TR ALIGN=CENTER> 
<form method="post" name="desc1" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<td ALIGN=CENTER>
<br><br>
<input type="submit" name="submit2" value="Aceptar"><br>
<br><br>
</form>
</td>
</TR> 


</table>

    
asked by Stravos77 18.07.2016 в 23:35
source

1 answer

0

You would have to create a table called, for example, 'article_to_carrier' where the following rows would be: id, id_article, user_id. In this way, each time an article is added, a new article is created in the database and all the information mentioned above is saved. Obviously, I absolutely recommend that when you make the display of the list of available articles, you add an input type hidden with a value equal to the id number of the article. Then, when you include it in the table mentioned at the beginning of this answer, you send it by ajax the value of the id_article to your php file that makes the requests. Inside this php you get the id_article and you get the $ _SESSION ["id], and finally you insert it (and in the case that you select" remove article from the cart "), another php will be opened that eliminates it instead of adding it. It is necessary to put the data of the article in the cart, because it would be duplicating the info that would already be stored where the id_article is equal to the id of the specific article in your article table.

I hope I could help you. If you need an example, let me know.

    
answered by 19.07.2016 в 01:28