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

[eluser]LifeSteala[/eluser]
Usually, in your controller you have an array, say $data[] which is parsed to the view, like so:

Code:
$this->load->view('View', $data);

The array would store strings like:

Code:
$data['a'] = "hello";
$data['b'] = "goodbye";
$data['c'] = "cya";

How can you create objects and store it in a,b and c?? So, $data['a'] will have object a, b will have object b, c->object c.. any idea's?
#2

[eluser]obiron2[/eluser]
There is no reason why the array elements cannot be objects or arrays them selves. if you had an object $hello and passed that as $data['a'] = $hello;

you could call its properties in the view using

$a->propertyofhello

obiron
#3

[eluser]LifeSteala[/eluser]
Sorry, could you please explain that a little more?? OO Isn't my strongest subject Smile.

Appreciate your reply.
#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.




Theme © iAndrew 2016 - Forum software by © MyBB