SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)

    Question [SOLVED] Passing An Array Between Classes

    This is a generic problem I'm having but an example is below. What I'm trying to do is get the data from the database (using model classes) and pass the data via an array to the view classes (via controller classes if any processing of the data needs to be done).

    The example below works fine when dealing with the data in the model class but whatever I've tried so far either results in the display of only one line of the array or a line of the array still displays but it keeps on repeating until php times out.

    I'm trying to keep the model classes solely for accessing and interfacing with the data with the controllers only processing the data and the views only displaying the data (with a minor bit of processing to decide what needs viewing).

    I'm trying to always use while loops over foreach loops wherever possible due to the superior performance of while loops.

    As i'm still relavtivly new to OOP php, my suspision is that i don't know the proper syntax for what i'm trying to do.

    method from messages_model class
    PHP Code:
          function message_inbox_get_messages($db,$user_id) {
              
    $sql="SELECT
                  ue_messages_delivery.message_id AS message_id,
                  ue_messages_delivery.sender_id AS sender_id,
                  ue_messages.subject AS subject,
                  ue_messages_delivery.date_sent AS date_sent
                  FROM ue_messages
                  INNER JOIN ue_messages_delivery
                  ON ue_messages.message_id = ue_messages_delivery.message_id
                  WHERE ue_messages_delivery.recipient_id = '
    $user_id'
                  AND ue_messages_delivery.parent_id = 0 "
    ;
              
    $result=$db->dbQuery($sql);
              while ( 
    $row=$db->dbFetchArray($result) ) {
                  
    $sent_by=$row['sender_id'];
                  
    $message_id=$row['message_id'];
                  
    $subject=$row['subject'];
                  
    $from_user=$this->messages_get_sender_name($sent_by,$db);
                   echo 
    "<tr>\n";
                   echo 
    "<td>" .$from_user."</td>\n";
                   echo 
    "<td>link for selecting a message to view</td>\n";
                   echo 
    "<td>" .$row['date_sent']."</td>\n";
                   echo 
    "</tr>\n";
                  
    $message['message_id']=$row['message_id'];              
                  
    $message['date_sent']=$row['date_sent'];
              }
          } 
    Method from messages_view class
    PHP Code:
          function display_inbox($db) {
              
    $user_id=4;
              echo 
    "<h1>View Inbox</h1>";
              echo 
    "<table class='all_messages'>\n";
              echo 
    "<tr>";
              echo 
    "    <th>";
              echo 
    '        From';
              echo 
    "    </th>\n";
              echo 
    "    <th>";
              echo 
    '        Subject';
              echo 
    "    </th>\n";          
              echo 
    "    <th>";
              echo 
    '        Time';
              echo 
    "    </th>\n";
              echo 
    "</tr>";
              
    $num_messages=$this->messages_model->messages_count($db);
              if ( 
    $num_messages ) {
                  echo 
    "<tr>";
                  echo 
    "<td colspan='4'>";
                  echo 
    "<div class='no_news'>No messages in your inbox</div>";
                  echo 
    "</td>";
                  echo 
    "</tr>";
              } else {
                  
    $this->messages_model->message_inbox_get_messages($db,$user_id);
              }
              echo 
    "</table>\n";
              echo 
    "<div class='button'><p><a href='main_page.php?view=messages&amp;subview=write_message'>Write New Message</a></p></div>\n";
          } 
    Last edited by SpacePhoenix; Feb 24, 2009 at 23:02.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ideally, your model shouldn't output HTML, if fact, it shouldn't output anything.

    If for example you wanted to use the 'inbox' model to provide the user an XML feed of their messages, you cannot do this now as it outputs HTML.

    Hopefully, the following should clarify a little.

    PHP Code:
    <?php
    class Messages
    {
        public function 
    view()
        {
            
    $oView = new MessagesHTMLView();
            
    $oModel = new MessagesModel();
            
    $oView->bind('messages'$oModel->getInbox(76));
            echo 
    $oView->render();
        }
        
        public function 
    rss()
        {
            
    $oView = new MessagesRSSView();
            
    $oModel = new MessagesModel();
            
    $oView->bind('messages'$oModel->getInbox(76));
            echo 
    $oView->render();
        }
    }
    ?>
    Notice that we used the same model to display the same data 2 different ways, which is as it should be, as we only changed the view.

    Your model provides the data to the view, the view can iterate the data as it's related to the view.

    I hope that makes sense.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    SilverBulletUK, you understand only part of the problem. The array is created fine in the model but when it is returned to the view (or whatever class has happened to call it), only one row of the array is returned.

    Experimenting with foreach loops and while loops + print_r and var_dump has shown that the view will only ever receive one row of the array. Once i can get the whole array and not just one row to the view then i can remove all the display output from the model.

    I suspect that there is a special syntax that I need so that:

    $an_arry=$another_object->model_method_to_get_data_array($database_connection,$search_value);

    will return the whole array and not just like the first row of the array.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No, there is no special syntax for obtaining a full array rather than a partial one. They problem lies in your model, I don't see any code in the example posted that returns the data, are you?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have something like this?
    PHP Code:
    $messages = array();
    while (
    $row $this->db->fetchRow($sql))
    {
        
    $messages[] = $row;
    }

    return 
    $messages
    Adrian Schneider - Web Developer

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    I did have it in the while loop like this:

    PHP Code:
    $message['subject']=$row['subject']; 
    with one for each column of the array.

    I tried SirAdrian's suggestion:

    PHP Code:
    $messages = array();
    while (
    $row $this->db->fetchRow($sql))
    {
        
    $messages[] = $row;
    }

    return 
    $messages
    And that worked a treat, thanks

    On the view side of things i changed it to (after a bit more experimenting):

    PHP Code:
                  foreach ($message as $user_message) {
                      
    extract($user_message);
                      
    $from_user=$this->messages_model->messages_get_sender_name($sender_id,$db);
                      echo 
    "<td>" .$from_user."</td>\n";
                      echo 
    "<td><a href='link for selecting a message to view'>$subject</a></td>\n";
                      echo 
    "<td>" .$date_sent."</td>\n";
                      echo 
    "</tr>\n";
                  } 
    The foreach gives me one row at a time, the extract grabs the individual variables from the from then i display them, repeated until there are no more messages to list.

    Result, all data display removed from the model, thanks for the help SirAdrian. SilverBulletUK, at the time i was experimenting with another view and model where i had exactly the same problem, now i can migrate the data viewing and processing to the view or controller as appropraite.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •