1

I'm a little bit stumped. I've never messed around with objects and classes too much in PHP, but someone recommended that I re-did some code with it.
What I'm trying to do is make $auctions an object property, while saving all of the row data to it.

Right now, I do echo $auctions[1]['title']; to echo out the listing where id=1 title. And I wish to re-create it so that it would be an object.

Here's my current code,

$sqlquery = "SELECT * FROM auctions";
if ($result = $db->query($sqlquery)) {
    while ($row = $result->fetch_assoc()) {
        $auctions[$row['id']]['id'] = $row['id'];
        $auctions[$row['id']]['title'] = $row['title'];
        $auctions[$row['id']]['featured_image'] = $row['featured_image'];
        $auctions[$row['id']]['description'] = $row['description'];
        $auctions[$row['id']]['date'] = $row['date'];
        $auctions[$row['id']]['location'] = $row['location'];
        $auctions[$row['id']]['highlights'] = $row['highlights'];
        $auctions[$row['id']]['catagories'] = $row['catagories'];
        $auctions[$row['id']]['notes'] = $row['notes'];
        $auctions[$row['id']]['terms'] = $row['terms'];
        $auctions[$row['id']]['contact'] = $row['contact'];
    }
}

I don't have any idea on how to accomplish this, but if someone could give me a little hint to point me in the direction, it would be very appreciated! :)

5
  • 1
    Why not $auctions[$row['id']] = $row? Commented Nov 23, 2014 at 18:19
  • 1
    While you don't know what classes are, don't try to use it. You can't to get some profit from feature, that you can't use. Learn, understand what is it, understand why is it, then use. Commented Nov 23, 2014 at 18:23
  • You should think about using an ORM. Commented Nov 23, 2014 at 18:25
  • You may wish to look into an ORM such as Maphper or PHPDataMapper to reduce a lot of the code. Commented Nov 23, 2014 at 18:43
  • 1
    @vp_arth I learn best from reading code, and knowing what it will do. This would help me understand it. Commented Nov 23, 2014 at 19:30

5 Answers 5

2

Create a class auctions with all the needed member variables that you listed above (e.g. id, title, feature_image etc.). Next create a setter method (e.g. setValues()) inside the class that can accept the $row.

$sqlquery = "SELECT * FROM auctions";
$auction = new Auctions();
if ($result = $db->query($sqlquery)) {
    while ($row = $result->fetch_assoc()) {

       $auction->setValues( $row );
       // do something with $auction...
    }
}

Instead of a explicit setter method, You may also use magic method __set().

Sign up to request clarification or add additional context in comments.

Comments

1

I'll write a minimal snippet here now:

First let create a base class for all our models:

abstract class Model() {
  public $fields = array();
  private $data = array();
  public function setValues(array $vals) {
    foreach($vals as $key=>$value) {
      if (in_array($key, static::$fields)) {
        $this->data[$key] = $value;
      }
    }
  }
  public function get($key) {
      if (in_array($key, static::$fields) && isset($this->data[$key])) {
        return $this->data[$key];
      }
      return null; // or throw Exception)
  }
}

Next, create some concrete model:

class Users extends Model {
  public static $fields = array('id', 'name');
}

And we can use it now:

$users = array();
$sqlquery = "SELECT * FROM Users";
if ($result = $db->query($sqlquery)) {
    while ($row = $result->fetch_assoc()) {
      $user = new User();
      $user->setValues($row);
      $users[] = $user;
    }
}

You can to add some user-specific methods (aka login) to User model directly..

Also you should to implement other Model methods, like getById, getByQuery, save and other, and no use direct sql queries, because models can do this itself

1 Comment

Also be careful. Creating models in a loop, as here, is not recomended, because its very heavy. Guys create a Collection entity for this case usually. It can store list of data, and iterate through it with a Models.
0

You can store the values in a object like

$obj = new stdClass; //create new standard class object

$obj->id = $row['id']; //assign property value
$obj->title = $row['title'];
//further properties

... and so on

Comments

0

You really are trying to create an array of objects (instances of a type containing info for one auction. Something like this:

class Auction
{
    var $id = null;
    var $title = null;
    var $featured_image = null;
    var $description = null;
    var $date = null;
    var $location = null;
    var $highlights = null;
    var $catagories = null;
    var $notes = null;
    var $terms = null;
    var $contact = null;
}

$sqlquery = "SELECT * FROM auctions";
if ($result = $db->query($sqlquery)) {
    while ($row = $result->fetch_assoc()) {
        $newAuction = new Auction();
        $newAuction->id = $row['id'];
        $newAuction->title = $row['title'];
        $newAuction->featured_image = $row['featured_image'];
        $newAuction->description = $row['description'];
        $newAuction->date = $row['date'];
        $newAuction->location = $row['location'];
        $newAuction->highlights = $row['highlights'];
        $newAuction->catagories = $row['catagories'];
        $newAuction->notes = $row['notes'];
        $newAuction->terms = $row['terms'];
        $newAuction->contact = $row['contact'];
        $auctions[$row['id']] = $newAuction;
    }
}

Please note that you have misspelled "categories" (you have "catagories").

4 Comments

what profit from this class? can you though add setValues(array $row) method?
@vp_arth I'm not sure I know what you're asking.
I asking about What make this object different from the array used before? -> instead of []?
It's different because it's an object, not an array. I'm recommending that the OP build an array of objects, not a multidimensional associative array. I think that much is obvious, so I'm not sure what the point of your question is. I also don't understand why you are asking about a setValues method. That wasn't part of the question, and there is no such method in the class that I have defined in my answer, so no, you can't use that method.
0

I advice you to use PDO

class Auction
{
    public $id;
    public $title;
    public $featured_image;
    public $description;
    public $date;
    public $location;
    public $highlights;
    public $catagories;
    public $notes;
    public $terms;
    public $contact;

    // This will return $all being an array of objects of class Auction
    public static function getAll() {
        $query = "SELECT * FROM auctions";

        $statement = $db->prepare($query);
        if (!$statement->execute())
            return false;
        $all = $statement->fetchAll(PDO::FETCH_CLASS, "Auction");
        return $all;
    }
}

Comments

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.