FUNCTIONS AND CODE FROM / BOOK/ SURVEY/ HEADER.PHP

FUNCTIONS AND CODE FROM / BOOK/ SURVEY/ HEADER.PHP

This file is included in every file in the survey application, so every one of these functions will be available.

START OF PAGE Before we get to the functions in this file, there is some code that does a bit of housekeeping.

if (!defined(“LOADED_HEADER”)) {

include “../functions/charset.php”; include “../functions/basic.php”; dbconnect(“survey”); include “functions.php”; define(“LOADED_HEADER”, “yes”);

$result = safe_query(“select 1 from blocked_domains where ‘$REMOTE_HOST’ like concat(‘%’,domain) and release_dt is null

236 Part III: Simple Applications

if (mysql_result($result,0) > 0) {

print “<h2>sorry - your domain has been blocked from this page</h2>\n”; exit; }

This preceding code contains information you’re going to need before working with the heart of the application. The includes are clear enough. The includes have been put inside an if statement as a precaution. There is no need to reload the header once it has been loaded once. We can make sure that doesn’t happen by creating a constant named LOADED_HEADER. If by chance, this page were loaded

a second time, you wouldn’t have to worry that includes would be imported more than once.

N OTE

Remember that PHP 4 has the include_once construct, which will ensure that no files are included multiple time.

As we mentioned earlier, there is a facility here to block domains, and this appli- cation will be doing that off the $REMOTE_HOST variable. This is hardly necessary, and it is easy enough to comment out this code. In order to understand this code, look more closely at the query, particularly the like predicate. When we dial in to the net from my ISP ( att.net ), my REMOTE_HOST is something like this: 119.san- francisco-18-19rs.ca.dial-access.att.net . When you block domains, you’ll

be doing it on the top-level domain —in this case, att.net . And this top-level domain is what will reside in the database. So the query will have checked on any number of wildcard characters prior to the top-level domain name.

To achieve this you will need to concatenate the domain names with the % wild- card character. So, for instance, the query will work against %att.net . This may seem somewhat different from your typical like predicate. It’s another powerful technique to use with your SQL.

Also note that the start of the select statement doesn’t contain a select count(*), instead opting for select 1. This is a good way of testing if any rows meet the con- dition of the where clause. If the where clause matches any number of rows, the query will return a single column with the value of 1, which in the programming world means TRUE. If there are no rows returned you know the where portion of the query had no matches.

WEEKSTART( ) This function creates a MySQL function to grab the day that starts the week. You use this in the application to pick a winner for the current week.

Chapter 9: Survey 237

function weekstart ($when=””) {

if (empty($when)) { $when = “now()”; } elseif ($when != “create_dt”) { $when = “‘$when’”; } return “from_days(to_days($when)-dayofweek($when) + 1)”;

} It works like this: the MySQL to_days() function returns an integer of the number

of days since January 1, 0000. dayofweek() returns an integer representing the day of the week (Sunday equals 1, Saturday equals 7). So the portion (to_days($now)- dayofweek($when) + 1) will return an integer representing the Sunday of the week in question. The from_days() function then turns that number into a date. Here is the result of this query run on Thursday July 27, 2000 (the day this chapter was written):

mysql> select from_days(to_days(now())-dayofweek(now()) + 1); +------------------------------------------------+ | from_days(to_days(now())-dayofweek(now()) + 1) | +------------------------------------------------+ | 2000-07-23 | +------------------------------------------------+

1 row in set (0.00 sec) Note that the value passed here can be a string representing a date, it can be

empty, or it can be a field from the users table —namely the create_dt field. COUNTRYLIST( ) This function creates a drop-down list of country names. function country_list ()

{ $countries[“”] = “”; $countries = array_merge($countries

,db_values_array(“countries”,”country”) ); return $countries;

} Uses the db_values_array() function (discussed earlier in this chapter, in the

section “Reusable functions”) to get an array of countries and their abbreviations. STATE_LIST( ) This creates a drop-down list of state names. function state_list ()

{ $states[“”] = “”;

238 Part III: Simple Applications

$states = array_merge($states ,db_values_array(“states”,”state”,”statename”,”state”) ); return $states;

} Uses the db_values_array() function (discussed earlier in this chapter, in the

section “Reusable functions”) to get an array of countries and their abbreviations. FETCH_QUESTION( ) This function grabs the contents of a row in the questions

table and assigns the columns to global variables. function fetch_question ($question_id=””)

{ if (empty($question_id)) { $question_id = 0; } $result = fetch_record(“questions”,”question_id”,$question_id); return $result;

} This will run the fetch_record() function and return from the database all the

information regarding a particular question, based on the questionid. FETCH_USER( ) This function grabs the contents of a row in the users table and

assigns the columns to global variables. function fetch_user ($user_id=””)

{ if (empty($user_id)) { $user_id = 0; } $result = fetch_record(“users”,”user_id”,$user_id); return $result;

} Returns the result set based on a user_id .