How to create function to automate a dynamic progress bar?

Asked

Viewed 443 times

3

I have a form with some "modules" (module = a set of questions - I do not know if it is the appropriate technical term...), so depending on which module is chosen, a certain amount of questions will be inserted in the form to be answered.

And depending on how many modules have been included, the form will have a certain amount of "pages" (divs hidden, that will arise in place of the previous, as the user advances in the questions).

For example:

  • module 1 = 8 pages - 14 questions
  • module 2 = 3 pages - 7 questions
  • module 3 = 20 pages - 27 questions
  • ...

So, thinking for now just amount of pages, and imagining only one page for each module to start the attempt, I did something more or less like this:

<div class="col-md-10">
<?php

    if (!$modulo1 && !$modulo2 && $modulo3) {
        echo "  <div class='progress'>
    <div class='progress-bar progress-bar-info' role='progressbar' style='width:30%'>   30%
    </div>
</div>";
    }
    elseif (!$modulo1 && $modulo2 && $modulo3) {
        echo " <div class='progress'>
    <div class='progress-bar progress-bar-info' role='progressbar' style='width:20%'>   20%
    </div>
</div>
    ";
    }
    elseif ($modulo1 && $modulo2 && $modulo3) {
        echo " <div class='progress'>
    <div class='progress-bar progress-bar-info' role='progressbar' style='width:10%'>   10%
    </div>
</div>
    ";
    }
?>
</div>

The variables $modulo are boolean session variables, which indicate whether the module has been chosen previously (in a form that creates the question form, where the user chose which modules...):

if (isset($_POST['modulo1'])) {
    $modulo1 = $_POST['modulo1'];
    $_SESSION['modulo1'] = $modulo1;
} else {
    $modulo1 = false;
    $_SESSION['modulo1'] = false;
}

But as there are many progress bars (up to 30, depending on how many modules...), I think that this way will be enough to repeat code, bad to read, etc...

So the question is:

How best to create a function to automate a dynamic progress bar?

  • So I don’t understand @Guilhermenascimento. Do you mean if you update your browser? If it is not, it is without Ajax... or JS... after the user chooses the modules, these variables are saved $modulo, and from them I know what percentage should appear, then I started doing it the way up, half in the arm hehe...

  • Variables are temporary instances, once updated the page they disappear, or have the value modified.

  • @Guilhermenascimento It really is what it seems.

  • So @Guilhermenascimento and Edilson, I’m using session variables to store the states, so after rendering the page (tbm I’m not sure that’s the term, but I say at the time that PHP takes the HTML templates and assembles the form, I use these variables in the templates to know how many pages... is the same way I use to change the links of the "Continue" buttons to change the Ivs... but I wanted to do it in a simpler way... I don’t know if I could understand, anything of a touch that edits the question... is that I thought it would be too long if I added all ...

  • necessary code to understand how the entire system works... The variables are like this: if (isset($_POST['modulo1'])) {&#xA; $modulo1 = $_POST['modulo1'];&#xA; $_SESSION['modulo1'] = $modulo1;&#xA;} else {&#xA; $modulo1 = false;&#xA; $_SESSION['modulo1'] = false;&#xA;}&#xA; @Edilson

  • This violates the principles of dynamization on the page, and there will be no change in real time. Instead of adding an existing percentage, a new one will be added to the total value.

  • Yes, it is dynamic in that it depends on the number of modules chosen before, but when you assemble the page no matter how many questions or pages have passed, this account "has already been done", when the modules were selected, then PHP will create in the templates with this heap of if... @Guillhermenascimento

  • Yes @Edilson, there will be no change in real time, but if the user chooses 2 modules with 10 pages, then it will be 10% per page, and if it is 1 module with 5 pages, it will be 20% per page. I think about doing it in real time in the future, getting the question answered, but I have no idea how to do it, and so it already meets what I need... I do not understand what you mean by "violates the principles of dynamization on the page"... could clarify better? Thanks.

  • 1

    When I have time, if there’s still no solution to the problem, I’ll answer.

  • 1

    So it’s @Edilson and I that are confused because the word Modulos is a little abstract and can mean anything, but it’s pretty clear now yes :)

  • Ah, nice, thanks a lot! @Guilhermenascimento I even wondered whether this word would cause confusion... and how I use it in the system for users, I think I’ll even re-evaluate its use. + 1

  • @Gustavox, let me get this straight. If I fall for a module like this, I can only go to module 3 if I’ve completed module 1 and 2, right? So would it be like "a college", passing the grades... or not, one can do module 4 and 7 at the same time, as long as those modules are not each other’s prerequisites? If this is the first case I think I have a complete code in my head...

  • That’s not quite it Henry... I think until the title and description of the question are not quite certain even... First has a page with several checkboxes where the user chooses which groups of questions he wants to answer. Then from this choice a page is mounted with these groups (can be up to a single group). It’s a single page, but all the questions are in hidden Ivs, and when it answers a the continue button opens, and it advances to the next hidden div... [continues]

  • What happens is that each group has a lot of questions, so depending on the group that is chosen, there will be a certain amount of hidden Ivs, and in each one I put a progress bar... Take a look at the question (I gave a modified one), and see how I am doing without using a function, only with ifs... I think that from your answer I am getting a solution here, but I have no time to finish, if I can put here... But if you have an idea for another code I would love to see! : ) Thanks!

  • Just to top it off, this bar is not dynamic in the sense of being modified depending on the questions answered, but only the choice in the initial checkbox. It won’t change anything by answering a question, or by clicking the forward button... So for example, if he chose 2 modules out of a total of 20 questions (hidden Ivs) the bar advance would always be 5% by 5%... if there were ten, it would be 10% by 10%...

  • Oops, sorry, I stuck a H in your name in the first comment... hehe

Show 11 more comments

1 answer

3


Hi, Gustavox,

From what I understand, you want the bar to be filled out before sending the information (via POST or GET), so I don’t think I could do it in PHP. Make a call to an external Java file, or create the function within the HTML itself, as you prefer. Now for each form, you leave a button with an onclick="funcao-em-js();", this being the function that Voce will create.

For the JS function to act on the bar, the simplest way (I know the simple JS, do not expect large shows of code from me kkkk) is to put the bar being a div with any id, and there in JS Voce uses Document.getElemetById("a_id_chooseida_pra_div_da_barra_de_progress").

Now comes the only part where PHP enters. For example in Módulo1, there are 8 pages, you divide the bar into 8 parts... and so on to others. When the person clicks to enter these modules, you send the information to this php page saying which module $modulo = x. There in the code you put type that you did even, but I think with switch is better (in the sense of less code and more readable):

switch ($modulo){
    case 1:
        $divisoes = 8;
        break;
    case 2:
         $divisoes = 3;
        break;
    //o resto dos casos...
    default:
    //bota uma mensagem de erro, caso a pessoa entre nos módulos de maneira errada, sei lá...

Then you know which module the person accessed. Then just make a loop to echo inside the div which is the progress bar:

for ($k = 0; $k < $divisoes; $k++) {
   echo "<div class='incompleta'></div>";
}

This loop inside the div which is the progress bar. There in CSS you create an incomplete style (display: None;) and a complete one. You can instead of div, use list, create progress bar instead of div, a ul. (CSS below)

ul#barra_de_progresso {
   list-style-type: none;
   display: inline;
}
ul#barra_de_progresso li {
   margin: 0;
}
.incompleto {
   background-color: transparent;
}
.completo {
    background-color: a-cor-da-barra-preenchida;
}

Now the question is, with JS you have to change the class of the list, and since I am not an expert, I would do the trick... put there in the loop where the lists would come out something like li id="$divisoes+1", and the lists would have id 1 until the last division. Then with the onclick button calling the function, I would create a function with an if inside a loop.

function funcao_aqui() {
   var p;
   var divisao;
   var max;
//
for (p = 0; p < max; p++) {
   if (a divisao já tem class="completo")
      faz nada
   else 
      muda a classe para completo
      p = max (pra parar o loop)

The JS I would have to think more, surely there will be people to help this part better than me.

But I hope you’ve made your point.

Hug

  • Thanks Enrique, looking here... I made some comments on the question now, explaining how I took the variables (which are session), and so picked up on the template whether it was set or not, and then if it was, I put a %, or I put another... I think what I’m looking for is just a function with for and/or switch... I don’t know... I’m still a beginner in PHP, so I’ll think a little here hehe... Thanks! + 1

  • So your answer made me realize that I was actually traveling in trying to do this with PHP, since with Javascript/jQuery it’s infinitely simpler, and the choice in the checkboxes on the previous page is irrelevant, since the JS will run the DOM and get all inputs filled... result: besides being much simpler, it will be much more perfect (the real percentages), update in real time, and I will only have a single bar per form (and not 30+ rsrs). Since I’m wearing Bootstrap, I decided to use this bootsnip as a basis.

Browser other questions tagged

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