Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

Error call to undefined function

I am trying to get the starttime from a db in a function and then pass that function return result to another function so I can run another query on that.

However I am getting the error;

Fatal error: Uncaught Error: Call to undefined function Tutor\Provider\get_starttime() in /var/www/html/southlondontutors.com/src/provider/TutorServiceProvider.php:94 Stack trace: #0 /var/www/html/southlondontutors.com/index.php(46): Tutor\Provider\Tutor->get_student_driver_count() #1 [internal function]: {closure}() #2 /var/www/html/southlondontutors.com/vendor/symfony/http-kernel/HttpKernel.php(139): call_user_func_array(Object(Closure), Array) #3 /var/www/html/southlondontutors.com/vendor/symfony/http-kernel/HttpKernel.php(62): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #4 /var/www/html/southlondontutors.com/vendor/silex/silex/src/Silex/Application.php(586): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #5 /var/www/html/southlondontutors.com/vendor/silex/silex/src/Silex/Application.php(563): Silex\Application->handle(Object(Symfony\Component\HttpFoundation\Request)) #6 /var/www/html/southlondontutor in /var/www/html/southlondontutors.com/src/provider/TutorServiceProvider.php on line 94

here is the two functions I am trying to create;

    public function get_student_driver_count(){
        $starttime = get_starttime();
        $result = mysqli_query($this->link,"select count(*) from studentdriverdetails where starttime='$starttime'");
        var_dump($result);
        exit;
    }

    public  function get_starttime() {
        $res = mysqli_query($this->link,"select starttime from studentdriverdetails");
        while($row = mysqli_fetch_assoc($res)){
            foreach($row as $item){
                $starttimes = $item;
            }
        }
        return $starttimes;
    }
Simon Coates
Simon Coates
28,694 Points

are these in a class? Otherwise it might have issue with using the 'public' keyword. (update: I'm assuming from your use of $this that the above code is a fragment and it does fit within a class.)

1 Answer

Simon Coates
Simon Coates
28,694 Points

You might be missing $this-> prior to get_starttime(); THe php error message seems to differ depending on whether you are failing to find a function or a method. eg (" Uncaught Error: Call to undefined method Peach::getsmex() "). In your case, it seems to be looking for a function.

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Yes these are in a class. I managed to get it working but still getting errors for what I am trying to achieve.

1) I want to create a driver for a lesson with four spaces

2) When student books, I want it to update somewhere that there are only 3 spaces left in the car

3) get the count for the particular lesson, then display this on the view

However I am having difficulty passing around arguments to functions in the controller that will then end up undefined

controller

groups = $app['tutor']->get_group_tuition($app['auth']->get_user()['email']);
    $counter = $app['tutor']->get_student_driver_count($app['tutor']->get_starttime());
    return $app['twig']->render('group-tuition.twig', array('active_page' => 'group-tuition', 'is_user_logged_in' => $app['auth']->is_user_logged_in(),'groups' => $groups, 'counter' => $counter, 'user' => $app['auth']->get_user(), 'drivers' => $app['tutor']->get_driver_details(),));
});
 public function get_student_driver_count($starttime){
        foreach($starttime as $start){
            $result = mysqli_query($this->link,"select count(*) as counts from studentdriverdetails where starttime='$start' group by starttime");
        }
        $counter = array();

        while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
            var_dump($row);
            exit;
            $counter[] = $row['counts'];
        }
        var_dump($counter);
        exit;
    }

    public  function get_starttime() {
        $res = mysqli_query($this->link,"select * from studentdriverdetails");
        $storeStartTime = array();
        while($row = mysqli_fetch_array($res,MYSQLI_ASSOC)){
            $storeStartTime[] = $row['starttime'];
        }

        return $storeStartTime;
    }




    public  function get_starttime() {
        $res = mysqli_query($this->link,"select * from studentdriverdetails");
        $storeStartTime = array();
        while($row = mysqli_fetch_array($res,MYSQLI_ASSOC)){
            $storeStartTime[] = $row['starttime'];
        }

        return $storeStartTime;
    }




    public function addDriver($tutoremail,$starttime,$location,$class,$driveremail){
        $driveremail = mysqli_real_escape_string($this->link, $driveremail);
        $tutoremail = mysqli_real_escape_string($this->link, $tutoremail);
        $starttime = mysqli_real_escape_string($this->link, $starttime);
        $location = mysqli_real_escape_string($this->link, $location);
        $class = mysqli_real_escape_string($this->link, $class);

        $res=mysqli_query($this->link, "insert into driver (driveremail,tutoremail,starttime,location,class)  values ('$driveremail', '$tutoremail', '$starttime', '$location','$class')");
        if($res){
            return true;
        }else{
            return false;
        }

    }

    public function get_driver_details(){
        $result = mysqli_query($this->link, 'select * from driver');

        while($driver = mysqli_fetch_assoc($result)){
            $drivers = $driver;
        }
        return $drivers;
    }



    public function add_student_driver_details($driveremail,$studentemail,$tutoremail,$starttime,$endtime,$location,$class){
        $driveremail = mysqli_real_escape_string($this->link, $driveremail);
        $studentemail = mysqli_real_escape_string($this->link, $studentemail);
        $tutoremail = mysqli_real_escape_string($this->link, $tutoremail);
        $starttime = mysqli_real_escape_string($this->link, $starttime);
        $endtime = mysqli_real_escape_string($this->link,$endtime);
        $location = mysqli_real_escape_string($this->link, $location);
        $class = mysqli_real_escape_string($this->link, $class);
        mysqli_query($this->link, "insert into studentdriverdetails values ('$driveremail','$studentemail','$tutoremail','$starttime','$endtime','$location','$class')");
    }