how to concatenate values from a php session

Asked

Viewed 762 times

1

Hello to everyone I’m having trouble to concatenate a session in php, what I intended would be the following a page to insert into an object, and then the values I would concatenate. my product class I made so.

class Produto {
protected $nome;
protected $valor;

public function __construct($nome, $valor) {
    $this->nome = $nome;
    $this->valor = $valor;
}

public function addProd($nome, $valor){
    $prod = new Produto($nome, $valor);
}

public function getNome() {
    return $this->nome;
}
public function setNome($nome) {
    $this->nome = $nome;
}

public function getValor() {
    return $this->valor;
}
public function setValor($valor) {
    $this->valor = $valor;
}
function __toString() {
    $this->nome + $this->valor;
}

then I made the page that displays the values, in case my index.

<?php
session_start();
if(isset($_GET["acao"]) and $_GET["acao"] == "ok") {
$_SESSION["nome"] = $_POST["nome"];
$_SESSION["valor"]= $_POST["valor"];
$prod = new Produto($_SESSION["nome"], $_SESSION["valor"]);

//gostaria de conseguir concatenar aqui esses valores
echo $prod->getNome() . $prod->getValor();
}
?>

and my page to enter the values in the session.

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>

 <div class="container">
 <div class="row-fluid">
        <div class="span4">
        </div>
        <div class="span4">

            <div class="well">
            <form action="index.php?acao=ok" method="post">
                <fieldset>
                    <center><legend>Adicione um produto e seu valor</legend> </center>

                    <label>Descrição do produto</label>
                    <input class="input-xlarge" type="text" placeholder="Digite o nome do produto" name="nome" autofocus required>
                    <label>Valor do produto</label>
                    <input class="input-xlarge" type="number" placeholder="R$" name="valor" required>
                    </br></br>
                    <button type="submit" class="btn btn-primary">Salvar</button>

                </fieldset>
            </form>

            </div>
        </div>


    <div class="span4">
    </div>
</div>  

I’ve tried a few ways to concatenate, but I’m a bit of a beginner in php and I’m not getting it, I summarized the code to make it easier to read.

  • André, it was not clear in your code where the session part comes in. I could explain better?

2 answers

1

good managed to solve changing some things thanks to the precious tips of the PHP Brazil community on Google+

my class is now as follows.

<?php
class Produto {
private $nome;
private $valor;

public function getNome() {
    return $this->nome;
}
public function setNome($nome) {
    $this->nome = $nome;
}
public function getValor() {
    return $this->valor;
}
public function setValor($valor) {
    $this->valor = $valor;
}
//vou setar os valores aqui hehe
public function setValores($nome, $valor) {
    $_SESSION ["produtos"] [] = array (
            'nome' => $nome,
            'valor' => $valor 
    );
}
}

and delete my other page leaving only index.php where I changed some things like the value concatenation.

<?php
session_start();
//incluo o produto.php
include_once 'Produto.class.php';
//se existir a variavel acao e ela for igual a ok
if(isset($_GET["acao"]) and $_GET["acao"] == "ok") {    
//a variavel nome recebe se exitir o que vem do post[nome] = nome
$nome = (isset($_POST["nome"])?$_POST["nome"]:"");
//a variavel valor recebe se exitir o que vem do post[valor] = valor
$valor = (isset($_POST["valor"])?$_POST["valor"]:"");       
//crio um produto novo
$prod = new Produto();
//seto seus valores
$prod->setValores($nome,$valor); 
}

?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="view/assets/bootstrap/css/bootstrap.min.css">
</head>

<body>
<?php
echo '<div class="container">';

echo '<div class="row-fluid">';

echo '<div class="span10">';
echo '<figure>
<img src="view/catolica_logomarca.png" title="Catolica do Tocantins"/>
</figure>';
echo '<h5>Aluno Andre Martins</h5>';
echo '<h5>FC20121081</h5>';
echo '<h5>Linguagem 5</h5>';
echo '</div>';
echo '<div class="span2">';
echo '<figure>
<img src="view/sistemas.png" title="Catolica do Tocantins"/>
</figure>';
echo '</div>';
echo '</div>';
echo '</br>';
echo '</br>';
echo '</div>';
?>
    <div class="container">
        <div class="row-fluid">
            <div class="span4">
            </div>
            <div class="span4">

                <div class="well">
                <form action="index.php?acao=ok" method="post">
                <fieldset>
                <center><legend>Produto e seu valor</legend></center>

                <label>Descrição do produto</label>
                <input class="input-xlarge"
                type="text" placeholder="Digite o nome do produto"
                name="nome" autofocus required>
                <label>Valor do produto</label>
                <input class="input-xlarge"
                 type="number" placeholder="R$"
                 name="valor" required>
                </br></br>
                <button type="submit" class="btn btn-primary">Salvar</button>

                </fieldset>
                </form>

                </div>
            </div>
            <div class="span4">
            </div>
        </div>
    </div>
<?php       

echo '<div class="container">
<div class="row-fluid">';

echo '<div class="span2">';
echo '</div>';

echo '<div class="span8">';
if(isset($_GET["acao"]) and $_GET["acao"] == "ok") {
echo '<table class="table table-hover" border="2">';

echo '<tr class="info">';
echo '<td>' . 'Nome' . '</td>';
echo '<td>' . 'Valor' . '</td>';
echo '</tr>';
echo '<tr class="success">';

$pos=0;
foreach ($_SESSION['produtos'] as $value) {
    if($pos == 1){
        echo '<tr class="success">';
        $pos = 0;
    }
    echo '<td>' .$value['nome'] .'</td> <td>' . $value['valor'] .'</td>';
    $pos++;
}   
echo '</tr>';    
echo '</table>';
}
echo '</div>';
echo '</div>';
echo '</div>';
?>

<script src="view/assets/bootstrap/js/bootstrap.min.js"></script>

</body>

</html>

well I hope that if someone goes through the same problem find this solution.

-1

Have you tried:

$nome = $prod->getNome();
$valor = $prod->getValor();
echo $nome . $valor;
  • guy in this case I believe it won’t work because you’re not saving in the session and the variables are lost.

Browser other questions tagged

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