I have created a class Vacancy which has some variables :
public $salary;
public $id;
...

I am looping through the result of a query and creating a vacancy for each match
Then I adding the vacancy to an array.

$tempVacancy=new Vacancy();
$tempVacancy->salary = mysql_result($result,$i,"salary");
$tempVacancy->id =mysql_result($result, $i, "yid");

//Then when i what to put the created vacancy in an array something happens $vacancies[$i] = $tempvacancy; <===============Somehow this is not working because

when i try to access vacancies[$i] it is giving the following error:

Fatal error: Call to a member function printVacancy() on a non-object in C:\wamp\www\home\search.php on line 210

Is there a special way to create an array of objects in php??

Thanks in advance!

Well, if the code you provided is what you're using, then the problem is simply that the "V" is capital in $tempVacany in the top code, but when you are putting it into the array you wrote $tempvacany - which is not the same thing.

Can you verify that this is not the issue?

No no it's not the issue.. this is just to show u my code logic..

Can you post some more of the code (like the loop where $i is assigned/used and where the call happens on line 210)?

This is the Vacancy Class

class vacancy {
   	public $vacancyid; 
	public $salary; 
	public $workinghours; 
	public $major; 
	public $company; 
	public $requirements; 
	public $jobdescription;
	public $type;
	
	function getId()
	{
		return $this->vacancyid;
	}
	function printVacancy()
	{
		echo "<table width=\"600\" height = \"60\" bordercolor= \"#33CCCC\" border= \"1\" bgcolor= \"#FFFFCC\"><tr>";
		echo "<td>
		<h2>$this->company</h2>
		 </td>
		<td><p><strong>Job Description: </strong>$this->jobdescription <br> 
			<strong>Type: </strong>$this->type <br>
			<strong>Requirements: </strong>$this->requirement<br>
			<strong>Salary: </strong>$this->salary<br>
			<strong>Working Hours: </strong>$this->workinghour<br> 
			<strong>Major: </strong>$this->major <br>
			<strong>Vacancy Number: </strong>$this->vacancyid </p>";
		echo "</td></tr></table>";
   			
	}
}

This is the code where I am creating the vacancy (from the mySQL database) and "attempting" to insert it to the array $vacancies

$vacancies = array();
if (isset($_POST['vacancyid']) && $_POST['vacancyid'] !== '')
{
	$result = mysql_query("select * from vacancy where vacancyid = '$vacancyid'", $db);
	$i=0;
	while ($i < mysql_numrows($result)) 
	{
		$tempVacancy=new Vacancy();
		$tempVacancy->company = mysql_result($result,$i,"company");
		$tempVacancy->jobdescription = mysql_result($result,$i,"description");
		$tempVacancy->requirement = mysql_result($result,$i,"requirement");
		$tempVacancy->salary = mysql_result($result,$i,"salary");
		$tempVacancy->workinghour = mysql_result($result,$i,"workinghour");
		$tempVacancy->major =mysql_result($result,$i,"major");
		$tempVacancy->type =mysql_result($result, $i, "type");
		$tempVacancy->vacancyid =mysql_result($result, $i, "vacancyid");
		$vacancies[] = $tempvacancy;		
		$i++;
		
	}
}

And this is the statement that is causing the following error:
Fatal error: Call to a member function printVacancy() on a non-object in C:\wamp\www\home\search.php on line 211 $vacancies[0]->printVacancy();

OH CRAPPPP!!!! YOU WERE RIGHT THE FIRST TIME!!
I can't believe i just wasted 4 hours because over capitalization issue!!
THANK YOU SO MUCH!!

No worries - and glad I caught it. I do the same thing from time to time too and I'm sure it happens to everyone! Glad it's working now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.