Get a data array from controller into view.php? (Profiles single page.)

Permalink
I'm sure the answer here is obvious, but for some reason I'm not getting it...

I'm trying to build a new-messages summary into the main profile view. So far, I've added a function into controllers\single_pages\profile.php with a db->getAll query that outputs a table in which each row has a message subject, sender firstname, and sender lastname (names are custom user attributes) for unread messages for the current logged-in user.

I think this function works, insofar as the array'll print_r:

Array ( [0] => Array ( [msgSubject] => Hi, Daisy! [ak_firstname] => Vivien [ak_lastname] => Smith ) [1] => Array ( [msgSubject] => Do you have... [ak_firstname] => Steve [ak_lastname] => Gray ) )


Problem is, I can't figure out how to get that array into a $this->set statement.

Or maybe I'm going about it wrong... Any chance one of y'all wizards can bail me out?

diorist
 
ScottC replied on at Permalink Reply
ScottC
I'd first make sure your controller is in fact running, which it seems like it is since you are seeing the print_r.

literally from your controller override you should see whatever you have set.. though me being a guessing man i think you haven't copied concrete/single_pages/login.php to ~/single_pages/login.php and cleared cache.

I hope that helps.
diorist replied on at Permalink Reply
diorist
I'm working with profiles, not login, and the profile view file is in [root]\single_pages\profile\view.php. (Pls pardon my backwhacks: I dev on xampp.)

The controller /must/ be running because the print_r (and other custom functions in the controller) display immediately on reloading the frontend profile page. Caching is turned off entirely.

So, I don't think those are the answers. But thanks for your suggestions.
diorist replied on at Permalink Reply
diorist
Just to clarify: I know I'm missing the piece of code that takes my array and puts it into a return() (or whatever is needed) to pass it to the view.php.

Right now, I have:

1. a custom function "getMsgInfo()" in profile.php
2. $this->set('msgInfo',$this->getMsgInfo()); in profile.php public function view
3. $message2 = "$msgInfo"; echo $message2; in view.php

So I think I need the part that gets my db output (array) from 1 > 2.

(Unless you can't get there from there; in which case, I'd love some guidance on how I should instead get the data into view.php.)

Thanks!
JohntheFish replied on at Permalink Reply
JohntheFish
Controller view method:
$my_data = array(......... etc.);
$this->set('my_data', $my_data);


View:
$something = $my_data;


Have a look at
http://www.concrete5.org/documentation/how-tos/developers/concrete5...
for some debug snippets.

For example, in your View:
echo "<pre>";
echo "********************\n";
echo htmlentities(print_r($my_data,true));
echo "\n====================";
echo "</pre>";
JohntheFish replied on at Permalink Reply
JohntheFish
PS,
You can also use a similar debug clip in the controller view method to check it is running.
echo "<pre>";
echo "*****In the Controller View Method*****\n";
echo htmlentities(print_r($my_data,true));
echo "\n====================";
echo "</pre>";
diorist replied on at Permalink Reply
diorist
I'm trying to follow your meaning, but still missing something. Now I have a new function in the profiles controller:

function getMsgInfo() {
      $u = new User();
      $uID = $u->getUserID();
      $db = Loader::db();
      $msgDat = array();
      $mq = $db->getAll("SELECT UserPrivateMessages.msgSubject, UserSearchIndexAttributes.ak_firstname, UserSearchIndexAttributes.ak_lastname FROM UserPrivateMessages JOIN (UserPrivateMessagesTo JOIN UserSearchIndexAttributes ON UserSearchIndexAttributes.uID = UserPrivateMessagesTo.uAuthorID) ON UserPrivateMessages.msgID = UserPrivateMessagesTo.msgID WHERE UserPrivateMessagesTo.uID = ? AND UserPrivateMessagesTo.msgIsNew = 1 AND NOT UserPrivateMessages.uAuthorID = UserPrivateMessages.uToID;",array($uID));
      foreach ( $mq as $row ) {
         $msubj = ($row['msgSubject']);
         $afirst = ($row['ak_firstname']);
         $alast = ($row['ak_lastname']);
      }
      return $msgDat;
   }


...plus a method in the profiles controller view function:
// Message info
      $this->set('msgInfo',$msgDat);


...and then a variable in my profiles view.php:
$message2 = "$msgInfo";
echo $message2;


...All of which outputs nothing. Debug print_r also prints nothing (although thanks for that pointer: helpful.)

Anyway, if I could impose on the community expertise again, could anyone tell me what, oh what, I'm missing?
diorist replied on at Permalink Best Answer Reply 1 Attachment
diorist
Here's what finally worked. The following changes to the profile controller and view files add a new-messages summary table to the user profile. I've also attached an screenshot of the net result.

In controllers/single_pages/profile.php:

public function getMsgInfo() {
      $u = new User();
      $uID = $u->getUserID(); 
      $db = Loader::db();
      $mq = $db->Execute("SELECT UserPrivateMessages.msgID, UserPrivateMessages.msgDateCreated, UserPrivateMessages.msgSubject, UserSearchIndexAttributes.ak_firstname, UserSearchIndexAttributes.ak_lastname FROM UserPrivateMessages JOIN (UserPrivateMessagesTo JOIN UserSearchIndexAttributes ON UserSearchIndexAttributes.uID = UserPrivateMessagesTo.uAuthorID) ON UserPrivateMessages.msgID = UserPrivateMessagesTo.msgID WHERE UserPrivateMessagesTo.uID = ? AND UserPrivateMessagesTo.msgIsNew = 1 AND NOT UserPrivateMessages.uAuthorID = UserPrivateMessages.uToID;",array($uID));
      $messages = array();
      while($my_data = $mq->FetchRow()){
      $messages[] = $my_data;
      }
      $this->set('my_data', $messages);


And then in the single_pages/profile/view.php:
<table width="100%" border=0>
  <tr>
     <td><?php echo t('<b>Sender</b>') ?></td>
     <td><?php echo t('<b>Message Sent</b>') ?></td>
     <td><?php echo t('<b>Subject</b>') ?></td>
  </tr>
  <?php
  foreach($my_data as $row){
  ?>
  <tr>
   <td><?php echo $row['ak_firstname'].' ' .$row['ak_lastname']?></td>
   <td><?php echo $row['msgDateCreated']?></td>
   <td><a href="<?php echo $this->url('profile/messages/view_message/inbox/',$row['msgID']); ?>"><?php echo $row['msgSubject'] ?></td>
  </tr>
  <?php


A couple of other prerequisites were defining custom user attributes (firstname and lastname) and adding a tab script to the page.