Welcome Guest, Not a member yet? Register   Sign In
 ??
#1

[eluser]Tanax[/eluser]
Hi!

I have an output problem/error.
If I'm on the index function in a certain controller, everything works great. However, if I'm in the function called category, it outputs  before everything else(in the view!).

http://img27.imageshack.us/img27/6672/codeerror.jpg

Observe that this only occurs when I'm in a category that has subcategories. When I'm in a main category(a category without subs), it displays without that weird thing.

Controller functions used:
Code:
class Product extends Controller
{
    
    public $data = array();
    private $id;
    private $offset;
    
    public function __construct()
    {
        
        parent::Controller();
        
        $this->data['site'] = $this->Site_config->getConfig();
        $this->data['site'] = $this->data['site'][0];
        $this->data['categories'] = $this->Site_config->getCategories();
        
        $this->load->view( 'header', $this->data );
        
    }
    
    public function category($id, $offset = NULL)
    {
        
        // If the category id is invalid
        if(!$this->Category->valid($id))
        {
        
            $this->load->view( 'category/error', $id );
        
        }
        
        // If the category id is valid
        else
        {
            
            // Save the id and offset
            $this->id = $id;
            $this->offset = $offset;
            
            // Load the category
            $this->Category->load($this->id);
            
            // Get category info
            $this->data['category'] = $this->Category->getCategoryInfo();
            
            // If the category has sub-categories
            if($this->Category->subs() > 0)
            {
            
                $this->sub_category();
            
            }
            
            // If the category has no sub-categories
            else
            {
            
                $this->main_category();
            
            }
            
        
        }
        
        $this->load->view( 'footer' );
        
    }
    
    private function sub_category()
    {
    
        $this->data['subs'] = $this->Category->getSubs();
        $this->load->view( 'category/sub', $this->data );
    
    }

...

}

Category model
Code:
class Category extends Model
{
    
    private $id;
    
    public function __construct()
    {
        
        parent::Model();
        
    }
    
    public function valid($id)
    {
    
        if(is_numeric($id))
        {
        
            $this->db->from('categories')->where('cat_id', $id)->limit(1);
            $query = $this->db->get();
            
            if($query->num_rows() > 0)
            {
            
                return true;
            
            }
            
            return false;
        
        }
        
        return false;
    
    }
    
    public function load($id)
    {
    
        $this->id = $id;
    
    }
    
    public function subs()
    {
    
        $this->db->from('categories')->where('cat_parent', $this->id);
        $query = $this->db->get();
        
        return $query->num_rows();
    
    }
    
    public function getSubs()
    {
    
        $this->db->from('categories')->where('cat_parent', $this->id)->order_by('cat_order', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    
    }
    
    public function getCategoryInfo()
    {
        
        $this->db->from('categories')->where('cat_visible', 1)->where('cat_id', $this->id)->limit(1);
        $query = $this->db->get();
        
        if($query->num_rows() > 0)
        {
            
            foreach($query->result() as $row)
            {
                
                return $row;
                
            }
            
        }
        
        return false;
        
    }
    
}

The "sub" view
Code:
<div id="cat-title">&lt;?=$category->cat_name?&gt;</div>

        <div id="cat-desc">&lt;?=$category->cat_desc?&gt;</div>
                
        <div id="products">
                
            &lt;?php foreach($subs as $sub): ?&gt;
                
            <h2>&lt;?=anchor('product/category/' . $sub->cat_id, $sub->cat_name)?&gt;</h2>
            
            &lt;?php endforeach; ?&gt;
                
        </div>

Again, note that the display of a maincategory is rendered completely without errors and any of those characters(so there's nothing wrong with the charset or anything like that), so my guess is that something's wrong with the sub_category-used functions either in the model, view or controller.

Anyone have an idea?
#2

[eluser]Dam1an[/eluser]
This has come up a few times, the solution seems to be to make sure ALL files are encoded as UTF-8 without BOM (Byte Order Mark)

Not sure how/why that works, but it seems to
#3

[eluser]Tanax[/eluser]
Well, this would then be.. a problem with the view file, since I've used different view files for subcategories and maincategories. How would I encode it as UTF-8??
#4

[eluser]Tanax[/eluser]
Solved it!

Thanks for the help!! Smile
#5

[eluser]Dam1an[/eluser]
Was it the file encoding? How did you solve it?
#6

[eluser]Tanax[/eluser]
Yep! It was encoded as UTF-8, I changed it to UTF-8 without BOM, and now it's working xD
Weird error I must say.
#7

[eluser]darri[/eluser]
I had the same problem with odd characters appearing before anything else on the page (I got ). After a good half hour of tinkering it seems that the problem lies with one or more of the codeigniter files (controller, model or view) being set to UTF8 encoding, while the header's meta tag charset was set to something else (for English it's usually iso-8859-1). The solution for me was to make sure that they were all set to the same type of encoding.

So if your codeigniter files are encoded as UTF8, use a header meta tag like this:
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

Or if your codeigniter files are encoded as 8 Bits (I use Flash Develop + this seems to be the default alternative to UTF8), use a header meta tag like this:
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;

In the second case there, it might help if you save your codeigniter files as UTF8 and then save them again as 8 Bit, just to refresh the encoding.

Hope that helps you, it worked for me.




Theme © iAndrew 2016 - Forum software by © MyBB