0

I am new in PHP OOP and was wondering if someone could help me with this.

I have a basic class with one method which returns data from database. Currently I am calling the method which displays everything inside the function.

Here is my class Definition:

class Products{
    //properties
    public $familyName = "";
    public $familyProduct = "";

    //Methods
    public function getFamily($catId){
    global $conn;
    $sql = "SELECT * FROM product_family WHERE catID = '$catId'";
    $result = $conn->query($sql);
    if($result->num_rows > 0){

        while($row = $result->fetch_assoc()){
            echo "<li>".$row['familyName']."</li>";
            echo "<li>".$row['familyProduct']."</li>";
        }
      }
   }
}

Here is how I call the method:

$Products = new Products; 
$Products->getFamily( 4 );

This works however, how can I assign each data coming from database ( ex familyName, familyProduct ) into variables inside class implementation and then access them individually where ever I need to. Something like this:

$Products = new Products; 
$Products->familyName;
$Products->familyProduct;

I have empty properties but I am not sure how can I assign values to them coming from the loop and then return them each.

Thanks,

4
  • In order to access the Class properties within your getFamily function, you will need to use $this (as in, this object): $this->familyName will return the value of it, $this->familyName = 'test' will set the value of it. Commented Oct 25, 2017 at 21:43
  • $this->familyName = $row['familyName'] ? Commented Oct 25, 2017 at 21:43
  • In addition, you may want to add a __construct() function (called the Class constructor) that executes automatically when you create the object like $products = new Products();. So if you have a constructor with a DB query to get information, you can then set the values using $this-> within the Class, and then you could use $products->familyName (or better yet, create a getter and use $products->getFamilyName(); to get the value. Commented Oct 25, 2017 at 21:45
  • 3
    I'm voting to close this question as off-topic because you have not read the documentation: php.net/manual/en/language.oop5.php Commented Oct 25, 2017 at 21:47

1 Answer 1

1

There are view things I would change in your Code.

Don't make Properties public use use Getters and Setters.
This will protect you Object from being used the wrong way e.g. now you can't change the familyName from outside: $products->familyName = "some value" because this would make the data of the object corrupt.

global $conn; is a no go in OOP use the construct of the Object,
in your case $products = new Products($conn);

Now you can set a Cat ID $products->setCatId(4); and read the result
$familyName = $products->getFamilyName(); or $familyProduct = $products->getFamilyProduct();

If you have more than one result you will get an array, if catId will always result one row you can delete this part. If you learn more about OOP you will find out that the hole SQL stuff can be done with a separate Object, but this is off Topic.

class Products
{
    // Properties
    protected $conn;
    protected $catId;
    protected $familyName;
    protected $familyProduct;

    public function __construct($conn)
    {
        $this->conn = $conn;
    }

    // set Cat ID and get date
    public function setCatId($catId)
    {
        $this->catId = (int) $catId;
        $this->getDate();
    }

    public function getCatId()
    {
        return $this->catId;
    }

    // get Family Name
    public function getFamilyName()
    {
        return $this->familyName;
    }

    // get Family Product
    public function getFamilyProduct()
    {
        return $this->familyProduct;
    }

    // get date
    protected function getDate()
    {
        $sql    = "SELECT * FROM product_family WHERE catID = '$this->catId'";
        $result = $this->conn->query($sql);

        // Default if no result
        $this->familyName    = null;
        $this->familyProduct = null;

        // if one Result
        if ($result->num_rows == 1)
        {
            $row                 = $result->fetch_assoc();
            $this->familyName    = $row['familyName'];
            $this->familyProduct = $row['familyProduct'];
        }

        if ($result->num_rows > 1)
        {

            $this->familyName    = [];
            $this->familyProduct = [];

            while ($row = $result->fetch_assoc())
            {
                $this->familyName[]    = $row['familyName'];
                $this->familyProduct[] = $row['familyProduct'];
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot for explanation and taking the time to code. I will review and will put it in a test to see what I get and then will mark your answer as correct one. I think your solution is great.
FYI: There was one line to much in the while loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.