Welcome Guest, Not a member yet? Register   Sign In
Call to a member function row() on a non-object
#1

[eluser]stephencoverdale[/eluser]
Hi All,

I get this error -

PHP Fatal error: Call to a member function row() on a non-object in /var/www/application/controllers/site.php on line 32

Here is the code

Code:
function scorecalc(){
  $this->load->model("get_db");
  $data['fbid'] = $this->get_db->getFBID();
  foreach($data['fbid'] as $row){
   $fbid = $row->fb_id;
  $fbquery = $this->get_db->getFB($fbid);
  $row = $fbquery->row();
  $likes = $row->total_page_like;
  $talkingabout = $row->talking_about_count;
  $werehere = $row->were_here_count;
  $score = $likes+$talkingabout+$werehere;
  echo $score;
  
  
  }

I did a var_dump and got the following results

array(1) { [0]=> object(stdClass)#22 (3) { ["total_page_like"]=> string(2) "43" ["talking_about_count"]=> string(1) "6" ["were_here_count"]=> string(3) "899" } }

Was wondering if anyone could see what was going on.

Thanks,
Steve
#2

[eluser]Aken[/eluser]
Your model probably already uses result() after the query. You can only use row() or result() once - both are methods for retrieving requests from the query object (which is sent by get(), get_where(), etc.).
#3

[eluser]stephencoverdale[/eluser]
Your right. I tried removing result() and using row() in the model but it still did not work. My goal is that I want to scan each facebook id and then with that facebook id it will then go and collect selective data from another table, add them together and print a different score for each of the facebook id's. My issue however comes to the fact that trying to run the nested foreach causes issues as when it echos the final score it only gives me one score that added up the data from every facebook id but there should be multiple scores. This is why I tried using the row() function to limit to just one row for each score to hope tofix this issue. Am I looking at this wrong?

Here is function I am trying -
Code:
function scorecalc(){
  $this->load->model("get_db");
  $data['fbid'] = $this->get_db->getFBID();
  foreach($data['fbid'] as $row){
   $fbid = $row->fb_id;
  $data['fbdata'] = $this->get_db->getFB($fbid);
  foreach($data['fbdata'] as $row){
   $likes = $row->total_page_like;
   $talkingabout = $row->talking_about_count;
   $werehere = $row->were_here_count;
   $score = $likes+$talkingabout+$werehere;
   echo $score;
   }
  
  }

Model functions -
Code:
function getFBID(){
  $query = $this->db->query("SELECT `fb_id` FROM `fb_page_profile`");

  return $query->result();

}


function getFB($fb){
  $query = $this->db->get("SELECT total_page_like, talking_about_count, were_here_count FROM `fb_page_profile` WHERE fb_id =  '$fb'");

  return $query->result();

}

Thanks,
Steve
#4

[eluser]stephencoverdale[/eluser]
I fixed this! I realized I did not have any separation in my echo's that's why the number was odd!

Sorry!
#5

[eluser]Aken[/eluser]
You've got a lot of unnecessary logic going on. You're using one query to retrieve all of the IDs, and then you're looping through them and doing individual queries to get each row. Pretty redundant, considering you can retrieve all of that data just doing one normal query.

Code:
$query = $this->db
->query("SELECT fb_id, total_page_like, talking_about_count, were_here_count FROM fb_page_profile")
->result();

// result() returns an array of objects, each object is a row in the DB.

foreach ($query as $row)
{
$score = $row->total_page_like + $row->talking_about_count + $row->were_here_count;
}
#6

[eluser]stephencoverdale[/eluser]
Thanks so much, this is great feedback. I got a little lost with some of it and so kind of went off in a tangent in my code. I'll definitely update to yours.

Thanks again
#7

[eluser]ramirors[/eluser]
on you model try:

return $query->result_array();




Theme © iAndrew 2016 - Forum software by © MyBB