Jump to content

Can anyone see the error in my code? Thanks


spires

Recommended Posts

Hi guys.

 

I'm just learning OOP for the first time, I'm trying to create a user class (see below). But I can't seem to get it to work, I know there's an error, but my server is set up so it doesn't show any error.

 

I would really appreciate any help

 

Class

<?PHP
// Users Class

require_once('database.php');


class User {

public $id;
public $username;
public $password;
public $first_name;
public $last_name;



//////	
public static function find_all() {
return = self::find_by_sql("SELECT * FROM photo_gallery_users");
}


//////	
public static function find_by_id($id="") {
global $database;
$result_array = self::find_by_sql("SELECT * FROM photo_gallery_users WHERE photo_id='{$id}' LIMIT 1");
return !empty($result_array) ? array_shift($result_array) : false;
}


//////	
public static function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set)) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}			



//////
private static function instantiate($record) {
$object = new self;
foreach($record as $attribute=>$value) {
if($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}

return $object;
}



//////
private function has_attribute($attribute) {
$object_vars = get_object_vars($this);
return array_key_exists($attribute, $object_vars);
}
}


?>

 

 

 

HTML

<?PHP
require_once("../includes/database.php");
require_once("../includes/user.php");


// record or row
$user = User::find_by_id(1);
echo $user->id;

echo "<hr />";

$users = User::find_all();
foreach ($users as $user) {
	echo "ID: " . $user->id . "<br>";
	echo "user: " . $user->username . "<br>";
}

 

 

Thanks :)

Link to comment
Share on other sites

Hi Thanks

 

I didn't know I could do that to see the errors.

 

Anyway, I fix one of the bugs, but I am left with a new one.

 

 

 

Error 1: Notice: Trying to get property of non-object in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 9

 

Error 2: Warning: Invalid argument supplied for foreach() in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 14

 

$user = User::find_by_id(1);
echo $user->id;

echo "<hr />";

$users = User::find_all();
foreach($users as $user) {
echo "User: ". $user->username ."<br />";
echo "Name: ". $user->first_name ."<br /><br />";
}

 

Error 1 points to : echo $user->id;

Error 2 points to : foreach($users as $user) {

 

 

Any help on this would be great as i'm completely stuck.

 

Thanks

Link to comment
Share on other sites

Error 1: Notice: Trying to get property of non-object in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 9

Error 2: Warning: Invalid argument supplied for foreach() in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 14

 

The first error points out that $user is not an object. Try var_dump($user);

 

The second error is because of:

 

return = self::find_by_sql("SELECT * FROM photo_gallery_users");

 

Which should be:

 

return self::find_by_sql("SELECT * FROM photo_gallery_users");

 

 

Link to comment
Share on other sites

Hi

 

First, I really appreciate your help on this, I never would have got this far with you. :)

 

Anyway.

var_dump($user); is outputting:

array(10) {

[0]=> string(1) "1" ["photo_id"]=> string(1) "1" [1]=> string(6) "spires" ["photo_user"]=> string(6) "spires" [2]=> string(7) "ss16" ["photo_pass"]=> string(7) "ss16" [3]=> string(4) "Jeff" ["photo_Fname"]=> string(4) "Jeff" [4]=> string(6) "Spires" ["photo_Lname"]=> string(6) "Spires" }

 

 

The 2nd error: I have removed the = but I am still getting the same error.

 

Any more ideas would be great.

 

 

Link to comment
Share on other sites

Hi

 

First, I really appreciate your help on this, I never would have got this far with you. :)

 

Anyway.

var_dump($user); is outputting:

array(10) { [0]=> string(1) "1" ["photo_id"]=> string(1) "1" [1]=> string(6) "spires" ["photo_user"]=> string(6) "spires" [2]=> string(7) "ss165le" ["photo_pass"]=> string(7) "ss165le" [3]=> string(4) "Jeff" ["photo_Fname"]=> string(4) "Jeff" [4]=> string(6) "Spires" ["photo_Lname"]=> string(6) "Spires" }

 

 

The 2nd error: I have removed the = but I am still getting the same error.

 

Any more ideas would be great.

 

 

 

change $users->id to $users['id']

 

Second do a var_dump($users); before foreach ($users as $user)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.