Create Page Loop 3 times

Asked

Viewed 119 times

0

The user will enter the system, make another registration on a page not related to this question and will arrive on this page php.. On the page in question, it will register 3 different directors, and because it has only 1 form, it should go back and forth 3 times on the same page, how to do this?

  • 1

    Just didn’t understand the while pq, when you call the header() the script ends will not run any more lines below.

  • 2

    In practice he will enter 3 entries, right? because it does not make a form that the person can register N directors instead of redirecting.

  • 2

    I didn’t understand why I called the header 3 times, but I answered from Loop Repeat.

3 answers

2


With a repeating structure you won’t be able to do that, the right thing would be:

php.

<?php
if(!isset($_SESSION['cadastro_cont'])
    $_SESSION['cadastro_cont'] = 1;

// restante da ação da página

insert_director.php

// ação da página
<?php
//no fim, quando terminar de cadastrar:

if($_SESSION['cadastro_cont'] >= 3)
    header('Location: pagina_seguinte.php');

$_SESSION['cadastro_cont'] += 1;
header('Location: cadastro_diretor.php');

The first time you access the registration page a session variable is created to tell you what the current registration is. On the page of Insert, after the action insert, I check if the current registration represented by this variable is 3 if it is I forward to the next screen, if not I assign one more to the counter and call the registration page again.

1

Whereas you have the relationship between the tables enterprise and director, in order to store the id of the company in the director’s register, something like:

create table empresas 
(
  id int,
  name varchar(255)
);

create table diretores (
  id int,
  name varchar(255),
  empresa int
);

In a file, possibly called insert_empresa.php, the registration of the company is made. For example:

insert into empresas (id, name) values (1, "empresa_1");

This insert generates a id, related to the company record, which is passed to the archive cadastro_diretor.php, where the form is displayed. In the file insere_diretor.php, you create the director’s record by storing in the field enterprise the id of the related undertaking.

insert into diretores (id, name, empresa) values (1, "diretor_1", 1);

To know if the three directors have not yet been registered, just count the number of records in the bank:

select count(*) from diretores where empresa = 1;

Where empresa = 1 refers to the id of the company in question. This query returns an entire value and if it is less than 3, redirect the user again to the form page.

See on Ideone and in the Github Gist.

1

It would be better to put a button in the form of the signup page.php, so that the user adds new entries. Then you put a control to allow the user to register up to 3 people or exactly 3 people.

  • 4

    Can you better detail your answer? If possible with examples that can be reproduced. Doing this will increase the quality of content in the community.

Browser other questions tagged

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