PHP - agregar item carrito de compras y aparezca arriba

 
Vista:
Imágen de perfil de ccessaro
Val: 52
Ha aumentado su posición en 4 puestos en PHP (en relación al último mes)
Gráfica de PHP

agregar item carrito de compras y aparezca arriba

Publicado por ccessaro (23 intervenciones) el 12/07/2017 18:18:47
Buenos días!
Cada dia aprendo más y me gusta pero también me encuentro con nuevos requerimientos y debo de aprender y seguir aprendiendo tengo un carrito de compras para pedidos de muchos articulos y lo que quiero es uqe cuando agregue un producto este me aparezca en la fila de arriba (se agrega en la ultima) yo lo quiero en la primera php y mysql.

este es el codigo (table) en el cual quiero ingresar los item arriba .. todo me funciona bien.. y estoy agregando nuevas cosas, muchas gracias por la colaboración.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<div class="table-responsive">
    <?php
		if (isset($_POST['id_txt'])){
			$id_articulo  = $_POST['id_txt'];
			$referencia   = $_POST['referencia'];
			$nombre       = $_POST['nombre'];
			$precio       = $_POST['precio'];
			$promo        = $_POST['promo'];
			$descuento    = $_POST['descuento'];
			$cuanto_hay   = $_POST['cuanto_hay'];
			$cantidad     = $_POST['cantidad'];
			$imagen       = $_POST['imagen'];
			$categoria    = $_POST['categoria'];
			$mi_carrito[]=array('id_articulo'=>$id_articulo,'referencia'=>$referencia,'nombre'=>$nombre,'precio'=>$precio,'promo'=>$promo,'descuento'=>$descuento,'cuanto_hay'=>$cuanto_hay,'cantidad'=>$cantidad,'imagen'=>$imagen,'categoria'=>$categoria);
			//print_r($mi_carrito);
		}
 
 
 
		if (isset($_SESSION['carrito'])){
				$mi_carrito=$_SESSION['carrito'];
					if (isset($_POST['id_txt'])){
						$id_articulo = $_POST['id_txt'];
						$referencia  = $_POST['referencia'];
						$nombre      = $_POST['nombre'];
						$precio      = $_POST['precio'];
						$promo       = $_POST['promo'];
						$descuento   = $_POST['descuento'];
						$cuanto_hay  = $_POST['cuanto_hay'];
						$cantidad    = $_POST['cantidad'];
						$imagen      = $_POST['imagen'];
						$categoria   = $_POST['categoria'];
						$descripcion = $_POST['descripcion'];
						$pos=-1;
						for($i=0;$i<count($mi_carrito);$i++){
							if($id_articulo==$mi_carrito[$i]['id_articulo']){
								$pos=$i;
							}
						}
						if($pos<>-1){
							$cuanto=$mi_carrito[$pos]['cantidad']+1;
							$mi_carrito[$pos]=array('id_articulo'=>$id_articulo,'referencia'=>$referencia,'nombre'=>$nombre,'precio'=>$precio,'promo'=>$promo,'descuento'=>$descuento,'cuanto_hay'=>$cuanto_hay,'cantidad'=>$cuanto,'imagen'=>$imagen,'categoria'=>$categoria,'descripcion'=>$descripcion);
						}else{
							$mi_carrito[]=array('id_articulo'=>$id_articulo,'referencia'=>$referencia,'nombre'=>$nombre,'precio'=>$precio,'promo'=>$promo,'descuento'=>$descuento,'cuanto_hay'=>$cuanto_hay,'cantidad'=>$cantidad,'imagen'=>$imagen,'categoria'=>$categoria,'descripcion'=>$descripcion);
							}
			}
		}
		//$cantidad2 = $cantidad < $cuanto_hay;
		echo " $_POST[cuanto_hay] ";
		if (isset($_POST['id_articulo2'])){
			$indice=$_POST['id_articulo2'];
			$existen=$_POST['existencia'];
			$cuanto=$_POST['cantidad2'];
			$mi_carrito[$indice]['cantidad']=$cuanto;
		}
 
		if (isset($_POST['id_articulo3'])){
			$indice=$_POST['id_articulo3'];
			$mi_carrito[$indice]=NULL;
		}
 
		if (isset($mi_carrito)) $_SESSION['carrito']=$mi_carrito;
 
 
	?>
      <div class="bs" data-example-id="contextual-table" style="border: 0">
        <table class="table">
          <thead>
            <tr>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>
                 <form id="form1" name="form1" method="post" action="confirmar.php">
                   <input class="add-to item_add hvr-skew-backward" type="submit" name="confirmarPedido" id="confirmarPedido" value="Confirmar Pedido" />
                 </form>
              </th>
              <th>
                 <div class="produced">
                    <a href="product.php" class="add-to item_add hvr-skew-backward">Seguir Comprando</a>
                 </div>
              </th>
              <th>&nbsp;</th>
            </tr>
          </thead>
          <tbody>
          </tbody>
        </table>
      </div>
 
<div class="bs" data-example-id="contextual-table" style="border: 1px solid #eee">
        <table width="616" class="table">
          <thead>
            <tr>
              <th>&nbsp;</th>
              <th>Nombre</th>
              <th>Referencia</th>
              <th>Existencias</th>
              <th>Precio</th>
              <th>Cantidad</th>
              <th>Subtotal</th>
              <th>&nbsp;</th>
              <th>Eliminar</th>
              <th>Observaciones</th>
            </tr>
          </thead>
          <tbody>
			<?php
 
              if (isset($mi_carrito)){
                $total=0;
                for ($i=0;$i<count($mi_carrito);$i++){
                    if($mi_carrito[$i]<>NULL)
                    {
           ?>
            <tr class="active">
              <th scope="row"><span class="ring-in"><img src="http://localhost/productos/<?php echo $mi_carrito[$i]['imagen'] ?>.jpg" width="55" height="60" alt=""></span></th>
              <td height="30%"><?php echo $mi_carrito[$i]['nombre'] ?></td>
              <td><strong><?php echo $mi_carrito[$i]['referencia'] ?></strong></td>
              <td align="center" class="warning"><h9><?php echo $mi_carrito[$i]['cuanto_hay'] ?> Unidades</h9></td>
              <td>$<?php echo number_format($mi_carrito[$i]['precio']);  ?></td>
              <td align="center">
                <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="actualizo">
                    <input name="id_articulo2" type="hidden" value="<?php echo $i ?>" />
                    <input name="existencia" id="<?php echo 'existencia_'.$i ?>" type="hidden" value="<?php echo $mi_carrito[$i]['cuanto_hay'] ?>" />
                    <input name="cantidad2" id="<?php echo 'cantidad2_'.$i ?>" type="text" value="<?php echo $mi_carrito[$i]['cantidad'] ?>" size="4" maxlength="4" />
                    <input name="" type="image" src="iconos/actualizar.png" onClick="buscar(<?php echo $i ?>)"/>
                </form>              </td>
				<?php
                    $subtotal=$mi_carrito[$i]['precio']*$mi_carrito[$i]['cantidad'];
                    $total=$total+$subtotal;
                ?>
              <td align="center" bgcolor="#FF0000"><h6>$ <?php echo number_format($subtotal); ?> </h6></td>
              <td align="center"></td>
              <td align="center">
               <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
                    <input name="id_articulo3" type="hidden" value="<?php echo $i ?>" />
                    <input name="" type="image" src="iconos/close.png" />
               </form>
              </td>
              <td align="center">
               <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
                    <input name="cantidad2" id="descripcion" type="text" value="<?php echo $mi_carrito[$i]['descripcion'] ?>" size="30" maxlength="30" />
               </form>
				</td>
            </tr>
			  <?php
                        }
                  }
              }
              ?>
            <tr>
              <th scope="row">&nbsp;</th>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr class="success">
              <th scope="row">&nbsp;</th>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td align="right">Total:</td>
              <td>$ <?php if (isset($total)) echo number_format($total); ?></td>
              <td>&nbsp;</td>
              <td>
              	<div class="produced">
                    <a href="product.php" class="hvr-skew-backward">Seguir Comprando</a>
                </div>
              </td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <th scope="row">&nbsp;</th>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>
                <form id="form1" name="form1" method="post" action="confirmar.php">
                  <input class="add-to item_add hvr-skew-backward" type="submit" name="confirmarPedido" id="confirmarPedido" value="Confirmar Pedido" />
                </form>
              </td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr class="warning">
              <th scope="row">&nbsp;</th>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </tbody>
        </table>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Stalin

agregar item carrito de compras y aparezca arriba

Publicado por Stalin (3 intervenciones) el 14/07/2017 00:53:42
Hola, y si el for lo recorres asi

for ($i=count($mi_carrito);$i==0;$i--){


esto es para recorrer tu arreglo desde la última posición

Si necesitas más ayuda puedes escribirme a mi correo.

Espero te sirva.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de ccessaro
Val: 52
Ha aumentado su posición en 4 puestos en PHP (en relación al último mes)
Gráfica de PHP

agregar item carrito de compras y aparezca arriba

Publicado por ccessaro (23 intervenciones) el 19/07/2017 16:08:26
Muchas gracias!
Me parece interesante la respuesta intente implementarla y no pude y no veo como enviarte un correo el mio es [email protected] te agradeceria tu ayuda,
Gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar