3

Can someone explain me why I cannot call a var that is set inside an if? And how to call it? I don't understand why this come empty.

I need the vars $workshop_manha and $workshop_tarde bring the values that comes from the DB.

CODE

$id    = implode(",", $id);
 
$sql_consulta  = mysql_query("SELECT * FROM pessoa WHERE id IN($id)")
    or die (mysql_error());
    
    $linha = mysql_fetch_array($sql_consulta);
    $id = $linha['id'];
    $nome = $linha['nome'];
    $opcoes = $linha['opcoes'];
    

    $opcoes2 = explode(":", $opcoes);

    $opcoes3 = explode("+", $opcoes2[1]);
    $opcao_congresso = $opcoes3[0]; // Option Congress
    
    
    if(!empty($opcoes2[2])){
    $opcoes4 = explode("+", $opcoes2[2]);
    $pre_workshop_manha = $opcoes4[0]; // Workshop Morning Option
        if($pre_workshop_manha == 'Paul Gilbert'){
            $workshop_manha = "Paul Gilbert: Introdução à Terapia Focada na Compaixão e Técnicas";
        }
        if($pre_workshop_manha == 'Daniel Rijo'){
            $workshop_manha = "Daniel Rijo: Os Esquemas do terapeuta e a relação terapêutica com doentes com patologia de personalidade";
        }
        if($pre_workshop_manha == 'Maria Salvador'){
            $workshop_manha = "Maria do Céu Salvador: Os Esquemas do terapeuta e a relação terapêutica com doentes com patologia de personalidade";
        }
    }
    
    
    if(!empty($opcoes2[3])){
    $opcoes5 = explode("+", $opcoes2[3]);
    $pre_workshop_tarde = $opcoes5[0]; // Worhshop Afternoon Option
        if($pre_workshop_tarde == 'Donna Sudak'){
            $workshop_tarde = "Donna Sudak: Quando as coisas ficam difíceis: Aplicações práticas da Terapia Comportamental Dialética";
        }
        if($pre_workshop_tarde == 'Philipp Kendall'){
            $workshop_tarde = "Philipp Kendall: Estratégias dentro de tratamentos empiricamente baseados em evidências para jovens com ansiedade";
        }
    }
    
    echo "Work manhã: ".$workshop_manha; //is coming empty :(
    echo "Work tarde: ".$workshop_tarde; //is coming empty :(
Community
  • 1
  • 1
Felipe Lima
  • 443
  • 1
  • 10
  • 19
  • What Dennis said. Also check out variable scope http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and – Dave Goten Feb 10 '15 at 15:47
  • The values are empty because your if statements are all `false`. What is in `$opcoes4[0]` and `$opcoes5[0]` besides open for sql injections and you probably don't have error reporting on. – Daan Feb 10 '15 at 15:47
  • Thank in advance Dave Goten and Daan and for sure Dennis I did what Dennis indicate but it not works. Let me say that if I echo the $opcoes4[0] it brings me the morning option. But I not getting to pass it to the $workshop_manha var – Felipe Lima Feb 10 '15 at 16:11
  • Ok. I figured it out. I added a 'trim' command before the $pre_workshop_manha and $pre_workshop_tarde The registry in DB was coming with spaces after the explode and comparing are not equals Thank you guys for all the help and for your time :) – Felipe Lima Feb 10 '15 at 16:13

2 Answers2

1

That's because $workshop_manha and $workshop_tarde are not defined before the if statement.

Put this before the if statement:

$workshop_manha = '';
$workshop_tarde = '';
Refilon
  • 3,334
  • 1
  • 27
  • 51
1

You can use them as an array(). Empty the values at the beginning :

$workshop_manha=array();
$workshop_tarde=array();

Than use the values as :

$workshop_manha[] = "Paul Gilbert: Introdução à Terapia Focada na Compaixão e Técnicas";

Display them as below :

if(!empty($workshop_manha))  {
    foreach ($workshop_manha as $manha) {
        echo "$manha <br />";
    }
}

if(!empty($workshop_tarde))  {
    foreach ($workshop_tarde as $tarde) {
        echo "$tarde <br />";
    }
}
Alex
  • 626
  • 7
  • 16