0

I am trying to set up a simple MVC framework for my PHP script. Model ('conference'):

  public function getConferenceLogs($pin){

    $stmt_conferences = $this->db->prepare(
      'SELECT
      date_created,
      timediff(date_completed, date_created) AS duration,
      RecordURL,
      conference_sid
      FROM
      conference
      WHERE
      pin=:pin');
    $stmt_conferences->bindParam(':pin', $pin);
    $stmt_conferences->execute();

    $count =  $stmt_conferences->rowCount();
    if ($count !== 0) {
      $row = $stmt_conferences->fetch();
      return $row;
    }

    return false;
  }

Controller:

function conference_history() 

{        
    Auth::handleLogin();

    $this->loadModel('conference');
    $row = $this->model->getConferenceLogs(Session::get('user_pin'));
    $this->view->row = $row;
    $participant = $this->model->getParticipantLogs(Session::get('user_pin'));
    $this->view->participant = $participant;

    $this->view->render('account/conference_history');
}

View:

<?php 

 var_dump($this->row);    

?>

I know this query in the model should return at least 7 records, when executed outside an MVC framework in a conventional format (https://stackoverflow.com/a/20275597/2429989)

However, a var_dump($this->row) gives the following results:

object(stdClass)[5]
  public 'date_created' => string '2013-10-29 19:20:37' (length=19)
  public 'duration' => string '00:03:42' (length=8)
  public 'RecordURL' => string '' (length=0)
  public 'conference_sid' => string '1c540158-40cf-11e3-b4cc-81e296145de2' (length=36)

Which I assume means there is only one record? I want to display all 7 records; have I set up the query wrong or is it my echo statement? I want to display all 7 records for each property (e.g. print $this->row->date_created).

Community
  • 1
  • 1
alias51
  • 8,178
  • 22
  • 94
  • 166

1 Answers1

2

Looking at your var_dump, $this->row is just an object and it cannot be casted to a string. So, you'll want to access the public properties as such:

<?php
print $this->row->date_created;
...//repeat as needed
?>

Or if you have more than one row object in some sort of collection

<?php
foreach($this->row_collection->row as $row){
    print $row->date_created;
...//blah blah blah
}
Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
  • Thanks, if there is more than one entry for $this->row, how do I print that? (e.g. two dates for two different conferences returned from the query) – alias51 Nov 29 '13 at 19:54
  • Well, I can't speak for the exact structure of your data, but you could pretty easily just loop over it. I'll edit my answer. – Zarathuztra Nov 29 '13 at 19:57
  • Thanks, the structure of the data is in the question (this is the entirety); what does row_collection represent? – alias51 Nov 29 '13 at 19:59
  • Well, like I said, I don't know the exact structure of your data as though there would be multiple rows, so it's just my assumption that you've collected each row into a collection of some sort :) – Zarathuztra Nov 29 '13 at 20:00
  • Sorry, I don't understand. The data collected in $row is shown in the model query in the question, isn't it? – alias51 Nov 29 '13 at 20:03
  • Oh, you mean the other elements in the row? Just use their property name as I did with date_created, so the next one would be $this->row->duration, and so on. – Zarathuztra Nov 29 '13 at 20:05
  • No, I mean the query in the question returns more than one row from the database (each of which has different property names you describe) – alias51 Nov 29 '13 at 20:08
  • Would you mind editing your question to show an example? Sorry, I'm rather confused as to what you are now referring to. – Zarathuztra Nov 29 '13 at 20:09
  • 1
    Ah ok, I see what you're getting at. Here is the deal, you only get one property of that name per object, so in order to have 7 "duration" properties you'd need 7 "row" objects to loop over and collect the data in the way mentioned above. – Zarathuztra Nov 29 '13 at 20:18
  • So 7 queries? Would you be able to show me an example please? – alias51 Nov 29 '13 at 20:21
  • Consider this your homework assignment :) Basically take what I've said above and roll it all into one unified scheme. The foreach loop is your friend in this case, it's awesome for iterating over table rows returned in multiple. – Zarathuztra Nov 29 '13 at 20:23
  • Ok, should I iterate in the model, view, or controller? – alias51 Nov 29 '13 at 20:26
  • Well, in this case I'd say the view, just pass the row objects on. – Zarathuztra Nov 29 '13 at 20:27
  • Then the query would need to be in the view? – alias51 Nov 29 '13 at 20:34
  • lol no, the query should still be in your model, along with any special processing you might need in the model or controller. Then, when you render your view just pass in the row object or collection of objects, and have fun ;) – Zarathuztra Nov 29 '13 at 20:36