Control modules on Community Builder pages

(4 votes, average 4.75 out of 5)

In this example we choose modules based on the zipcode that the logged-in user has entered with their address in their profile. You can select based on their state, country or anything else in their profile.

$user_id = $db->getEscaped((int)($user->id));
 
if ($user_id > 0) {
 $query = "select * from #__comprofiler  where user_id = '$user_id';"; 
 $db->setQuery( $query );
 $row     = $db->loadObject();
 /* you can now get hold of state, phone, fax, website,
  * location, or whatever else they enter. All optional fields
  * like this get prefixed with "cb_" (see below).
  */ 
 $zipcode = $row->cb_zipcode;
 $country = strtolower($row->cb_country);  /* lower-case for easier comparisons */
 $city    = strtolower($row->cb_city); 
 $state   = strtolower($row->cb_state);
 // etc... add more variables depending on what fields are set up in CB.
 
 // your rules: use any of the following and customise as required.
 if ($country == "new zealand" || $country == "nz") return 30; /* check countries */
 if ($zipcode == 210) return 55; /* module 55 is the one for people in zip 210 */ 
 if ($zipcode == 211) return 56; /* module 56 is the one for people in zip 211 */ 
 if ($zipcode >= 212 && $zipcode <= 300) return 57; /* mod 57 for the range 212-300 */ 
 /* In PHP, || means "or", && means "and" */
 if ($zipcode == 222 || $zipcode == 333
   || ($zipcode >= 412 && $zipcode <= 512) ) return 58; 
}