Doubt about logica php

Asked

Viewed 53 times

0

Do you guys talk? I’m here to ask for help because I’m a little tied up, I’m making an example for the faculty of questions for covid, I’m looking for a way to take the questions that were marked in the checkbox and do the logica with php, only I’m not sure how to proceed. I want to take the answers and put a percentage on them to pass the user’s real chance to have covid.

the index and the part of the calculation I will send now.

<!DOCTYPE html>
<html lang="pt-br">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>Home - Trabalho Acadêmico</title>

  <!-- Links Site -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <link rel="icon" type="image/png" href="https://ufersa.edu.br/wp-content/themes/temaufersa/img/favicon.png" />
  <link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@1,300&display=swap" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="assets/style.css">
  <link rel="stylesheet" type="text/css" href="assets/style.scss">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Fira+Sans" rel="stylesheet">
  <!-- Fim Links -->

</head>

<body>

  <!-- Topo -->
  <header>
    <div class="topo">
      <div class="logo">
        <a href="index.php"><img src="img/logo.png" alt=""></a>
      </div>
      <nav class="menu">
        <ul>
          <li><a href="index.php">Home</a></li>
          <li><a href="sobre.php">Sobre o Projeto</a></li>
          <li><a href="sobre_nos.php">Sobre Nós</a></li>
        </ul>
    </div>
    </div>
  </header>
  <!-- Fim topo -->

  <!-- Slider -->

  <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
      <div class="carousel-item active">
        <img class="d-block w-100" height="396px" src="https://www.slmandic.edu.br/wp-content/uploads/2020/03/BANNER-SITE-CORONAVIRUS.png" alt="First slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100" height="396px" src="https://www4.infraero.gov.br/media/802252/banner_santafe_v01.png" alt="Second slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100" src="https://www4.infraero.gov.br/media/802252/banner_santafe_v01.png" alt="Third slide">
      </div>
    </div>
    <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>

  <!-- Fim Slider -->

  <!-- Info Covid19 -->
  <section class="jumbotron text-center">
    <div class="container">
      <h1 class="jumbotron-heading">O QUE É CORONAVÍRUS? (COVID-19)</h1>
      <p class="lead text-muted">Coronavírus é uma família de vírus que causa infecções respiratórias, cujos primeiros casos foram isolados pela primeira vez em 1937, porém, apenas em 1965 foi descrito como coronavírus, em decorrência do aspecto apresentado a microscopia, similar a uma coroa.
        O novo agente do coronavírus (SARS-CoV-2) foi descoberto em 31 de dezembro de 2019 após casos registrados na China, e a doença, por determinação da Organização Mundial da Saúde (OMS), foi denominada de COVID19. Em 11 de março de 2020 foi decretada como pandemia pela OMS.</p>
      <p>
        <a href="sobre.php" class="btn btn-primary my-2">Saiba Mais</a>
      </p>
    </div>
  </section>

  <!-- Fim Info Covid19 -->

  <!-- Questionario Covid -->
  <div class="container">
    <div class="row featurette">
      <div class="col-md-7">
        <h2 class="featurette-heading">Realize o teste do novo <span class="text-muted"> NOVO CORONAVIRUS.</span></h2>
        <p class="lead">A sua jornada começa com um questionário de sintomas do novo coronavírus. As respostas são associadas às suas condições prévias de saúde, como doenças cardíacas e respiratórias e geram o resultado da triagem, é só clicar nos sintomas que está sentindo logo abaixo e enviar, logo será dado o resultado.</p>
      </div>
      <div class="col-md-5">
        <img src="https://testedocorona.com.br/assets/logo-teste-do-corona.webp" height="120px" alt="">
      </div>
    </div>
  </div>

  <hr class="featurette-divider">

        <div class="container">
        <form action="calculo.php" method="post" name="questionario">

        <input id="toggle1" name="pergunta1" value="1" type="checkbox">
        <label for="toggle1">Teve contato com alguém sintomático nos últimos 30 dias ?</label>

        <input id="toggle2" name="pergunta2" value="2" type="checkbox">
        <label for="toggle2">Você teve problemas para respirar recentemente (respiração curta) ?</label>

        <input id="toggle3" name="pergunta3" value="3" type="checkbox">
        <label for="toggle3">Seus olhos tem coçado nos últimos dias ?</label>

        <input id="toggle4" name="pergunta4" value="4" type="checkbox">
        <label for="toggle4">Você está com coriza (nariz escorrendo) ?</label>

        <input id="toggle5" name="pergunta5" value="5" type="checkbox">
        <label for="toggle5">Teve frebre nos ultimos dias?</label>

        <input id="toggle6" name="pergunta6" value="6" type="checkbox">
        <label for="toggle6">Tem tossido muito ultimamente ?</label>

        <input id="toggle7" name="pergunta7" value="7" type="checkbox">
        <label for="toggle7">Você está com dor de garganta?</label>

        <input id="toggle8" name="pergunta8" value="8" type="checkbox">
        <label for="toggle8">Você teve diarréia nos últimos 10 dias?</label>

        <button name="enviar " type="submit" value="Enviar" class="btn btn-primary btn-lg btn-block">Resultado</button>

  </form>
  </div>
  <!-- Fim Questionario Covid -->

  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

  <footer>
    <div class="logo_rodape">
      <img src="https://ufersa.edu.br/wp-content/themes/temaufersa/img/favicon.png" alt="">
      <a href="index.php">PROJETO BSI UFERSA - ANGICOS | <?= date('M, Y') ?></a>
    </div>
  </footer>

</body>

</html>

--------------------------------------------------- calculo.php

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Home -  Trabalho Acadêmico</title>

      <!-- Links Site -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <link rel="icon" type="image/png" href="https://ufersa.edu.br/wp-content/themes/temaufersa/img/favicon.png" />
  <link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@1,300&display=swap" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="assets/style.css">
  <link rel="stylesheet" type="text/css" href="assets/style.scss">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Fira+Sans" rel="stylesheet">
  <!-- Fim Links -->

</head>
<body>


<header>
    <div class="topo">
        <div class="logo">
            <a href="index.php"><img src="img/logo.png" alt=""></a>
        </div>
             <nav class="menu">
                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="sobre.php">Sobre o Projeto</a></li>
                    <li><a href="sobre_nos.php">Sobre Nós</a></li>
                </ul>
            </div>
    </div>
</header>


<?php
 
$pergunta1 = $_POST["pergunta1"];
$pergunta2 = $_POST["pergunta2"];
$pergunta3 = $_POST["pergunta3"];

 

$resposta1 = "1";
$resposta2 = "2";
$resposta3 = "3";
$resposta4 = "4";
$resposta5 = "5";
$resposta6 = "6";
$resposta7 = "7";
$resposta8 = "8";

 
$acertou = 0;
$errou = 0;
 
if ($pergunta1 === $resposta1)
{
    $acertou++;
    echo "<font color = blue> Questao 1 correta<br></font>";
}
else
{
    $errou++;
    echo "<font color = red> Questao 1 errada<br></font>";
}
 
if ($pergunta2 == $resposta2)
{
    $acertou++;
    echo "<font color = blue> Questao 2 correta<br></font>";
}
else
{
    $errou++;
    echo "<font color = red> Questao 2 errada<br></font>";
}
 
if ($pergunta3 == $resposta3)
{
    $acertou++;
    echo "<font color = blue> Questao 3 correta<br></font>";
}
else
{
    $errou++;
    echo "<font color = red> Questao 3 errada<br></font>";
}
 

 
$calc = 10;
$porcent = $calc * $acertou;
 
echo "<br><br> Voce <font color = blue>acertou</font> $acertou e <font color = red>errou</font> $errou.";
 
 
if ($acertou >=7)
{
echo "<br><br><b><blink>Muito bem, um total de $porcent % de acerto</blink></b>";
}
else
{
echo "<br><br><b><blink> Voce nao foi muito bem, um total de $porcent% de acerto, tente novamente!</blink></b>";
}
 
?>

<div class="alert alert-primary" role="alert">
  <?php 
  
    echo "Muito bem um total de $porcent%";
    
  ?>
</div>


<br /><input  type="button" value="voltar" id="botao_voltar" onclick="history.go(-1)">

<footer>
    <div class="logo_rodape">
        <img src="https://ufersa.edu.br/wp-content/themes/temaufersa/img/favicon.png" alt="">
    </div>
    <a href="index.php">PROJETO BSI UFERSA - ANGICOS | <?= date('M, Y') ?></a>
</footer>
    
</body>
</html>

I am giving a studied in the part of the calculation, I am using bootstrap for design, if you can give me a light how to proceed thank you

  • Only one detail. When a checkbox is checked, it returns "on" as value, if not, it does not exist

  • thank you very much.

  • Not necessarily it will return 'on', it depends on the method applied by the developer.

  • @Mike565 Complementing my answer, an alternative to the checkbox question does not return anything when it is unchecked is you work with database, this is how most do, creates a table, puts in the column the default value "no" and then if the user marks, you replace the value Only on the result page. Create an if, if you have no return, you consider the value of the table, if you have return, you consider as yes, in case you would use two php files, 1 sends the data via Post and the other validates and shows the result, it is cleaner so, use Windows! it’ll be easier for you.

2 answers

1

In the words of the mozila

Note : If a checkbox is unchecked when your form is sent, there will be no value sent to the server to represent its unchecked state (for example value=unchecked); the value is not submitted to the server.

Do not use value for this, use the input id to identify them, remembering that the input name should be different from the others. First recover the value:

<input id="toggle1" name="pergunta1" value="<?php echo $resp1 = 1; ?>" type="checkbox" required>

If it is marked returns 1, if it is not marked returns nothing, as documented by mozila: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox | in this case we require the user to select the checkbox.

that is, in your case you will have to create another chackbox or, you can use javascript to know whether or not it is marked and convert this into a value, but let’s consider that it is marked, since the attribute required obliges the user to mark it.

Knowing this and considering that the value you want is in $resp1 (value="<?php echo $resp1 = 1; ?>"). Make an if:

$resp1 = '1';
$result = ($resp1) ?: '0'; // $result retornará 1 **se**, o valor de $resp1 for igual a 1

$resp1 = '';
echo ($resp1) ?: '0'; // se $resp1 tem valor diferente de '1', ele retorna 0.

Based on this you can start building the rest of the calculation function once you know that 0 is lie and 1 is true.

This is an old way of writing if, but it is compatible with versions prior to php 7.0, if an error occurs, just remove the quotes.

For php 7 and higher:

if(1 == $resp1){
  echo "1"; //se resp1 é igual a 1, retorna 1.
} elseif (on == $resp) {
    echo "1"; //se $resp1 é igual a on, retorna 1.
} else {
    echo "0"; //se $resp1 tem valor diferente de 1 e de on, retorna 0.
}

Always use the value you want to check in front and put two signs equally this way ==, if you put only one, it will not check anything and will still change the value for which you are trying to check. (see: https://www.php.net/manual/en/control-structures.if.php)

0

You can do a function that picks up all inputs, then traverse it with an iterator checking which one is checked, when checked you increase one in your counter. Then you divide the counter value by the amount of symptoms and multiply by 100 to have a percentage of symptoms. Counter/totalDeInputs*100. So you can tell what percentage of symptoms to worry about.

Browser other questions tagged

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