Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Passing values from one function to another in a model?
#1

[eluser]Unknown[/eluser]
** Admin please move to code discussion forum, my apologies for wrong section **

Ok first off I am totally new to CI and php and was stoked when I was able to actually get the controller, model and view to all talk to each other and actually show data.

I have one table (CatList) that list the names of categories with a CatID field. I have a 2nd table (PageDetails) with more details and a CatID field that relates to the CatList.CatID field.

What I want to do is list the headings from the CatList table with a list of the pages from the PageDetails table that are listed with the same CatID.

I was able to get the headings to list (my first success with working with data Smile ) and used the function below:

Model
Code:
function fetch_cats()
{
//select the categories from the database and order by cat name
        $this->db->select('CatID,CatName');
    $this->db->order_by('CatName');
    $query=$this->db->get('PageCats');
            
    if($query->num_rows() > 0)
    {
           return $query->result();
    }
    else
    {
        return FALSE;
    }
}
In the controller I called it as such:
Code:
//* load the model to fetch the invader categories
    $this->load->model('CatList','CList');
    $this->data['Cats'] = $this->CList->fetch_cats();

    //load view invaders/index and attach model data to it
    $this->load->view('getcats/index', $this->data);

View
Code:
<?php
    foreach ($Cats as $Cat)
    {        
    echo('<h1>' . $Cat . '</h1>');
    }
?&gt;


Now what I can figure out for the life of me is how to script a second function (and believe me I tried many combinations) to call and list the pages for each category heading according to its CatID. I am stuck on how to pass values between functions in the model.

I am a cold fusion developer and this is my 2nd day attempting to learn this framework (and php) so please bare with me as I realize this may be a simple question to most people here.....got to learn somewhere Smile

Thanks in advance to anyone that can help.
#2

[eluser]pickupman[/eluser]
Codeigniter is object oriented so you when you want to call another function/method inside a controller/model/library you would use:
Code:
//Controller or Model or Library
function a(){
  //Do something here
}

function b(){
  //Get value from function a
  $this->a();
}

So you basically write $this->functionName() to reference a function/method in the same class/file. If you want to reference a function in another class's function/method, then you would use $this->anotherClass->functionName() (using CI's syntax). In regular PHP you would just use anotherClass->functionName().

A second part of your question/dilemma could also be fixed using a JOIN statement. You can join the two table based on a matching CatID.
Code:
$this->db->select("PageDetails.*, CatsList.*");
  $this->db->from("PageDetails");
  $this->db->join("CatsList","PageDetails.CatID=CatsList.CatID");
  $query = $this->db->get();
#3

[eluser]Unknown[/eluser]
[quote author="pickupman" date="1271754581"]
A second part of your question/dilemma could also be fixed using a JOIN statement. You can join the two table based on a matching CatID.
Code:
$this->db->select("PageDetails.*, CatsList.*");
  $this->db->from("PageDetails");
  $this->db->join("CatsList","PageDetails.CatID=CatsList.CatID");
  $query = $this->db->get();
[/quote]

The join statement is what I needed (sorry been a while since I did them in SQL). My next question would be how to get the results to show up under their respective category heading.

UPDATE

Looks like I was able to do it with a little tweaking of the output. Not 100% if this is the recommended way but it does work (from what I can see)

It was done in the view file:

Code:
$catTitle ="";
foreach ($invaders as $inva){
      if ($catTitle == "$inva->CatName") {
      }else{
    echo($inva->CatName);
    $catTitle="$inva->CatName";
      }
    echo ('<li>' . $inva->PageName . '</li>');                
      }

Thank you pickupman for the assistance!!! VERY much appreciated. Now I can move forward with the project and use this ability elsewhere.




Theme © iAndrew 2016 - Forum software by © MyBB