CodeIgniter Forums
Help with listing a category tree - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Help with listing a category tree (/showthread.php?tid=20242)



Help with listing a category tree - El Forum - 07-02-2009

[eluser]JayArias[/eluser]
Code:
<?php

class tags extends Model{
    var $title;
    var $description;
    var $link;
    
    function tags()
    {
        parent::Model();
        $this->load->database('default');
    }
    function showAll()
    {
        $fetch_all =  $this->db->query("SELECT * FROM `tags`");
        return "<li><a >row('link')."' alt='".$fetch_all->row('description')."'>".$fetch_all->row('title')."</a></li>";
    }
}


This code generates
Tags
tees

What it should be generating is
Tags
tees
sweaters
pants
braceletes
hats

problem is when I do

Code:
function showAll()
    {
        $fetch_all =  $this->db->query("SELECT * FROM `tags`");
        while($fetch_all->result()){
echo "<li><a >row('link')."' alt='".$fetch_all->row('description')."'>".$fetch_all->row('title')."</a></li>";
}
    }

yes it servers its purpose. But, It will generate the rows till oblivion causing my server or browser to crash.

any thoughts? , I've looked through the documentation and haven't found a method.


Help with listing a category tree - El Forum - 07-02-2009

[eluser]LifeSteala[/eluser]
Try foreach instead of while..

Code:
foreach($fetch_all->result() as $r){
  echo "<li><a href='' alt=''>".$r->title."</a></li>";
}

href = $r->link
alt = $r->description

I couldn't put these two in as it breaks the code..


Help with listing a category tree - El Forum - 07-02-2009

[eluser]JayArias[/eluser]
Ok, did that works but ... why is it showing up on the top of my page Sad...

I think its because I defined it in the controller...


Help with listing a category tree - El Forum - 07-02-2009

[eluser]LifeSteala[/eluser]
Your model is not right still. You should not do any data manipulation in a model. Instead, a model should be used to put and get data to and from your database. It should be:

Code:
&lt;?php

class tags extends Model{
    var $title;
    var $description;
    var $link;
    
    function tags()
    {
        parent::Model();
        $this->load->database('default');
    }

    function showAll()
    {
        $fetch_all =  $this->db->query("SELECT * FROM `tags`");

        if ( $fetch_all ) {
            return $fetch_all;
        }

        return false;
    }
}

Then in your controller catch whatever returns from showAll, and pass it through to the view via an Array.

Controller:

Code:
class Test extends Controller {
    function Test() {
        parent::Controller();
        
        // Load stuff here
        $this->load->model('tags');
    }

    function method() {
       $data['showall'] = $this->tags->showAll();
       $this->load->view('viewname', $data);
    }
}

In your view, access $showall and use the foreach statement above.


Help with listing a category tree - El Forum - 07-02-2009

[eluser]JayArias[/eluser]
LifeSteala, Thanks very much. Works like a charm.


Help with listing a category tree - El Forum - 07-02-2009

[eluser]LifeSteala[/eluser]
You are most welcome. Good Luck Smile