• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a function from a function and passing data within same model?

#6
[eluser]Vheissu[/eluser]
[quote author="rhopek" date="1288145627"]Yes, but I found a way around it (though I'll try what you noted).

It was all a simple matter of taking the object that CI was giving, converting it to an array, merging in a second array with the values I wanted to add and then converting it back to an object.

Here's some sample code for those that might be interested:

For my first function, I have:

Code:
function getPeeps($event_id=0) {

  ... CI Database Code

  $q = $this->db->get();
  if ($q->num_rows() > 0) {
    foreach ($q->result() as $row) {
      $array = (array)$row;
      $ave_rating = $this->getPeepsRating($row->v_id);
      $array = array_merge((array)$array, (array)$ave_rating);
      $data[] = (object)$array;
    }
  }
  return $data;

}

And in the second function:

Code:
function getPeepsRating($peeps_id) {

  ... CI Database Code

  $q = $this->db->get();
  if ($q->num_rows() > 0) {
      foreach ($q->result() as $row) {
          $total_ratings[] = $row;
      }
  }
  $count = 0;
  $totals = 0;
  foreach ($total_ratings as $item => $entry) {
      foreach ($entry as $rating => $value) {
          $totals = $totals + $value;
      }
      $count++;
  }
  $ave_rating = $totals/$count;
  $data['ave_rating'] = $ave_rating;
  return $data;

}


All of this allowed me to call one function from another function within the same model, pass parameters to both the first and the second functions and then have all of the relevant data passed to the view.[/quote]

Dude..

Using $q->result() will return an object, that is how result() works. Read the link I gave you, it clearly explains that. If you want it to return just a pure array, use $q->result_array() instead.. Writing all of that extra code to convert an object into an array is unnecessary.


Messages In This Thread
Calling a function from a function and passing data within same model? - by El Forum - 10-26-2010, 09:07 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.