• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looping inside of a model

#1
[eluser]neph[/eluser]
i'm trying to run loops to count two different columns inside of a model and having a bit of trouble. am i going about this the wrong way?

Code:
if($trigger == "fields") {
            $this->db->select('name, desciption');
            $categories =     $this->db->get_fields();
    } else {
            $query = $this->db->get('fm_categories');
            $categories = $query->result();
            foreach($categories as &$category) {
                $query = $this->db->get_where('fm_threads', array('categoryId' => $category->id));
                $category->numThreads = $query->num_rows();
                $threads = $query->result();
            foreach($threads as &$thread) {
                    $this->db->where('threadId', $category->id);
                    $thread->numPosts = $this->db->count_all_results('fm_posts');
                }    
            }
        }

        return $categories;
    }

#2
[eluser]neph[/eluser]
well, i switched some stuff up but i still cant seem to nail it..anyone have any ideas or suggestions?

#3
[eluser]umefarooq[/eluser]
its better to loop in controller just pass the parameters to your model, either tables name, fields to be selected from table like this

Code:
if(what ever true){
  $fields = array(feild1,field2,...,n);
$getvalue =  $this->modelname->yourfunction(tablename,$fields);
}
else{
  foreach(what you want to loop){
     $getvalue =  $this->modelname->yourfunction(tablename,$fields);
  }
  foreach(what you want to loop){
     $getvalue1 =  $this->modelname->yourfunction(tablename,$fields);
  }
}

use these parameters from controller and use in you model and your model can be some thing like this
Code:
function yourfunction($tables='', $fields=''){
  do what ever with your database. no need to loop here
  
  return your values.
}

or you can create other function with different parameters in your model for your looping code.

#4
[eluser]neph[/eluser]
well, what im trying to accomplish is a structured array that i cant seem to get from looping in the controller. below is my best representation of what im trying to do.
Code:
array(3) {
  [0]=>
  object(stdClass)#20 (7) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(7) "General"
    ["description"]=>
    string(36) "This area is for general discussions"
    ["privilege"]=>
    string(1) "0"
    ["active"]=>
    string(1) "1"
    ["numThreads"]=>
    int(1)
    ["numPosts"]=>
    string(1) "1"
  }
  [1]=>
  object(stdClass)#21 (7) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(10) "Neat stuff"
    ["description"]=>
    string(27) "this area is for neat stuff"
    ["privilege"]=>
    NULL
    ["active"]=>
    string(1) "1"
    ["numPosts"]=>
    int(1) "7"
    ["numThreads"]=>
    int(1) "12"
  }

that is the simplest output im trying to get.

so, for each instance of a category i want it to count the threads, and foreach instance of a thread count posts. i just assumed it should be done in the model since that's where the queries are coming from and using an &$value to append it to the existing array object.

#5
[eluser]kgill[/eluser]
Just a quick thought, your 2nd loop is using a reference to $threads but you aren't actually doing anything with it - you populate it but when the loop exits you aren't assigning it to the category you're working with so the information is lost.

#6
[eluser]neph[/eluser]
yeah i've fixed that since the post. its using foreach($threads as &$category) (im guessing thats right)

#7
[eluser]neph[/eluser]
alright, well i've realized i can get around the entire ordeal if i can join two other columns to the query and count results in each join, no real need for the loop to begin with.

#8
[eluser]cristian_c[/eluser]
neph,

Im having the same problem. can you post some code of the solution.
thanks

#9
[eluser]neph[/eluser]
i would need to see an example of your code, i worked around mine by joining mysql tables. i'm more than happy to help if i can though. just paste a snippet of what you have or what you're trying to do and we can see if my resolution will work for you as well.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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