how to concatenate values from a session in php

1
Hello everyone I'm having trouble concatenating a session in php, what I intended would be the following one page to insert into an object, and then the values I would concatenate. my product class I did 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 to concatenate in some ways, but I'm a beginner in php and I can not, I've summarized the code to make it easier to read.

    
asked by anonymous 16.03.2015 / 17:28

2 answers

1

I was able to solve this by changing some things thanks to the valuable tips from the PHP Brasil community on Google+

My class started to look like this.

<?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 the index.php where I changed some things like the concatenation of values.

<?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>

I hope that if someone goes through the same problem, I'll find this solution.

    
17.03.2015 / 04:08
-1

You've tried:

$nome = $prod->getNome();
$valor = $prod->getValor();
echo $nome . $valor;
    
16.03.2015 / 17:34