Welcome Guest, Not a member yet? Register   Sign In
Help with listing a category tree
#1

[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.
#2

[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..
#3

[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...
#4

[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.
#5

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

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




Theme © iAndrew 2016 - Forum software by © MyBB