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 :(
4
  • What Dennis said. Also check out variable scope stackoverflow.com/questions/16959576/… Commented Feb 10, 2015 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. Commented Feb 10, 2015 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 Commented Feb 10, 2015 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 :) Commented Feb 10, 2015 at 16:13

2 Answers 2

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 = '';
Sign up to request clarification or add additional context in comments.

Comments

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 />";
    }
}

1 Comment

I can not vote up but I can thank you. Thanks! :) beautiful coded ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.