Search code examples
phpfunctiondefinitiondefined

Why code works out of but not inside of an empty function?


Im working on a project for school which is mostly data management and presentation. I have a long piece of code that i would need to copy and paste into a different part of the project so i thought i would make it a function. I can copy and paste the code that works outside of the function into an empty function and it will not produce the effect that the code produced outside of the function (but it doesn't cause an error). It is my first time using a function in php, and it seems simple but i cannot get it to work.

Here is the code that works:

<?php

//function display_question($problem_id, $slide_num, $attempt, $admin_id, $result){

//}

include('header.php');
$result = "";
if(isset($_POST['problem_id'])){
    if (!isset($_SESSION['slide']) || empty($_SESSION['slide'])){
        $_SESSION['slide'] = 1;
    }
    $slide_num = $_SESSION['slide'];
    $problem_id = $_POST['problem_id'];
    $admin_id = $_SESSION['user_id'];
    if (!isset($_SESSION['phase']) || empty($_SESSION['phase'])){
        $_SESSION['phase'] = 1;
    }
    $phase = $_SESSION['phase'];
    if($phase == 1){              //show problem attempt#1
        $attempt = 1;
    }elseif($phase == 2){         //graph attempt#1

    }elseif($phase == 3){         //match Students into groups

    }elseif($phase == 4){         //show problem attempt#2
        $attempt = 2;
    }elseif($phase == 5){         //graph attempt#2

    }else{
        $result = 'phase Error';
    }
    //display_question($problem_id, $slide_num, $attempt, $admin_id, $result);
    $update = mysql_query("UPDATE users SET active=1, slide_id='$problem_id', attempt='$attempt' WHERE id='$admin_id'");
    $query = mysql_query("SELECT * FROM problem WHERE id='$problem_id'");

    if($update && $query){

        $row = mysql_fetch_row($query);
        $topic = $row[2];
        $problem = $row[3];
        $solution_a = $row[4];
        $solution_b = $row[5];
        $solution_c = $row[6];
        $image = $row[7];


        if(!empty($image)){
            $result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
                    Attempt #: '.$attempt.'
                    <h1>'.$topic.'</h1>
                    <img src="'.$image.'" width="500"/>
                    <h2>'.$problem.'</h3>
                    <h3><ol type="A">
                        <li>'.$solution_a.'</li>
                        <li>'.$solution_b.'</li>
                        <li>'.$solution_c.'</li>
                    </ol></h3>';

        }else{
            $result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
                    Attempt #: '.$attempt.'</br>
                    <h1>'.$topic.'</h1>                         
                    <h2>'.$problem.'</h3>
                    <h3><ol type="A">
                        <li>'.$solution_a.'</li>
                        <li>'.$solution_b.'</li>
                        <li>'.$solution_c.'</li>
                    </ol></h3>';

        }
    }else{
        $result = "Error";
    }

    $result .= ' </br>


        <div id="outer"><div class="inner"><form action="slide.php" method="post">
        <input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
        <input type="submit" value="Prev Slide"  class="submit"/>
        </form></div>
        <div class="inner"><form action="slide.php" method="post">
        <input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
        <input type="submit" value="Next Phase" class="submit"/>
        </form></div>
        <div class="inner"><form action="slide.php" method="post">
        <input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
        <input type="submit" value="Next Slide"  class="submit"/>
        </form></div></div>';

} else {
    $result = "problem_id not set";
}
?>
<!DOCTYPE HTML>
<html>
<title>Presentation</title>
<link rel="stylesheet" type="text/css" href="css/header.css">
<style type="text/css">
div.main{
width: 500px;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
padding: 20px;  
background-color: white;
border: 2px solid black;
border-radius: 5px;
}
#outer{
    width:100%;
    text-align: center;
}
div.inner{
    display: inline-block;
}
form.problem> input{
font-size: 1.1em;
margin-top: 10px;
}
form.nav{
font-size: 1.1em;
margin-top: 10px;
display: inline;
}
input.submit{

width: 120px;
height: 40px;
color: white;
background-color: green;
border: none;
border-radius: 5px; 
margin-top: 25px;
}
input.submit:hover{
background-color: #006600;
}
</style>
</head>
<body>

    <div class="main">
        <?php echo $result;?>
    </div>
</body>
</html>

And here is the code copy and pasted into the function that doesnt work:

<?php

function display_question($problem_id, $slide_num, $attempt, $admin_id, $result){
    $update = mysql_query("UPDATE users SET active=1, slide_id='$problem_id', attempt='$attempt' WHERE id='$admin_id'");
    $query = mysql_query("SELECT * FROM problem WHERE id='$problem_id'");

    if($update && $query){

        $row = mysql_fetch_row($query);
        $topic = $row[2];
        $problem = $row[3];
        $solution_a = $row[4];
        $solution_b = $row[5];
        $solution_c = $row[6];
        $image = $row[7];


        if(!empty($image)){
            $result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
                    Attempt #: '.$attempt.'
                    <h1>'.$topic.'</h1>
                    <img src="'.$image.'" width="500"/>
                    <h2>'.$problem.'</h3>
                    <h3><ol type="A">
                        <li>'.$solution_a.'</li>
                        <li>'.$solution_b.'</li>
                        <li>'.$solution_c.'</li>
                    </ol></h3>';

        }else{
            $result = 'Admin ID: '.$admin_id.'</br><div align="right">Slide '.$slide_num.'</div>
                    Attempt #: '.$attempt.'</br>
                    <h1>'.$topic.'</h1>                         
                    <h2>'.$problem.'</h3>
                    <h3><ol type="A">
                        <li>'.$solution_a.'</li>
                        <li>'.$solution_b.'</li>
                        <li>'.$solution_c.'</li>
                    </ol></h3>';

        }
    }else{
        $result = "Error";
    }
}

include('header.php');
$result = "";
if(isset($_POST['problem_id'])){
    if (!isset($_SESSION['slide']) || empty($_SESSION['slide'])){
        $_SESSION['slide'] = 1;
    }
    $slide_num = $_SESSION['slide'];
    $problem_id = $_POST['problem_id'];
    $admin_id = $_SESSION['user_id'];
    if (!isset($_SESSION['phase']) || empty($_SESSION['phase'])){
        $_SESSION['phase'] = 1;
    }
    $phase = $_SESSION['phase'];
    if($phase == 1){              //show problem attempt#1
        $attempt = 1;
    }elseif($phase == 2){         //graph attempt#1

    }elseif($phase == 3){         //match Students into groups

    }elseif($phase == 4){         //show problem attempt#2
        $attempt = 2;
    }elseif($phase == 5){         //graph attempt#2

    }else{
        $result = 'phase Error';
    }
    display_question($problem_id, $slide_num, $attempt, $admin_id, $result);


    $result .= ' </br>


        <div id="outer"><div class="inner"><form action="slide.php" method="post">
        <input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
        <input type="submit" value="Prev Slide"  class="submit"/>
        </form></div>
        <div class="inner"><form action="slide.php" method="post">
        <input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
        <input type="submit" value="Next Phase" class="submit"/>
        </form></div>
        <div class="inner"><form action="slide.php" method="post">
        <input type="number" value="'.$id.'" hidden="hidden" name="problem_id"/>
        <input type="submit" value="Next Slide"  class="submit"/>
        </form></div></div>';

} else {
    $result = "problem_id not set";
}
?>
<!DOCTYPE HTML>
<html>
<title>Presentation</title>
<link rel="stylesheet" type="text/css" href="css/header.css">
<style type="text/css">
div.main{
width: 500px;
margin-top: 25px;
margin-left: auto;
margin-right: auto;
padding: 20px;  
background-color: white;
border: 2px solid black;
border-radius: 5px;
}
#outer{
    width:100%;
    text-align: center;
}
div.inner{
    display: inline-block;
}
form.problem> input{
font-size: 1.1em;
margin-top: 10px;
}
form.nav{
font-size: 1.1em;
margin-top: 10px;
display: inline;
}
input.submit{

width: 120px;
height: 40px;
color: white;
background-color: green;
border: none;
border-radius: 5px; 
margin-top: 25px;
}
input.submit:hover{
background-color: #006600;
}
</style>
</head>
<body>

    <div class="main">
        <?php echo $result;?>
    </div>
</body>
</html>

Nothing is being printed by the variable $result except for what is added after it. From what i read, function args are passed byval by default so I dont think thats the problem, and i have checked all the names of variables to make sure they are either defined in the function or passed into the function and by what i can see they are. I don't know what else to look for. it may be something easy to make me look stupid but i am new to php so please help me out.


Solution

  • You must pass $result by reference if you expect the function to modify it. Right now the way you have declared your function (function display_question($problem_id, $slide_num, $attempt, $admin_id, $result)) is pass by value, for all arguments. Try this:

    function display_question($problem_id, $slide_num, $attempt, $admin_id, &$result)
    

    Also read up here: http://php.net/manual/en/language.references.pass.php