Doubt with an IF ELSE

0

I'm making a cart for the web, and there's one thing I have to compare when I add a product to the cart. If the product that I add to the cart is for rent = id1 I will show a div and if the product that I add is any other one that has another id then I show another div.

Ok, I've differentiated it in PHP:

 if($mitipo == 1){
   echo "<div class=\"row justify-content-between\">
   <div class=\"col detaCart\">Kit iniciación</div>
   <div class=\"col-3 text-right detaCart\">99€</div>
   </div>
     <div class=\"txtGafas\">Gafas técnica, gafas paciente, garrafa de Gel conductor, caja de cuchillas</div>
     <div class=\"row justify-content-between\">
     <div class=\"col detaCart\"><small class=\"smallKit\">¿Incluir kit?</small></div>
     <div class=\"col-3 text-right detaCart ui form\">
     <div class=\"inline field\" style=\"vertical-align: -webkit-baseline-middle;\">
     <div class=\"ui checkbox posCheck\">
        <input type=\"checkbox\" tabindex=\"0\" class=\"hidden\">
         </div>
        </div>
       </div>
      </div>";

      }else{
      echo "<div class=\"row justify-content-between\">
      <div class=\"col detaCart\">Kit iniciación</div>
      <div class=\"col-3 text-right detaCart\">Incluido</div>
     </div>
     <div class=\"txtGafas\">Gafas técnica, gafas paciente, garrafa de Gel conductor, caja de cuchillas</div>";
     }

What I do with that is to tell you if the product that is in the variable cart has the ID: 1 show me this div if it is any other show me the other, but of course if I add any one and then one with ID: 1 makes correct and changes the div of the cart, but if I do the reverse, ejp; I add one with Id: 1 and then another one takes away that div and I put the other one. How can I always tell you that there is a product with the Id: 1 that div always comes out ??? Thanks

    
asked by Miguel 10.10.2018 в 10:30
source

2 answers

0

In the end I got it, the thing was not misguided but you had to take out the HTML for.

 if(isset($mi_carrito) && count($mi_carrito) != 0){
  // Por defecto establecemos que no debe incluir kit.
  $puedeIncluirKit = false;
  $cantidadProductosParaKit = 0;
  for ($i=0; $i<count($mi_carrito); $i++) {
  // Con almenos 1 objeto del carrito que sea tipo = 1, indicamos que puede incluir kit. y frenamos el ciclo.
     if ($mi_carrito[$i]['Tipo'] == 1) {
     $puedeIncluirKit = true;
     $cantidadProductosParaKit ++;

      }
    }
    // Si existe la variable en true, mostrarmos el div de incluir kit.
    if ($puedeIncluirKit) {
    $div= "Contenido si es que si";
    }else{
    $div= "Contenido si es que no";
    }
    echo $div.""; 
    }
    
answered by 10.10.2018 / 12:13
source
0

Example by adding an external variable to the route of the products of the cart that changes at the moment that one of the products is for rent.

$tipos = array(1,2); //array con los tipos de productos

//tu caso
for ($i=0; $i<count($tipos); $i++) {
    if ($tipos[$i]==1) {
        $div = "alquiler<br>";
    } else {
        $div = "otros";
    }
}
echo $div."<br>"; //otros

//con variable externa
$hay_alquiler=0;
for ($i=0; $i<count($tipos); $i++) {
    if ($tipos[$i]==1 || $hay_alquiler == 1) {
        $div = "alquiler<br>";
        $hay_alquiler = 1;
    } else {
        $div = "otros";
    }
}
echo $div."<br>"; //alquiler
    
answered by 10.10.2018 в 10:52