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 Integrating PHP with Databases Querying the Database with PHP Improve with Refactoring

Andrew Dickens
Andrew Dickens
18,352 Points

What are these arguments in the full_category_array( $limit = null, $offest = 0) and can/how do I use them?

Hi this isn't mentioned in the video but is in the code for the teachers notes

function full_catalog_array($limit = null, $offset = 0) {
    include("connection.php");

    try {
        $results = $db->query("SELECT title, category,img FROM Media”);
    } catch (Exception $e) {
        echo "Unable to retrieved results";
        exit;
    }

    $catalog = $results->fetchAll(PDO::FETCH_ASSOC);
    return $catalog;
}

what are the arguments $limit = null, $offest = 0 and can/how do I use them?

2 Answers

Andrew Dickens
Andrew Dickens
18,352 Points

I found the answer, in a much much later tutorial

https://teamtreehouse.com/library/integrating-php-with-databases/limiting-records-in-sql/setting-limits

In Alena's dulcet tones...

"In our catalog array we'll need to pass some optional arguments again. We'll do this with limit = null and offset = 0. Remember by giving a parameter a default value we do not require that the argument be passed. If we were to call the function full catalog array without passing these arguments, we would not have a limit and our offset would be set to 0. By setting the offset default to 0, we can then pass a limit without an offset. And it would start at the very first item."

I have not followed this course, but I think you would have to add this to the SQL Query before it works.

$db->query("SELECT title,category,img FROM Media LIMIT $offset, $limit");

When you set the arguments you can skip (offset) records and of course with limit, you can set a limit on how many records are returned.

<?php
full_catalog_array($limit = 10, $offset = 1); // skip the first record, limit to 10 records
?>
Andrew Dickens
Andrew Dickens
18,352 Points

it works fine without $limit = null, $offset = 0 , so why is it there? is the question.