-1

So I have classes that I would like to work together. My first two establish a connection to the database:


dbconn.php

<?php

class dbconn {
    protected $dbname;
    protected $dbuser;
    protected $dbpassword;
    protected $dbhost;
    protected $connection;

    public function __construct($dbhost, $dbname, $dbuser, $dbpass) 
    {
        $this->dbname = $dbname;
        $this->dbhost = $dbhost;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpass;
        $this->connect();
    }

    public function getConnection()
    {
        return $this->connection;
    }

    protected function connect()
    {
        $this->connection = new PDO("mysql:host={$this->dbhost};dbname={$this->dbname}", $this->dbuser, $this->dbpass);
    }
}
?>
<html>
    <h2>
        Hold My Beer!<br />
        <meta charset="UTF-8">
        <title>Hold My Beer!</title>
    </h2>
    <body>

    </body>
</html>


dblogin.php

<?php

$db = new dbconn('localhost','phpproject','carl','pdt1848?');

?>

My other class is trying to edit items from the database. I tried to link the db connection classes through the __construct of this class, I'm just going about this all wrong apparently.
editbeers.php

<?php
//a couple methods of trying to get this connecting to the db...neither working.


class BeerEditor
{
    private $db;

    function __construct(dbconn $db){
       $this->db = $db;

    }

    function addBeer(Beer $beerObj){
        //making connection to db here
        $conn = $this->db->getConnection();

        $stmt = $conn->prepare("INSERT INTO beers (beer_name, beer_type, beer_abv, beer_rating) VALUES (:beer_name, :beer_type, :beer_abv, :beer_rating)");
        //global $db;
        //$dbconn = $db->getConnection();
        //$stmt = $dbconn->prepare("INSERT INTO beers (beer_name, beer_type, beer_abv, beer_rating) VALUES (:beer_name, :beer_type, :beer_abv, :beer_rating)");

        // // was getting this warning "//Strict Standards: Only variables should be passed by reference in /path/to/file.php on line 123"
        // so i set them to vars
        $getbeer = $beerObj->getBeerName();
        $gettype = $beerObj->getBeerType();
        $getabv = $beerObj->getBeerABV();
        $getrating = $beerObj->getBeerRating();

        $stmt->bindParam(':beer_name', $getbeer);
        $stmt->bindParam(':beer_type', $gettype);
        $stmt->bindParam(':beer_abv', $getabv);
        $stmt->bindParam(':beer_rating', $getrating);

        $result = $stmt->execute();
        print_r($result);
        if($result === false){
            var_dump($stmt->errorCode());
        }

        return $result;
    }

    function listBeers(){

        $conn = $this->dbconn->getConnection();
        $result = $conn->query('SELECT * FROM beers');

        $result->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'beers');
        $beers = $result->fetchAll();

        return $beers;
    }


}
?>


Beer Class

<?php
/**
 * Description of beer
 *
 * @author root
 */
class beer {

    public $beer_name;   //varchar(45)
    public $beer_type;   //varchar(45)
    public $beer_abv;    //decimal(4,2) alcohol percentage ex. 06.50
    public $beer_rating; //char(10) 1 awful beer, 10 life-changing beer


    public function __construct($beer_name = null){
        if ($beer_name !== null){
            $this->setBeerName($beer_name);
        }    
        //defaults
        $this->setBeerType($_POST['beer_type']);
        $this->setBeerABV($_POST['beer_abv']);
        $this->setBeerRating($_POST['beer_rating']);


    }


        public function setBeerName($beer_name){ $this->beer_name = $beer_name; }
        public function getBeerName(){
                return $this->beer_name;
        }

        public function setBeerType($beer_type){ $this->beer_name = $beer_type; }

        public function getBeerType(){
            return $this->beer_type;
        }

        public function setBeerABV($beer_abv){ $this->beer_abv = $beer_abv; }
        public function getBeerABV(){
            return $this->beer_abv;
        }

        public function setBeerRating($beer_rating){ $this->beer_rating = $beer_rating;}
        public function getBeerRating(){
            return $this->beer_rating;
        }
}


addbeer.php

<?php

session_start();

if ($_SESSION['logged_in']!="yes"){
    header ("Location: unauth.php");
    exit();
}
require_once "/home/carlton/public_html/PHPproject/allincludes.php";
?>      
<h4> So you tried a new beer..tell me about it</h4>

    <form action="beeradded.php" method="post">
        Please enter a beer:<br>
        <input name="beer_name" type="text" />
        <br>
        Type of beer:<br>
        <input type="text" name="beer_type">
        <br>
        Beer ABV: <br>
        <input type="number" name="beer_abv">
        <br>
        Rate beer from 1 to 10: <br>
        <input type="number" name="beer_rating"> 
        <br>
 <input type="submit" value='Add Beer' />
</form>

1 Answers1

1

There's several ways to make classes work together but it's not clear what you're trying to do. Here's some general help and maybe this will get you going in the right direction.

First off, you should avoid using superglobals like $_POST within your class. A better way is to inject the $_POST from the top level and just treat it as an array. It makes your class more generic and flexible.

Second, BeerEditor has the database injected in its constructor. That's a best practice so props there. I don't see anything obviously wrong but you're not including where you're instantiating BeerEditor either. It's not clear at all what the beer class is being used for.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • My intention is to send the data entered into to a form into the beer class; then for beer editor to take the information from the beer class and push it to the database. – user3598266 May 05 '14 at 14:21