How to configure the session?

2

I have this proposed activity:

This is the Weekly Activity of the third competence and in the competency was shown what is and how to use functions with arguments and returns, scope of variables, forms, GET and POST sending method, sessions, isset() function, between other important commands in the development of a system. Now we need to make sure you know how to use the knowledge exposed.

Activity

In this activity each student should modify the application of competence 2. If you did not, you will have to do it now. However, this new application will have iteration with the user through hyperlinks and a web form. You will have to decide properly when you need to use GET or POST.

The application should display some words in a hyperlink, too, below the same page, should show a form to send a word and at the end should have two tables, one with words with odd letters total and the other with the total of even letters.

The words in the hyperlinks are the same as in the previous activity array. When the user clicks one of them, it will appear in the correct table. With the form, the user can send one more word to the table.

The word he wants. For this solution, it will be evaluated if it had use of function, session, sending and receiving by the method GET and POST. Below is the array of words and a figure showing the result in the browser.

$valores = ['estudar', 'educação', 'esforço', 'persistência', 'dedicação', 'crescimento', 'evolução', 'sabedoria', 'trabalho', 'entusiasmo', 'alegria', 'vitoria', 'sucesso', 'profissão', 'conhecimento', 'vida'];

I have already managed to do a part, but at the time of starting the session to echo the selected variables I can not do it correctly.

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title> Atividade Semanal 2 </title>
    </head>
<body>
    <a href="teste.php?palavra=estudar">estudar</a>
    <a href="teste.php?palavra=educação">educação</a>
    <a href="teste.php?palavra=esforço">esforço</a>
<a href="teste.php?palavra=persistência">persistência</a>
    <a href="teste.php?palavra=dedicação">dedicação</a>
    <a href="teste.php?palavra=crescimento">crescimento</a>
    <a href="teste.php?palavra=evolução">evolução</a>
    <a href="teste.php?palavra=sabedoria">sabedoria</a>
    <a href="teste.php?palavra=trabalho">trabalho</a>
    <a href="teste.php?palavra=entusiasmo">entusiasmo</a>
    <a href="teste.php?palavra=alegria">alegria</a>
    <a href="teste.php?palavra=vitoria">vitoria</a>
    <a href="teste.php?palavra=sucesso">sucesso</a>
    <a href="teste.php?palavra=profissão">profissão</a>
    <a href="teste.php?palavra=conhecimento">conhecimento</a>
    <a href="teste.php?palavra=vida">vida</a>

            <form action="teste.php" method="post">
<label for="palavra"> Insira a palavra: </label>
<input id="palavra" name="palavra" type="text" name="post" required><br>
<input type="submit" value="enviar"/>
</form>

<table width="200" border=" 1">
<tr>
<tr alingn="center"> 
<td align="center">Palavras</td>
<td align="center">Quantidade de Letras </td>
</tr>

<?php

$palavaead = $_POST['palavra'];
echo "<h1>Tabela Par</h1>";
$total = strlen(utf8_decode($palavaead));
if ($total % 2 == 0){ 
echo '<tr align="center"> 
<td align="center">' . $palavaead . '</td>
<td align="center">' . $total . '</td>
</tr>';
}

$palavaead = $_GET['palavra'];
$total = strlen(utf8_decode($palavaead));
if ($total % 2 == 0){ 
echo '<tr align="center"> 
<td align="center">' . $palavaead . '</td>
<td align="center">' . $total . '</td>
</tr>';
}
?>

<table width="200" border=" 1">
<tr>
<tr alingn="center"> 
<td align="center">Palavras</td>
<td align="center">Quantidade de Letras </td>
</tr>

<?php
$palavaead= $_POST['palavra'];
echo "<h1>Tabela Impar</h1>";
$total = strlen(utf8_decode($palavaead));
if ($total % 2 <> 0){ 
echo '<tr align="center"> 
<td align="center">' . $palavaead . '</td>
<td align="center">' . $total . '</td>
</tr>';
}

$palavaead = $_GET['palavra'];
$total = strlen(utf8_decode($palavaead));
if ($total % 2 <> 0){ 
echo '<tr align="center"> 
<td align="center">' . $palavaead . '</td>
<td align="center">' . $total . '</td>
</tr>';
}
?>
</html>

I'd like an orientation on how to log in to store the variables entered by POST and GET. Thanks in advance.

    
asked by anonymous 03.08.2016 / 00:48

2 answers

2

I do not know if I understand exactly what you need, but I think the example below might help you.

<?php
// inicia a seção. antes de escrever qualquer html na página
session_start();


// DEFINE O CHARSET COMO UTF-8
header ('Content-type: text/html; charset=UTF-8'); 

// se session['palavras'] não iniciada, inicia com valores da atividade anterior
if (!isset($_SESSION['palavras'])) {
    // array da atividade anterior com as palavras
    $valores = ['estudar', 'educação', 'esforço', 'persistência', 'dedicação', 'crescimento', 'evolução', 'sabedoria', 'trabalho', 'entusiasmo', 'alegria', 'vitoria', 'sucesso', 'profissão', 'conhecimento', 'vida'];
    $_SESSION['palavras'] = $valores;
}



// se recebeu POST
if (isset($_POST['palavra'])) { // se recebeu post
    // adiciona uma nova palavra à session
    $_SESSION['palavras'][] = $_POST['palavra'];
    $post_recebido = $_POST['palavra'];
} else {
    $post_recebido = 'nenhum';
}


// se recebeu GET
if (isset($_GET['palavra'])) { // se recebeu post
    $get_recebido = $_GET['palavra'];
} else {
    $get_recebido = 'nenhum';
}

?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title> Atividade Semanal 2 </title>
    </head>
    <body>
        <h1>Palavras existentes:</h1>
<?php
echo count($_SESSION['palavras']) . ' palavras.';


$total_letras = 0;
$array_par = array();
$array_impar = array();

echo '<ul>';
// faz um loop em $_SESSION['palavras']
for ($i = 0; $i < count($_SESSION['palavras']); $i++) {
    // lista cada palavra em um LI
    echo '<li>';
    echo '<a href="teste.php?palavra=' . $_SESSION['palavras'][$i] . '">' . $_SESSION['palavras'][$i] . '</a>';
    echo '</li>';
    // conta letras
    $total_letras += mb_strlen($_SESSION['palavras'][$i], 'UTF-8'); // contar corretamente considerando a codificação utilizada

    // separa pares e ímpares
    $qtd_letras = mb_strlen($_SESSION['palavras'][$i], 'UTF-8');
    if($qtd_letras %2 === 0){
        $array_par[] = $_SESSION['palavras'][$i];
    }else{
        $array_impar[] = $_SESSION['palavras'][$i];
    }


}
echo '</ul>';

$quantidade_de_palavras = count($_SESSION['palavras']);
?>

        <form action="teste.php" method="post">
            <label for="palavra"> Insira a palavra: </label>
            <input id="palavra" name="palavra" type="text" name="post" required><br>
            <input type="submit" value="enviar"/>
        </form>



        <table width="200" border=" 1">
            <tr alingn="center"> 
                <td align="center">Palavras</td>
                <td align="center">Quantidade de Letras </td>
                <td align="center">POST recebido</td>
                <td align="center">GET recebido</td>
            </tr>
            <tr alingn="center"> 
                <td align="center"><?php echo $quantidade_de_palavras; ?></td>
                <td align="center"><?php echo $total_letras; ?></td>
                <td align="center"><?php echo $post_recebido; ?></td>
                <td align="center"><?php echo $get_recebido; ?></td>
            </tr>
        </table>


        <?php

        echo '<strong>PAR</strong>';
        echo '<table border="1">';
        echo '<tr><td>Palavra</td><td>Letras</td></tr>';
        for($i = 0; $i < count($array_par); $i++){
            echo '<tr>';
            echo '<td>' . $array_par[$i] . '</td>';
            echo '<td>' . mb_strlen($array_par[$i], 'UTF-8') . '</td>';
            echo '</tr>';
        }
        echo '</table>';
        echo '<br/>';


        echo '<strong>ÍMPAR</strong>';
        echo '<table border="1">';
        echo '<tr><td>Palavra</td><td>Letras</td></tr>';
        for($i = 0; $i < count($array_impar); $i++){
            echo '<tr>';
            echo '<td>' . $array_impar[$i] . '</td>';
            echo '<td>' . mb_strlen($array_impar[$i], 'UTF-8') . '</td>';
            echo '</tr>';
        }
        echo '</table>';
        echo '<br/>'


        ?>

    </body>
</html>

Result:

PAR
Word / Lyrics
education / 8
persistence / 12
evolution / 8
work / 8 Enthusiasm / 10
knowledge / 12
life / 4

IMPAR
Word / Lyrics
to study / 7
effort / 7
dedication / 9
growth / 11 wisdom / 9
joy / 7
vitoria / 7
success / 7
Profession / 9

Any questions, post a comment.

I hope I have helped!

    
03.08.2016 / 13:53
1

I think you should think this way,

1 Thinking that you have to get the word for $ _GET using the hyperlink <a href="pag.php?variavel=estudar"> will be sent to a variable on page 2 $var = $_GET['variavel']

2º Catch by $_POST any word type in input (WHAT'S CALLED WORD BY EX) and store it in a variable tbm on the second page $palavra = $_POST ['palavra'];

3rd know that you should use $_SESSIONS to store words sent by hyperlink or input field, and test whether it will be even or odd.

4º In this test, you only test and store the values;

5º You must again count the total number of letters you have in each word and store it in another variable ... after that you have to write the results in tables

In sumula,

1- recover values 2- TEST these values AND ARMAZENA in sessions
3- TEST these values AND WRITE on tables (the stored sessions);

    
03.08.2016 / 17:15