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

Impossible to access attribute ('booked') on null

I am using twig to display whether or not someone has been booked for a lesson.

I have managed to get it working however I am a bit confused. At first I was getting the error in the title

Impossible to access attribute ('booked') on null

However my function get_student_driver_booked below, is returning correct array

    public function get_student_driver_booked($email){

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

        $result = mysqli_query($this->link,"select *,(select '$email' in 
                       (select studentdriverdetails.studentemail
                       from studentdriverdetails
                       inner join driver
                       on studentdriverdetails.driveremail = studentdriverdetails.driveremail
                       where studentdriverdetails.driveremail = driver.driveremail 
                       and studentdriverdetails.starttime = driver.starttime)) as booked from studentdriverdetails   
                       where studentdriverdetails.studentemail='$email' and studentdriverdetails.starttime >= NOW()");

        while($row = mysqli_fetch_assoc($result)){
            $booked = $row;
        }
        return $booked;

    }

Then passing this function to my controller like so;

$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($driver['driveremail'],$groups[0]['tutoremail'],$groups[0]['starttime']);
    $test = $app['tutor']->get_student_driver_booked($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,
            'test' => $test
    ));
});

After running var_dump tests on $test it was apparent my function returned the correct results.

However when using this in my twig file. I could not access the "booked" attribute (key).

I am not sure exactly why this is, I mean it works so not sure if I should have the mind frame "if it ain't broke don't fix it" But I am curious to know why it is not letting me access that key.

Here is what my twig file looks like;

<tbody>
                    {% for group in groups %}
                    <tr>
                        <td>{{ group.datetime }}</td>
                        <td>{{ group.location }}</td>
                        <td><a class="group-tutors" href="/tutor/{{ group.username }}">{{ group.name }}</a></td>
                        <td>{{ group.class }}</td>
                        <td>{{ group.level }}</td>
                        <td>{{ group.topic }}</td>
                        <td>{{ group.available }}/{{ group.capacity }}</td>
                        <td>

                                <a class="group-tutors" href="/login">Login</a>
                                {% elseif group.booked and not test and counter.available > 0  %}
                                Booked! If you would like a ride 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"
                                        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>
                            {% elseif group.booked and test  %}
                                Booked for both lesson and ride to lesson
                            {% elseif group.booked %}
                                Booked for lesson
                            {% elseif group.available > 0 %}
                                <form action="/charge" method="post">
                                    <script
                                        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                                        data-key="pk_test"
                                        data-image="images/footer-icon.png"
                                        data-name="SOUTH LONDON TUTORS"
                                        data-description="{{ group.topic }}"
                                        data-amount="2000"
                                        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="description" value="{{ group.topic }}"/>
                                </form>

                            {% else %}
                                Full
                            {% endif %}
                        </td>
                        {% if is_user_logged_in and (user.type == "admin" or user.type == "tutor" and user.email == group.tutoremail) %}
                            <td><a class="group-tutor" href="/group-tuition/{{ group.tutoremail }}/{{ group.starttime }}">View Students</a></td>
                        {% endif %}
                    </tr>
                    {% endfor %}
                </tbody>