How to configure the Session?

Asked

Viewed 276 times

2

I have this activity proposed:

This is the Weekly Activity of the third competence and in the competence has been shown what is and how to use functions with arguments and returns, variable scope, forms, GET and POST sending method, sessions, the function isset(), among other important commands in the development of a system. Now we need to check if you know how to use the exposed knowledge.

Activity

In this activity each student must modify the application of competence 2. If you did not, you will have to do it now. However, this new application will iterate 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 show some words in hyperlink, also, further down on the same page, should show a form to send a word and at the end should have two tables, one with words with the total of odd letters and the other with the total of even letters.

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

The word he wants. For this solution, will be evaluated if it had use of function, session, sending and reception by the GET and POST method. Below is the word array and a picture 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 been able to do a part, but at the time of starting the session to echo the selected variables I cannot 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 would like some guidance on how to log in to store the variables entered by POST and GET. Thanks in advance.

2 answers

2


I don’t know if I understand exactly what you need, but I think the example below can 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>

Upshot:

PAR
Word/Letters
education/8
persistence/12
evolution/8
work/8 enthusiasm/10
knowledge/12
life/4

UNIQUE
Word/Letters
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’ve helped!

  • Hello, thanks for the help, but I need the words to be in your separate tables saved there (through a session) as it appears in the photo, when clicking on a word it goes to a table, classifying it as even or odd.

  • the input methods should be by post or get(hyperlink)

  • should count the number of words individually and not as a whole.

  • Separate if it is even or odd... but if WHO is even or odd? The word order in the list? How to count the words individually?

  • See, I need to fix this code I sent to that separates the words obtained by get and post in two tables, a pair other odd (will be determined by the amount of letters) and so registering in the session. Ex: insert word "sun" will be stored in the session then the code will take all the words of the session and count the number of letters and send to a table. that’s all.

  • i can’t do the part of the session to pick up the strings one by one and put in strlen and sort if word % == 2 pair Lse % <> 2 end if I have to deliver today, I’m going crazy because I have no help from teachers

  • Ok, I’ve updated from your comment... see now if it’s as you need. Anything just let me know.

  • @0V3RL04D3R warns me if it worked or not ok?

  • It worked now I’m trying to configure the encoding. http://prntscr.com/c1c49x. Thank you very much

  • All set, I’m immensely grateful.

  • Need just send msg

Show 6 more comments

1

I believe you should think this way,

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

2nd Catch for $_POST any word you type in input (CALLED WORD FOR EX) and store it in a variable tbm on the second page $palavra = $_POST ['palavra'];

3rd knowing that you must use $_SESSIONS to store the 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;

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

In Sumula,

1- recovers values
2- TEST THESE VALUES AND STORE IN SESSSIONS
3- TEST THESE VALUES AND WRITE IN TABLES (Stored Sesssions);

Browser other questions tagged

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