Page 1 of 1

Need Help With Using A Class

Posted: Thu May 04, 2006 1:30 pm
by snowrhythm
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


***I made a class called ContactData, and it looks like this:***

Code: Select all

class ContactData
  {
	 
         var $firstName;
         var $lastName;
         var $title;

    function PutContactData($contactArray)
    {
        $this->firstName = $contactArray["firstName"];
        $this->lastName = $contactArray["lastName"];
        $this->title = $contactArray["title"];
    }

  }

 ***And then i have a database connection in another file that loads up an array with DB info and passes it to the PutContactData function. Like this:***


# Grab the Contact info for the given location
			$tmp = $rowLocations["LocationID"];
			$queryContacts = odbc_exec($odbc, "SELECT FirstName, LastName, Title FROM Contacts WHERE LocationID = $tmp") or die (odbc_errormsg());
			while($rowContacts = odbc_fetch_array($queryContacts))
			{
        #Create empty class for contact data
        $contactData = new ContactData();      

			  # Store Contact Data in Class
			  $contactData->PutContactData($rowContacts);


***and then in another file i try to display the info in the ContactData variables like this (the ContactData:***

Code: Select all

if (is_array ($contactData))
    {
         foreach ($contactData as $adata ) 
        { 
           echo '<div class="container3">'.$adata->firstName.''.$adata->lastName.'('.$adata->title.')</div>'; 
        }
		
    } 
         else 
        {
	   echo '<div class="container3">No Contacts</div>';
	 }

***none of the contacts get printed out to the screen....however i have a similar class (actually it's exactly the same with different names for functions and variables) that puts out locations but i don't have it in a foreach loop (because location is kind of like the parent to contact, so contacts needs to be looped within location) and it displays fine. does the class not like being renamed to $adata in the foreach loop?

any help is appreciated!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu May 04, 2006 3:46 pm
by santosj
Iterators in PHP 4 for classes don't exist, it is a Zend Engine 2 feature and available in PHP 5. However that said, you could return the array from the class function and then loop over that.

I however make the following assumptions:

1. That you are using PHP 4 for the class.
2. That $contactData is still a class variable in the other file.

Posted: Thu May 04, 2006 4:38 pm
by Christopher
Since what you want is an array of Value Objects, how about:

Code: Select all

# Grab the Contact info for the given location
                        $tmp = $rowLocations["LocationID"];
                        $queryContacts = odbc_exec($odbc, "SELECT FirstName, LastName, Title FROM Contacts WHERE LocationID = $tmp") or die (odbc_errormsg());
                        $contactData = array();
                        while($rowContacts = odbc_fetch_object($queryContacts)) {
                             $contactData[] = $rowContacts;
                        }
                        return $contactData;
PS - get rid of that die() and just have the caller of this method deal with getting no records.