Hello I am new to OOP and I have a class with a method and I am trying to return a variable to the class from a method but I am obviously missing something...
Here is my Class:
class Certification {
public $uid;
public $men_id;
public $sm_id;
public $sm_rec;
public $three_days;
public $min_on_test;
public $signed;
public $mid_year;
public $police_check;
public $io_comments;
public $date_certified;
public function __construct($uid)
{
include 'conn.php';
$stmt = $conn->prepare(
'SELECT * FROM certification WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->uid = $row['uid'];
$this->nid = $row['nid'];
$this->nid = $row['date_certified'];
$this->men_id = $row['men_id'];
$this->sm_id = $row['sm_id'];
$this->sm_rec = $row['sm_rec'];
$this->three_days = $row['three_days'];
$this->min_on_test = $row['min_on_test'];
$this->signed = $row['signed'];
//$this->mid_year = $row['mid_year'];
$this->police_check = $row['police_check'];
$this->io_comments = $row['io_comments'];
}
}
public function get_mid_year ($uid) {
include 'conn.php';
$stmt = $conn->prepare(
'SELECT courseReviewed FROM vti_users WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->mid_year = $row['courseReviewed'];
return $this->mid_year;
}
}
}
I thought that this method would return the $mid_year variable to the class but is null when I use var_dump().
Any advice about correcting my flawed logic would be a great help for a beginner.
Related
I am trying to retrieve the data from database using OO-PHP
Here is my code
user.php
<?php
require_once("init.php");
class User{
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
public static function find_all_users(){
return self::find_this_query("SELECT * FROM users");
}
public static function find_user_by_id($user_id){
global $database;
/* $result_set = $database->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");*/
$result_set = self::find_this_query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
$found_user = mysqli_fetch_array($result_set);
return $found_user;
}
public static function find_this_query($sql){
global $database;
$result_set = $database->query($sql);
$the_object_array = array();
while($row = mysqli_fetch_array($result_set)){
$the_object_array[] = self::instantation($row);
}
return $result_set;
}
public static function instantation($the_record){
$the_object = new self;
/* $the_object->id = $found_user['id'];
$the_object->username = $found_user['username'];
$the_object->password = $found_user['password'];
$the_object->first_name = $found_user['first_name'];
$the_object->last_name = $found_user['last_name'];*/
foreach($the_record as $the_attribute => $value){
if($the_object->has_the_attribute($the_attribute)){
$the_object->$the_attribute = $value;
}
}
return $the_object;
}
private function has_the_attribute($the_attribute){
$object_properties = get_object_vars($this);
return array_key_exists($the_attribute, $object_properties);
}
}
?>
database.php
public function query($sql){
/*$result = mysqli_query($this->connection,$sql);*/
$result = $this->connection->query($sql);
$this->confirm_query($result);
return $result;
}
index.php
$users = User::find_all_users();
foreach($users as $user){
echo $user->username;
}
When trying to retrieve the users from the database I get the error
Trying to get property of non-object
The problem is, you are not returning the users array from this method
public static function find_this_query($sql){
global $database;
$result_set = $database->query($sql);
$the_object_array = array();
while($row = mysqli_fetch_array($result_set)){
$the_object_array[] = self::instantation($row);
}
return $result_set;
}
instead you should return the array as
public static function find_this_query($sql){
global $database;
$result_set = $database->query($sql);
$the_object_array = array();
while($row = mysqli_fetch_array($result_set)){
$the_object_array[] = self::instantation($row);
}
return $the_object_array;
}
and just in case the result is empty, check for it as
$users = User::find_all_users();
if($users != null){
foreach($users as $user){
echo $user->username;
}
}
I'm trying to make this code access a certain row in the database and pull "title" from it to update public $Title in the UserBlog class, but, it's not doing so.
The database connection is definitely working as it worked procedually, but OOP is messing it up.
$ID = $db->real_escape_string(strip_tags(stripslashes($_GET['ID']))); // Page ID
class UserBlog
{
public $Title;
public $BannerImage;
public $ID;
public function GenerateBlog() {
$FetchBlogDetails = mysqli_query($db, "SELECT * FROM UserBlogs WHERE ID = '$this->ID'");
$FBL = mysqli_fetch_object($FetchBlogDetails);
$this->Title = $FBL->Title;
}
public function FetchBlog() {
return $this->Title;
return $this->ID;
}
};
$GetBlog = new UserBlog();
$GetBlog->ID = $ID;
$GetBlog->GenerateBlog();
echo $GetBlog->Title;
echo $GetBlog->ID;
$db doesn't exist in the scope of your method. You could inject it when calling the method.
class UserBlog
{
public $Title;
public $BannerImage;
public $ID;
public function GenerateBlog($db) {
$FetchBlogDetails = mysqli_query($db, "SELECT * FROM UserBlogs WHERE ID = '$this->ID'");
$FBL = mysqli_fetch_object($FetchBlogDetails);
$this->Title = $FBL->Title;
}
//...
}
$GetBlog = new UserBlog();
$GetBlog->ID = $ID;
$GetBlog->GenerateBlog($db);
I have a simple class that is not outputting:
class ICRR extends Profile {
public $recommendation_text;
public $comments;
public $men_id;
public $recommendation;
public $uid;
public function __construct($uid)
{
include 'con.php';
$stmt = $conn->prepare(
'SELECT * FROM icrr WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->men_id = $row['men_id'];
$this->uid = $row['uid'];
$this->nid = $row['nid'];
$this->recommendation = $row['reccomendation'];
$this->comments = $row['comments'];
$this->date_submitted = $row['date_submitted'];
}
}
public function recommendation_text() {
switch ($this->recommendation){
case 1 :
$this->recommendation = 'Yes Certify';
break;
case 2 :
$this->recommendation = 'No Do Not Certify';
break;
case 3 :
$this->recommendation = 'Certify Pending Requirements';
break;
default : echo "Recommendation Not Received";
}
return $this->recommendation_text;
}
}
When I ask:
$ICRR = new ICRR($uid);
var_export($ICRR);
Everything is null. I checked the query and it is returning the data fine... I used a class like this before and it worked fine so I think I am missing something I just cant see..
Hi I have a couple of php classes. And I am passing a connection to a MySql database.
When I make an instance of the firs class and try to call the invoice class with the method getinvoicedetails() I get error "Command out of sync; you can't run this command".
Any help wil be appreciated.
Here is the code:
<?php //Invoicectrl class
class invoicectrl
{
// define properties
public $invoices = Array();
public $connection;
public $userID;
public $ownerID;
// constructor
public function __construct($userIDin,$ownerIDin) {
$this->userID = $userIDin;
$this->ownerID = $ownerIDin;
}
//function to set connection to db
public function setconnection($conn){
$this->connection = $conn;
}
public function getinvoicelist(){
$res = Array();
$invIDs = Array();
//Get list of invoicenumbers and invoiceIDs
$queryd = "CALL sp_getOwnersInvNumIDs (".$this->ownerID.")";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
// GOING THROUGH invoices and getting there data
$o=0;
while($row = $result->fetch_assoc()) {
$invIDs[$o]['ID'] = $row['invoiceID'];
$invIDs[$o]['NR'] = $row['invoicenumber']; }
$result->free();
//Set each invoice own info
for($i=0;$i<=count($invIDs)-1;$i++){
$res[$i] = new invoice($invIDs['ID'],$invIDs['NR']);
$res[$i]->setconnection($this->connection);
$res[$i]->getinvoicedetails();
}
return $res;
}
}
?>
And here is the invoice class
<?php //Invoice class
class invoice
{
// define properties
public $invoiceID;
public $invoicenumber;
public $itemIDs=Array();
public $items= Array();
public $customerID;
public $cust_company;
public $cust_name;
public $cust_email;
public $cust_adress1;
public $cust_adress2;
public $cust_adress3;
private $connection;
// constructor
public function __construct($invoiceIDin,$invoicenumberin) {
$this->invoiceID = $invoiceIDin;
$this->invoicenumber = $invoicenumberin;
}
//function to set connection to db
public function setconnection($conn){
$this->connection = $conn;
}
//sets all invoices details
public function getinvoicedetails(){
//Get customer on invoice
$queryd = "select customerID FROM Accounting_Invoice_Customer where invoiceID =".$this->invoiceID."";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
while($row1 = $result->fetch_assoc()) {
$this->customerID = $row1['customerID'];
}
$result->free();
//Get list of items on invoice
$queryd = "call sp_getItems(".$this->invoiceID.")";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
$o=0;
while($row2 = $result->fetch_assoc()) {
$this->items[$o]['itemID'] = $row2['itemID'];
//$this->items[$o]['itemnumber'] = $row['itemnumber'];
//$this->items[$o]['itemdescription'] = $row['itemdescription'];
//$this->items[$o]['itempricewotax'] = $row['itempricewotax'];
$o++;
}
$result->free();
}
}
?>
When you run a SQL request, you have to get through the whole result or to close the cursore before sending another request.
The best thing to do, here is to get the whole result at first and then traverse it with a foreach loop to be able to send the second request.
Moreover, it should be a good idea to use INNER JOIN in your sql query.
$queryd = "CALL sp_getOwnersInvNumIDs (".$this->ownerID.")";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
$invIDs = $result->fetch_all();
$res = array();
foreach($invIDs as $inv){
$inv = new invoice($invIDs['invoiceID'],$invIDs['invoiceNumber']);
$inv->setconnection($this->connection);
$inv->getinvoicedetails();
$res[] =$inv;
}
I'm having problems with accessing variables from my classes...
class getuser {
public function __construct($id) {
$userquery = "SELECT * FROM users WHERE id = ".$id."";
$userresult = mysql_query($userquery);
$this->user = array();
$idx = 0;
while($user = mysql_fetch_object($userresult)){
$this->user[$idx] = $user;
++$idx;
}
}
}
I'm setting this class in a global 'classes' file, and later on I pass through a user id into the following script:
$u = new getuser($userid);
foreach($u->user as $user){
echo $user->username;
}
I'm hoping that this will give me the name of the user but it's not, where am I going wrong?!
Thanks
please define your users member as public in your class like this
class getuser {
public $user = null;
//...
}
in order to access a class property, you have to declare it public or implement getters and setters (second solution is preferable)
class A {
public $foo;
//class methods
}
$a = new A();
$a->foo = 'whatever';
with getters and setters, one per property
class B {
private $foo2;
public function getFoo2() {
return $this->foo2;
}
public function setFoo2($value) {
$this->foo2 = $value;
}
}
$b = new B();
$b->setFoo2('whatever');
echo $b->getFoo2();
in your example:
class getuser {
private $user;
public function __construct($id) {
$userquery = "SELECT * FROM users WHERE id = ".$id."";
$userresult = mysql_query($userquery);
$this->user = array();
$idx = 0;
while($user = mysql_fetch_object($userresult)){
$this->user[$idx] = $user;
++$idx;
}
}
/* returns the property value */
public function getUser() {
return $this->user;
}
/* sets the property value */
public function setUser($value) {
$this->user = $value;
}
}
$u = new getuser($userid);
$users_list = $u->getUser();
foreach($users_list as $user) {
echo $user->username;
}