2 Replies - 482 Views - Last Post: 07 May 2012 - 04:48 PM Rate Topic: -----

#1 Anthei  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-April 12

Fatal error calling function to view database values

Posted 07 May 2012 - 04:10 PM

My final in my PHP class is due tomorrow, and I have it pretty much done, aside from one vital issue. But no one's answering my plea for help on the school's forums. The project is a registration form for users to input their information directly into a database. After submitting the data, it's optional for the user to view all the users in the database. And that's where I'm having issues. The page to view all users is getting the Fatal error: Fatal error: Call to a member function getfirst_name() on a non-object in /home/a4238680/public_html/labs/labFinal/view/list_all.php on line 15.

Here is my web page, and here is the page I get the error on. I have way too many files to put all of the code here, but I'll display the code for list_all.php and the file that has the functions, registrant.php, and include the complete file set and structure in an attached file. Attached File  labFinal.zip (7.44K)
Number of downloads: 21

list_all.php
<?php include 'header.php'; ?>
	<div id="main">
		<div id="content">
			<!-- display a table containing users -->
		<h2>All Users</h2>
			<table>
				<tr>
				<th>First Name</th>
				<th>Last Name</th>
				<th>E-mail</th>
				<th>Gender</th>
				</tr>
				<?php foreach ($users as $user) : ?> 
				<tr>
				<td><?php echo $user->getfirst_name(); ?></td>
				<td><?php echo $user->getlast_name(); ?></td>
				<td><?php echo $user->getemail(); ?></td>
				<td><?php echo $user->getgender($value); ?></td>
				<td>
				<form action="index.php" method="get">
				<p><input type="hidden" name="action" value="delete" /></p>
				<p><input type="hidden" name="id" value="<?php echo $user->getuser_id(); ?>" /></p>
				<p><input type="submit" value="Delete" /></p>
				</form>
				</td>
				</tr>
				<?php endforeach; ?>
			</table>
			<form action="index.php" method="get">
			<p><input type="hidden" name="action" value="reset" /></p>
			<p><input type="submit" value="Add User" /></p> <!-- Visitor = User -->
			</form>
		</div><!-- end content -->
	</div><!-- end main -->
<?php include 'footer.php'; ?>


registrant.php
<?php

class Registrant {
	const MALE = 'm';
	const FEMALE = 'f';
	
	private $user_id, $first_name, $last_name, $email, $gender; 

	function __construct($user_id, $first_name, $last_name, $email, $gender) {
		$this->user_id = $user_id;
		$this->first_name = $first_name;
		$this->last_name = $last_name;
		$this->email = $email;
		$this->gender = $gender;
	}
	public function getuser_id() {
		return $this->user_id;
	}
	public function setuser_id($value) {
		$this->user_id = $value;
	}
	public function getfirst_name()	{
		return $this->first_name;
	}
	public function setfirst_name($value) {
		return $this->first_name = $value;
	}
	public function getlast_name() {
		return $this->last_name;
	}
	public function setlast_name($value) {
		$this->last_name = $value;
	}
	public function getemail() {
		return $this->email;
	}
	public function setemail($value) {
		return $this->email = $value;
	}
	public function getgender()	{
		return $this->gender;
	}
	public function setgender($value)	{
		if ($value == self::MALE || $value == self::FEMALE) { 
			$this->gender = $value; 
		}
	}
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error calling function to view database values

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Fatal error calling function to view database values

Posted 07 May 2012 - 04:12 PM

Wherever this $users object

<?php foreach ($users as $user) : ?> 


gets populated, it's not getting populated properly.

$user_information = new Registrant($row['user_id'], $row['first_name'], $row['last_name'], $row['email'], $row['gender']);
		$user_information->setUser_id($row['user_id']);


If you're passing the user_id to the Registrant constructor, why would you need a separate call to setUser_id?
Was This Post Helpful? 0
  • +
  • -

#3 Anthei  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-April 12

Re: Fatal error calling function to view database values

Posted 07 May 2012 - 04:48 PM

I must've put it in by mistake! Whoops. However, omitting the code does not effect the issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1