How do I print the conditions results inside the php index?

Asked

Viewed 26 times

-1

I am doing so and when it is to print the result(After the user selects the Submit in another index), nothing appears. It is empty. WHERE IT RECEIVES DATA VIA POST FROM THE RADIO TYPE FORM AND SETS OUT THE CONDITIONS:

<?php
            // Recebendo as respostas do Usuário
            $perfil = $_POST["pergunta1"];
            $condicao = $_POST["pergunta2"];
            //Iniciando as variáveis
            $cpu = "cpu";
            $rom = "rom";
            $ram = "ram";
            


            // Primeiro caso: Perfil de uso - Dia a dia
            if ($perfil == "diadia" && $condicao == "economico") {
                $cpu = "Intel Celeron";
                $rom = "HDD 300GB";
                $ram = "2GB DDR3";

            } elseif ($perfil == "diaadia" && $condicao == "normal") {
                $cpu = "Intel Core i3";
                $rom = "HDD 500GB";
                $ram = "4GB DDR3";

            }
            // Segundo caso: Perfil de uso - Trabalho
            elseif ($perfil == "trabalho" && $condicao == "economico") {
                $cpu = "Intel Core i3";
                $rom = "HDD 500GB";
                $ram = "4GB DDR3";
            
            } elseif ($perfil == "trabalho" && $condicao == "normal") {
                $cpu = "Intel Core i5";
                $rom = "HDD 1TB";
                $ram = "8GB DDR3";  
                $video = "2GB";
            }
            // Terceiro caso: Perfil de uso - Edição
            elseif ($perfil == "edicao" && $condicao == "economico") {
                $cpu = "Intel Core i5";
                $rom = "HDD 1TB";
                $ram = "8GB DDR3";
                $video = "2GB";
            } elseif ($perfil == "edicao" && $condicao == "normal") {
                $cpu = "Intel Core i7";
                $rom = "HDD 1TB";
                $ram = "12GB DDR3";
                $video = "4GB";
            };?>

WHERE THE USER WILL BE SHOWN THE SETTINGS ACCORDING TO THE CONDITIONS:

 <section class="page-section bg-primary text-white mb-0">
    <div class="container">
        <div class="divider-custom divider-light">
            <div class="divider-custom-line"></div>
            <div class="divider-custom-icon"><i class="fas fa-star"></i></div>
            <div class="divider-custom-line"></div>
        </div>
        <!-- About Section Content-->
        <div class="row">
             <?php
                  
            
            echo "Processador: $cpu " . "<br>";
            echo "Armazenamento: $rom " . "<br>";
            echo "Memoria RAM: $ram "  . "<br>";
            if ($perfil = "edicao" && $condicao = "economico"){
                echo "Placa de video dedicada: $video" . "<br>";

            } elseif ($perfil = "edicao" && $condicao = "normal"){
                echo "Placa de video dedicada: $video " . "<br>";
            };
             
            
            
            ?>

            
        </div>
    </div>
</section>

1 answer

0


Hello,

Make sure the code below answers. Note that ifs have been drastically reduced, and that if you want to add new profiles and conditions, just extend the multidimensional array.


//escolhendo computador conforme opções escolhidas
$computadores['diaadia']['economico']['cpu'] =  "Intel Celeron";
$computadores['diaadia']['economico']['rom'] =  "HDD 300GB";
$computadores['diaadia']['economico']['ram'] =  "2GB DDR3";

$computadores['diaadia']['normal']['cpu'] =  "Intel Core i3";
$computadores['diaadia']['normal']['rom'] =  "HDD 500GB";
$computadores['diaadia']['normal']['ram'] =  "4GB DDR3";

$computadores['trabalho']['economico']['cpu'] =  "Intel Core i3";
$computadores['trabalho']['economico']['rom'] =  "HDD 500GB";
$computadores['trabalho']['economico']['ram'] =  "4GB DDR3";

$computadores['trabalho']['normal']['cpu'] =  "Intel Core i5";
$computadores['trabalho']['normal']['rom'] =  "HDD 1TB";
$computadores['trabalho']['normal']['ram'] =  "8GB DDR3";
$computadores['trabalho']['normal']['video'] =  "2GB";

$computadores['edicao']['economico']['cpu'] =  "Intel Core i5";
$computadores['edicao']['economico']['rom'] =  "HDD 1TB";
$computadores['edicao']['economico']['ram'] =  "8GB DDR3";
$computadores['edicao']['economico']['video'] =  "2GB";

$computadores['edicao']['normal']['cpu'] =  "Intel Core i7";
$computadores['edicao']['normal']['rom'] =  "HDD 1TB";
$computadores['edicao']['normal']['ram'] =  "12GB DDR3";
$computadores['edicao']['normal']['video'] =  "4GB";


$cpu = $computadores[$perfil][$condicao]['cpu'];
$rom = $computadores[$perfil][$condicao]['ram'];
$ram = $computadores[$perfil][$condicao]['rom'];
if(isset($computadores[$perfil][$condicao]['video'])){
    $video = $computadores[$perfil][$condicao]['video'];
}

And the part where the data is displayed:

   echo "Processador: $cpu " . "<br>";
   echo "Armazenamento: $rom " . "<br>";
   echo "Memoria RAM: $ram "  . "<br>";
   if (isset($video)){
       echo "Placa de video dedicada: $video" . "<br>";
   } 
  • Dude, you got a problem, pressing Ubmit, you get 500

  • In this case, you need to see the Apache logs to identify the cause.

  • Fatal error: Cannot use Temporary Expression in write context in /home/defaultwebsite/public/descubraseupc/form/index.php on line 33

  • Line 33: $computers = ['diaadia']['economico']['cpu'] = "Intel Celeron";

  • There was an error in the assignments, but I already corrected the answer. You can check, but essentially, it was assigning wrong

  • /home/defaultwebsite/public/descubraseupc/form/index.php on line 63 Notice: Undefined index: in /home/defaultwebsite/public/descubraseupc/form/index.php on line 63 Notice: Undefined variable: condicao in /home/defaultwebsite/public/descubraseupc/form/index.php on line 63 Notice: Undefined variable: perfil in /home/defaultwebsite/public/descubraseupc/form/index.php on line 64 Notice: Undefined variable: condicao in /home/defaultwebsite/public/descubraseupc/form/index.php on line 64

  • Line 63: $ram = $computers[$profile][$condition]['ram']; Line 64: if(isset($computers[$profile][$condition]['video'])){

  • Remembering that I made a change 62 and 63, because it was the other way around, where it was supposed to be "ram" was "rom"

  • This is because the value of $profile and $condition did not come.Make sure the data is coming correctly from the form.

  • Ok, solved. However, one of the initial problems continues. It did not print the results of the variables, the other strings yes, as "Processor: "

  • It is working here: http://sandbox.onlinephpfunctions.com/code/ce1554e10c213deac04c5268abcb7e091159d1528 Please check carefully the rest of your code... And if it helped, please mark the answer.

  • 1

    Okay, thanks for your help!

  • Dispose. We are here to help.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.