Welcome Guest, Not a member yet? Register   Sign In
Store objects in array elements
#4

[eluser]obiron2[/eluser]
OK, real code..

from the controller

Code:
function get_race_results()
  {
    $this->load->model('F1_playerteams_model');
    $this->load->model('F1_players_model');
    $this->load->model('F1_teams_model');
    $this->load->model('F1_races_model');
    $this->Teams = $this->F1_playerteams_model->get_allplayerteams();
    $this->Races = $this->F1_races_model->get_allraces();
    foreach ($this->Teams as $Team)
     {
        $ADR = $this->build_team_profile($Team->ID);
        $Team->Driver1->ID = $ADR->Driver1->ID;
        $Team->Driver1->Name = $ADR->Driver1->Name;
        $Team->Driver2->ID = $ADR->Driver2->ID;
        $Team->Driver2->Name = $ADR->Driver2->Name;
        $Team->Car->ID = $ADR->Car->ID;
        $Team->Car->Name    =  $this->F1_teams_model->get_teamname($Team->Car->ID);
        $Team->Engine->ID = $ADR->Engine->ID;
        $Team->Engine->Name = $this->F1_engines_model->get_enginename($Team->Engine->ID);
        $Team->SeasonPoints =  0;
        foreach ($this->Races as $Race)
          {
            $Team->Race->ID = $Race->ID;
            $Team->Race->Points = $this->get_team_race_points($Team->ID,$Race->ID);
            $Team->SeasonPoints = $Team->SeasonPoints + $Team->Race->Points;
          }
        }
   $data['title'] = "FrothPoker Fantasy F1 Results";
   $data['Teams'] = $this->Teams;
   $this->load->view('f1results_view',$data);
}[code]

in the view

[code]foreach($Teams as $Team)
  {

   print "<TR class=\"line$x\"><TD><A HREF=\"".site_url()."/test/player_team_results/".$Team->ID."\">$Team->Name</A></TD><TD>";
   print $Team->Player."</TD><TD>";
   print $Team->Profile->Driver1->Name."</TD><TD>";
   print $Team->Profile->Driver2->Name."</TD><TD>";
   print $Team->Profile->Car->Name."</TD><TD>";
   print $Team->Profile->Engine->Name."</TD><TD>";
   print $Team->SeasonPoints."</TD></TR>";
   $x = 1- $x;
  }


explanation:

$this->Teams starts out as just a list of the team IDs. This is a CI stdClass object that is an array of objects, each object in the array only contains one property - the team ID
I then create properties and assign values to each object in the array by getting the relevant data from the model in the foreach loop.
I then pass $this->Teams to the view as ['Teams'] and pick it up in the model as $Teams

$Teams is an array of objects. each object then has properties, some of which (e.g. Profile) are objects in themselves which then have their own properties.

NOTE: the assignement of new properties to stdClass objects only seems to work in PHP5, but this is a limitation of PHP, not of CI.

2nd NOTE: These assignments have been done in the controller when in reality they should be done in the model but I was proving a concept. One nice thing about CI is that you do not have to follow MVC if you don't want to. The production version of this will have the components properly separated.


Messages In This Thread
Store objects in array elements - by El Forum - 08-29-2007, 04:51 PM
Store objects in array elements - by El Forum - 08-30-2007, 03:05 AM
Store objects in array elements - by El Forum - 08-30-2007, 03:29 AM
Store objects in array elements - by El Forum - 08-30-2007, 04:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB