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

Difficulty defining a variable before form has been submitted

I am building a program where I have a payment system that accepts a user's payment.

After they have paid for a lesson, I want to display another payment button (stripe form) that allows them to pay for an optional driver to the lesson.

However this is where my problem is.

I am creating a undefined variable counter array for the driver which doesn't work or update the table correctly if it is not defined.

Is there a way around this? Have been struggling with this for a while!

Here is the controller code

$app->get('/group-tuition', function() use($app) {
    $groups = $app['tutor']->get_group_tuition($app['auth']->get_user()['email']);
    $driver = $app['tutor']->get_driver_details();
    $counter = $app['tutor']->get_student_driver_count($app['auth']->get_user()['email']);


    return $app['twig']->render('group-tuition.twig', array('active_page' => 'group-tuition', 'is_user_logged_in' => $app['auth']->is_user_logged_in(),'groups' => $groups, 'user' => $app['auth']->get_user(), 'drivers' => $driver, 'counter' => $counter,));
});

Here is the twig view code

<td>
    {% if not is_user_logged_in %}
        <a class="group-tutors" href="/login">Login</a>
    {% elseif group.booked %}
        //this is where I have errors
        {% if counter is defined  %}
                        If you would like a lift to the lesson there are {{ counter.available }} spaces left!
            <form action="/charge1" method="post">
                <script
                    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="pk_test_7WJNkl33Jt9eXmBD7aujJniy"
                    data-image="images/footer-icon.png"
                    data-name="SOUTH LONDON TUTORS"
                    data-description="Lift"
                    data-amount="500"
                    data-currency="GBP"
                    data-locale="auto">
                </script>
                <input type="hidden" name="tutoremail" value="{{ group.tutoremail }}" />
                <input type="hidden" name="starttime" value="{{ group.starttime }}" />
                <input type="hidden" name="endtime" value="{{ group.endtime }}" />
                <input type="hidden" name="location" value="{{ group.location }}" />
                <input type="hidden" name="class" value="{{ group.class }}" />
                <input type="hidden" name="driveremail" value="{{ drivers.driveremail }}" />
            </form>
        {% else %}
         Booked 
        {% endif %}
</td>

Here is the model code

public function get_student_driver_count($studentemail){


    $studentemail = mysqli_real_escape_string($this->link, $studentemail);

    $result = mysqli_query($this->link,"select driver.driveremail,studentdriverdetails.studentemail, driver.starttime, driver.location, driver.class, capacity,
            (capacity - (select count(*) from studentdriverdetails where studentdriverdetails.driveremail = driver.driveremail and studentdriverdetails.starttime = driver.starttime)) as available,  
            (select '$studentemail' in 
            (select studentemail from studentdriverdetails where studentdriverdetails.driveremail = driver.driveremail and studentdriverdetails.starttime = driver.starttime)) as booked 
            from driver 
            inner join studentdriverdetails
            on studentdriverdetails.driveremail = driver.driveremail and studentdriverdetails.starttime = driver.starttime
            where driver.starttime >= NOW() and studentdriverdetails.studentemail='$studentemail' order by starttime asc");

    while($count = mysqli_fetch_assoc($result)){

        $counter = $count;
    }
    return $counter;
}

I know it has something to do with my twig file but I cannot seem to figure it out.

I keep going round in circles.

For example if I have two bookings with the same tutor, but only one of them has a driver associated with it. Then If I set the display to show the form for the driver, then both bookings will show the form.

This works same vice versa if a driver isn't associated with the lesson then it automatically displays both as booked which it shouldn't.

Any help would be great.