Page 1 of 1

Referencing class properties

Posted: Sun Mar 27, 2011 7:45 pm
by bpeaks1
Being fairly new to Php I am trying to adopt some principles and experiences I have learned with VB.NET. I am trying to create a class that will create an array of valid types in order to prevent going back to the data base dozens of times during a session.

This is the code for the class, it includes some debugging variables.

Code: Select all

<?php
final class utilities{
   var $errors     = array();
   var $error_type = '0';
   var $alltypes   = array();
   var $type       = array();
   var $statement  = '';
   var $success    = 0;
   var $elements   = 0;

   public function __construct(){}
   public function __destruct(){}
   
   public function GetAssetTypes(){
      try{
         $sql    = "SELECT * FROM `AssetTypes` ORDER BY `Description`;";
         $result = mysql_query($sql);
         $count  = mysql_num_rows($result);
         $i=0;
         while($type = mysql_fetch_array($result)){
            $alltypes[$i] = $type;
            $i++;
         }
         
//         while ($i < $count) {
//           $ID = mysql_result($result,$i,"ID");
//           $Description = mysql_result($result,$i,"Description");
//           $type['ID'] = $ID;
//           $type['Description'] = $Description;
//           $alltypes[] = $type;
//           $i++;
//         }
         $this->success = $count;
         $this->statement = $sql;
         $this->elements = count($alltypes);
         return$this->alltypes; 
      }catch(Exception $e){
         $this->errors[]   = $e->getMessage();
         $this->error_type = '1';
         return;
      }         
   }
}
?>
When I include some debugging code in the calling routine, the properties for counts, etc all return what I think they would, however the array for alltypes has an upper boundry of zero and thus contains no elements.

my calling routine contains the following code:

Code: Select all

   include("class_lib/utilities.php");
    .
    .
    .
    .
   if($_SESSION['AssetTypes']==''){
      $utility = new utilities;
      $_SESSION['AssetTypes'] = $utility->GetAssetTypes();
      $debug = count($utility->alltypes);
      $count = $utility->success;
   }
Obviously I'm screwing something up but can't for the life of me figure it out. Can someone help me out. Also I know there is a special forum for code critique but any pointers for a newbie are appreciated.

Re: Referencing class properties

Posted: Sun Mar 27, 2011 9:32 pm
by dgreenhouse
This should be:
while($type = mysql_fetch_array($result)){
$alltypes[$i] = $type['some_field_name'];
$i++;
}