I can not create an empty variable

0

// I create the variable array session

session_start();
$fact = [0 => 0];
$_SESSION['facturacion'] = $fact;

// I pick it up in another file

include 'cn.php';
session_start();
$fact = $_SESSION['facturacion'];

$total = 0;
if(isset($_POST['e']) && isset($_POST['c']))
{
 $id = $_POST['e'];
 $cantidad = $_POST['c'];

 if(count($_SESSION['facturacion']) <= 8){
 for ($i=0; $i < count($_SESSION['facturacion']); $i++) { 
 if (!(in_array($id, $fact))) {
  $resultado1 = mysqli_query($link, "SELECT * FROM productos WHERE ID = 
  $id") or die();
  $fila1 = mysqli_fetch_array($resultado1);
  if ($fila1['ProductS'] >= $cantidad) {
    $fact[$id] = $cantidad;
    $_SESSION['facturacion'] = $fact;


   }

   }
   } 
 }


 }

 $precio = 1;

foreach ($fact as $key => $value) {
$resultado = mysqli_query($link, "SELECT * FROM productos WHERE ID = $key") 
or die();
$fila = mysqli_fetch_array($resultado);
$precio = $fila['ProductP'];
$total+=$value*$precio;

if ($key != 0) {
$Subtotal = $precio*$value;
$p = strpos($precio, ".");
if($p === false){$Subtotal = $Subtotal.".00";}else{$Subtotal = 
$Subtotal."0";}
$salida.= "
          <tr>
            <td>".$value."</td>
            <td>".$fila['ProductU']."</td>
            <td>".$fila['ProductN']."</td>
            <td>".$fila['ProductP']."</td>
            <td>".$Subtotal."<span aria-hidden='true'>&times;</span></td>
          </tr>";

  } 
 }
 echo $salida;
 mysqli_close($link);

I want to create the variable in the second file, but when I try it I can not, and I can not stop using [0 = > 0]; because I get an error, my question is As I create the array in the second file and save that array in $ _SESSION

    
asked by Andres Gaibor 01.04.2018 в 04:47
source

1 answer

0

With this ...

if (!(in_array($id, $fact))) {

... you are looking for $id in the array $fact , but it turns out that in $ fact what you are putting are quantities and the value $id is used as a key:

$fact[$id] = $cantidad;

The search for $id should be done on the $fact keys:

if (!(in_array($id, array_keys($fact)))) {

Or better:

if (!isset($fact[$id})) {
    
answered by 02.04.2018 в 21:30