Question

I have the following code for a php quiz.

if(isset($_POST['submit'])) {

    //Check to make sure that the name field is not empty
        if(($_POST['q_1'] == '') ||  ($_POST['q_2'] == '') || ($_POST['q_3'] == '') || ($_POST['q_4'] == '') || ($_POST['q_5'] == '')) {
            $nameError = 'Please choose an option';
            $hasError = true;
        }
        else {
                for ($i=1; $i<=$types; $i+=1)
                    {
                        $nowval[$i] = 0;
                    }
                for ($i=1; $i<=$questions; $i+=1)
                    {
                        $qvar = "q_$i";
                        //echo $qvar;
                        tally($_POST[$qvar]);// LINE THAT CAUSES ERROR
                    }
                $dominant = 1;
                $domval = $nowval[1];
                for ($i=2; $i<=$types; $i+=1)
                    {
                        if ($domval < $nowval[$i])
                            {
                                $dominant = $i;
                                $domval = $nowval[$i];
                            }
                    }
                function tally ($question) // TALLY FUNCTION
                    {   
                        global $nowval;
                        $nowval[$question]++;
                    }
                if (is_file("$quiz.rsl"))
                    {
                        $fp = fopen("$quiz.rsl", 'r');
                        $line = fgets($fp, 1024);
                        fclose($fp);
                        $people = explode("|", $line);
                        $people[$dominant-1] += 1;
                        $timestaken = 0;
                        foreach($people as $tally)
                            {
                                settype($tally, 'integer');
                                $timestaken += $tally;
                            }
                        $fp = fopen("$quiz.rsl", 'w');
                        for($i=0; $i<$types; $i++)
                            {
                                fwrite($fp, $people[$i]."|");
                            }
                        fclose($fp);
                    }
                else
                    {
                        for($i=0; $i<$types; $i++)
                            {
                                $people[] = 0;
                            }
                        $people[$dominant-1] += 1;
                        $timestaken = 1;
                        $fp = fopen("$quiz.rsl", 'w');
                        for($i=0; $i<$types; $i++)
                            {
                                fwrite($fp, $people[$i]."|");
                            }
                        fclose($fp);
                    }
                $percentage = ($people[$dominant-1] / $timestaken) * 100;
                $dec=2;
                $format="%.$dec" . "f";  
                $number=sprintf($format,$percentage);
                $percentage=strtok($number,".");
                $dc=strtok(".");   
                if ($dec!=0) 
                    { 
                        $percentage = "$percentage" . ".$dc";
                    } 

                $emailSent = true;
    }//else
}//main if

When I POST the form data to a different file ie, if the above code is written in a separate file, it all works fine .

but when I POST the form data to the page itself, I am getting

Fatal error: Call to undefined function tally()

I am not understanding the root cause of this problem.

When I tried using

                $this->tally($_POST[$qvar]);

I am getting the following error.

Fatal error: Using $this when not in object context
Was it helpful?

Solution

It happens because you are declaring the function after trying to call it:

tally($_POST[$qvar]);// LINE THAT CAUSES ERROR

function tally ($question) // TALLY FUNCTION DECLARATION COMES LATER

I would put either before if(isset($_POST['submit'])) { or as the first thing inside.

   if (!function_exists('tally')) {
   function tally ($question) // TALLY FUNCTION
       {   
            global $nowval;
            $nowval[$question]++;
       }   
  }

$this->tally won't work because it's not an Object, you are justing decrating a function.

OTHER TIPS

You are defining the function after you call it. Moreover, you're only defining it conditionally (i.e. inside an if..else). Define your functions first, then write the code that uses them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top