Welcome Guest, Not a member yet? Register   Sign In
Calling a function from a function and passing data within same model?
#1

[eluser]rhopek[/eluser]
I feel like I am so close to getting a great handle on this CI framework, but one thing is currently stumping me.

Let's say I have a model that has a function, and that function performs a query that will include an 'id' field/value and stores it in the $data[] array, among many other key/value pairs.

Now I have another function in that same model that needs to receive that value for the 'id' field in the previous function. I then want to perform a new query using that 'id' value, which will sum a number of values from other tables (which is why there isn't a join being used in the original function; it would be far too difficult). I then want to feed that summed value back into the original $data[] array from the first function so that the complete array can be passed to a view from that original function.

My questions:

1) How do I call the second function from within the first and pass it the required value?
2) How do I return the additional key/value from the second function back to he first so that the full $data[] array can be passed to the view?

I'm been trying to make it happen for about the last 4 hours and have come to the conclusion I need some help.
#2

[eluser]rhopek[/eluser]
I've gotten things much closer now. My issue is now no longer CI-specific, but rather PHP array specific, so if this is now inappropriate to continue this thread, please let me know. If not, here's the current status:

My first function produces an array as follows:


Array
(
[0] => stdClass Object
( [id] => 1
[first_name] => John
[last_name] => Smith
[email_address] => [email protected]
)
[1] => stdClass Object
( [id] => 2
[first_name] => Jane
[last_name] => Doe
[email_address] => [email protected]
)
)



When I make the call to the second function from within the first function, I am looping through each row from the first array. On the first pass (which would be for item '0' in that initial array, I get back as my result:

Array
(
[ave_rating] => 4
)


So my question is now, how do I get the "[ave_rating] => 4" pushed into item "0" from the initial array, so that the store $data[] array now has:

Array
(
[0] => stdClass Object
( [id] => 1
[first_name] => John
[last_name] => Smith
[email_address] => [email protected]
[ave_rating] => 4
)
)

And obviously, as I keep looping, I need to keep inserting the additional element into each respective subsequent initial array items.

I have been trying to use the PHP push_array() function, but do not seem to be getting the expected results.
#3

[eluser]rhopek[/eluser]
It appears the issue is that the CI code is returning each of those items as an object, and I cannot for the life of me figure out how to insert another key/value pair into an object.
#4

[eluser]Vheissu[/eluser]
Sounds like you are using ->result() instead of result_array() when returning your database data and storing it. If you post your code in relation to where information is pulled from the database, I can help you pinpoint the error and rewrite it.

This article in the user guide should help you. It's about returning query results: http://ellislab.com/codeigniter/user-gui...sults.html
#5

[eluser]rhopek[/eluser]
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.
#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.




Theme © iAndrew 2016 - Forum software by © MyBB