Since we will be interacting with the database, one of your first lines of code can be
to connect to the database with your username and password. I recommend having this in a
single file, and just include the file when you need to connect in your scripts. See below:
Now we will run a query to find out how many items (rows) match the category being viewed.
This will be the total amount of items that can be showed.This query can look something
like this:
$q is the query, and after that we make sure the query was successful. If not, we display
the error. After that, $total_items will contain the value of the max amount of results.
Next, we can declare a few of our important variables, $limit, $cat and $page. The values
of these variables are obtained through the $_GET method.
If for any reason, this data is not available, we can set defaults. This might be because
someone hand typed the url containing these values, or because someone is intentionally
attempting to cause errors in your scripts. So, we use the code:
The first condition will set $limit to 10 (default) if $limit was empty, non numeric,
less than 10, greater than 50. The function, is_numeric(); is boolean, so it returns
true if $limit is numerical, false if it is non numerical (containing non numeric data).
The second condition is basically the same. If $page is empty, non numeric, less than zero,
greater than the max amount of results; Then set $page to the value of 1 (default).