RSS
 

PHP Analytics: Variables

11 Aug

1. Introduction

I want to introduce a new feature that has been added to PHP Analytics: the variables.

In PHP Analytics, variables are keywords that can be used in your queries by using the following syntax: $variable_name

2. Create a Variable

In order to use a variable, first we have to define it trough the administration panel; to define a variable we just have to specify it’s own name and a default value.

At the time of the query execution, every variable that has been previously defined in the administration tool, is replaced with the values passed trough a GET or a POST method; if none of them is used, a default value (specified at the variable definition phase) is used.

3. Usage

Usually a variable is included in the where clause so that you can filter dynamically your resultset. A typical example could be a set of charts that describe the user activity within the user’s profile page. Instead of creating a number of queries for each user (e.g.” select … from users_table where user_id = 1“), we can create a single query that works for all the users by simply passing to the query itself the user_id value, using a GET or a POST method (e.g.” select … from users_table where user_id = $user_id“)

So, if you use the GET method, the user’s profile page URL will look as follows: www.mydomain.com/profile.php?user_id=1

If you use the POST method, you will probably use in the form the POST method is associated to as follows: <input type=”hidden” id=”user_id” name=”user_id” value=”1″ />

In the example above we have used a hidden field type; but we can use whatever field type we wish.

4. Conclusions

PHP Analytics’ variables are very useful but must be used carefully in order to avoid SQL Injection; further information will be given in a specific post.

 

Tags: , , ,

Leave a Reply