Warning in admin page when no categories set

kurkuma - November 1, 2005 - 12:48
Project:Contact directory
Version:4.6.x-1.x-dev
Component:Code
Category:bug report
Priority:minor
Assigned:Unassigned
Status:needs review
Description

The first time to access the admin section there is a warning triggered by PHP due to a bug in the code:

Invalid argument supplied for foreach() in C:\Archivos de programa\xampp\htdocs\phire\modules\contact_dir\contact_dir.module on line 404.

There is no check for an empty array of categories when retrieving them from the database.
The best option is to set up the variable holding the categories values as an empty array. Following is the new code for the _contact_dir_get_categories() function (I have just added "$contact_dir_categories = array();"):

<?php
function _contact_dir_get_categories() {
    static
$contact_dir_categories;
    if ( !isset(
$contact_dir_categories) ) {
       
$contact_dir_categories = array();
       
$result = db_query('SELECT cid, name FROM {contact_category} ORDER BY name');
        while (
$field = db_fetch_object($result)) {
           
$contact_dir_categories[$field->cid] = $field->name;
        }
    }
    return
$contact_dir_categories;
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.