Welcome Guest, Not a member yet? Register   Sign In
Can't use custom library for some reason
#1

[eluser]D_Williams[/eluser]
I've used my own libraries before with no problem, but for some reason this one is erroring out on me.

The library file is called "Form_elements.php" and located in my application/libraries folder. Its purpose is to provide a bunch of functions to output form elements I use often (such as selecting a client from our client list in this case).

The contents for it are below:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Form_elements
{
    private $CI;    
    
    function __construct()
    {
        $this->CI =& get_instance();    
    }    
    
    function ClientSelect($name)
    {
        $this->CI->load->model('client_model');
        $clients = $this->CI->client_model->GetAllClients();

        $output = "<select name=\"$name\">";        
        foreach($clients as $c)
            $output .= "<option value=\"$c\" " . set_select($name, $c) . " >$c</option>";
        $output .= '</select>';
        
        return $output;
    }
}

Now I have a view file trying to load this library and use it.

Here is the view file:
Code:
&lt;?php
$this->load->library('form_elements');
echo form_open('utilities/batch_mail_sender');
?&gt;

<label for="client">Client Name</label>
&lt;?php echo $this->form_elements->ClientSelect('client'); ?&gt;

&lt;/form&gt;

When I load up that view, however, I get the following errors:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$form_elements

Filename: utilities/batch_mail_form.php

Line Number: 7

Fatal error: Call to a member function ClientSelect() on a non-object in /home/danny/workspace/OpenCollect/application/views/utilities/batch_mail_form.php on line 7

Am I doing something obviously wrong here?
#2

[eluser]D_Williams[/eluser]
Alright I've done some more poking around and apparently the cause of the problem is my template system.

I have a couple different "templates" in use so I can easily display all the page navigation and design on each page. Currently I have one for the main site layout and one minimalistic template view for popup windows (which this is). When I load a view I actually load the template view and pass it a $content_view variable and then the template view loads $content_view.

For example, here's my template view for popup windows:
Code:
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;?php echo $pageTitle; ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body style="padding: 20px 20px 12px 20px;border:#999 1px solid"&gt;
<b>&lt;?php echo $pageTitle; ?&gt;</b> - &lt;input type=button value="Close This Window"&gt;
<hr /><br />
&lt;?php $this->load->view($content_view); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

In my first post, the view I pasted in was the view being loaded by this template. If I take the $this->load->library('form_elements') out of the content view and put it in the template, it works as expected, but errors out as previously described if left in the content view.

I don't really want to keep the library load statement in the template since it won't be used by every content view. What's going on here?




Theme © iAndrew 2016 - Forum software by © MyBB