Welcome Guest, Not a member yet? Register   Sign In
Form field data pulled from database
#1

[eluser]mdcode[/eluser]
Another newbie question from me...

I've tried to search for this and look at other examples but I jsut can't find anything relevant to help me in my case. I have a form that I am attempting to convert using the form helper (which is autoloaded) and I need to populate certain fields (all of them seem to be select fields) with options from the database.

So I have a table in my database listing customers. In that table I have customerid, and customerName fields, and I need to populate a drop-down with the customerName displayed, where it would input the customerid into the database.

I've tried doing this myself and it's not doing anything...
#2

[eluser]jedd[/eluser]
Quote:I've tried doing this myself and it's not doing anything...

Ahhh .. yes. I can recommend having a read of [url="http://www.chiark.greenend.org.uk/~sgtatham/bugs.html"]Simon Tatham's elucidating article on how to write good bug reports[/url].

But just before you head off to read that, post your code! And expand on 'not doing anything'.
#3

[eluser]mdcode[/eluser]
Sorry jedd - I'm usually good at writing error and bug reports, and when I say it's not doing anything, I literally mean, it's not doing anything, no messages, no errors, all I have the select box. Also I did not think that code was required in this instance...

Still, while waiting I have done some more work and sort of getting a little closer I think with it, but if it helps, here's the code I have so far:

Model:
Code:
function get_all_field_data($data)
    {
        $data = array(
            'customer' => $this->db->get('customers')
        );
    }
    
    function add()
    {
        $input = array(
            'customer' => $this->input->post('customer'),
            'division' => $this->input->post('division'),
            'dateRequired' => $this->input->post('dateRequired'),
            'tech' => $this->input->post('tech'),
            'rep' => $this->input->post('rep'),
            'priority' => $this->input->post('priority'),
            'allocatedTime' => $this->input->post('allocatedTime')
            );
            
            $this->db->insert('projects', $input);
        
        // $this->db->query("INSERT INTO projects (id, customer, division, dateRequired, tech, rep, priority, allocatedTime) VALUES");
    }

Controller:
Code:
function __construct()
    {
        parent::Controller();
        
        /* load the session library */
        $this->load->library('session');
        
        /* set the template for the site */
        $this->template->set_master_template('default/template_main.php');
        
        /* load in the required models */
        $this->load->model('projects_model', '', TRUE);
    }
    
    function index()
    {
        $data['siteTitle'] = $this->config->item('siteTitle');
        $this->template->write_view('content', 'default/pages/projects_index', $data);
        $this->template->render();
    }
    
    function add()
    {
        $data['siteTitle'] = $this->config->item('siteTitle');
        $this->template->write_view('content', 'default/admin/projects_add', $data);
        $this->template->render();
    }

View:
Code:
echo form_open('models/project_model');
echo "<span class='fontSubH'>Main Detail<br /></span>";
echo form_label('Customer','customer'); "&nbsp;";
echo form_dropdown('customer',$this->projects_model->data['customer']);

Now with the changes that I have done while waiting for replies, I am getting errors:

Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Projects_model::$data
Filename: admin/projects_add.php
Line Number: 20
and
Code:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 268
#4

[eluser]jedd[/eluser]
No prob. I just ask the obvious questions so the smart guys can come along and spot your mistake faster.

One thing that does stand out is this code, though:

Code:
function get_all_field_data($data)  {
        $data = array(
            'customer' => $this->db->get('customers')
        );
    }

Normally you'd do two things differently. First, you'd say ($data = NULL) (or some value) in the function definition. It makes it easier to ...

Secondly you'd actually test to see if you'd been passed some data before rudely overwriting it with your $data = ... line.

One might suggest a third thing a function like 'get_something' would do .. is actually, you know, return something.
#5

[eluser]mdcode[/eluser]
Thanks jedd... I've been playing with this some and spotted the fatal ($data) thing and have taken that part out. I've also changed it so that instead of trying to get all data for all fields in one function, I'm just trying to set it up to display the list of customers in a function called "get_customers" like so:
Code:
function get_customers()
    {
        /* customer selection query */
        $this->db->select('customerid', 'customerName');
        $this->db->from('customers');
        $this->db->order_by('customerName');
        $this->db->get();
    }
Now I'm not quite sure what to put in the controller to make it pass this into the view, and what attributes to put on the select box to make it display the values -- I'm getting the same errors as before still.
#6

[eluser]jedd[/eluser]
Quote:View:
Code:
echo form_open('models/project_model');
echo "<span class='fontSubH'>Main Detail<br /></span>";
echo form_label('Customer','customer'); "&nbsp;";
echo form_dropdown('customer',$this->projects_model->data['customer']);

Okay .. reading further .. this one stands out too. You can't call models from within the view.

Actually, you can, but it takes more than just what you've done here.

More importantly - you shouldn't. You should re-engineer your code such that this data is picked up by the controller, which will then pass it to the view.
#7

[eluser]mdcode[/eluser]
Ok, I think we need an update, but first, thanks for your help on this -- and I apologize that the code keeps on changing that you don't know about... here's where I am now with the error:

Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: get_customers
Filename: admin/projects_add.php
Line Number: 20

The select box call in the view is now this:
Code:
echo form_dropdown('customer', $get_customers, '');

The model is this:
Code:
function get_customers()
    {
        /* customer selection query */
        $this->db->select('customerid', 'customerName');
        $this->db->from('customers');
        $this->db->order_by('customerName');
        $this->db->get();
    }

And the controller:
Code:
function add()
    {
        $data['siteTitle'] = $this->config->item('siteTitle');
        $this->projects_model->get_customers();
        $this->template->write_view('content', 'default/admin/projects_add', $data);
        $this->template->render();
    }
#8

[eluser]jedd[/eluser]
Quote:
Code:
function get_customers()
    {
        /* customer selection query */
        $this->db->select('customerid', 'customerName');
        $this->db->from('customers');
        $this->db->order_by('customerName');
        $this->db->get();
    }

This is in your model, right? If it's not, then it should be.

In any case, have a re-read of the CI Manual pages on [url="http://ellislab.com/codeigniter/user-guide/database/active_record.html"]Active Record[/url] and [url="http://ellislab.com/codeigniter/user-guide/database/results.html"] Processing database results [/url]

Quote:Now I'm not quite sure what to put in the controller to make it pass this into the view, and what attributes to put on the select box to make it display the values -- I'm getting the same errors as before still.

Your last line above - you need to actually use the output of the db->get() function. So instead have something like:
Code:
$foo = $this->db->get();
return $foo;

In your controller, you can process that data like this:

Code:
$customers = $this->projects_model->get_customers();

foreach ($customers->result() as $row)
{
    echo $row->customerid;
    echo $row->customerName;
    echo $row->body;
}

Code is very much untested, of course - as I tend to use this->-db->query() stuff instead of AR.

Btw, I concur with CI's style guide - avoid CamelCase (ItBlowsBigTime) - use underscores between words instead. Also you almost definitely do not want a customerid column in a table called customer. Just call it id instead. Ditto for 'customer.customerName' (etc).
#9

[eluser]jedd[/eluser]
[quote author="mdcode" date="1237273460"]
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: get_customers
Filename: admin/projects_add.php
Line Number: 20

The select box call in the view is now this:
Code:
echo form_dropdown('customer', $get_customers, '');
[/quote]

Okay .. in very general terms, any variables you are going to use inside your view need to have appeared in your controller in a construct like this:

Code:
$data['my_variable_wot_i_like'] = "some data goes in here";

If you don't do this, then you won't get $my_variable_wot_i_like in your view.

General terms, mind. The above is true because you are passing the $data variable around, and most people use this variable name, and so on. Everyone lies, and all that.

In this instance, $data['get_customers'] is never assigned in your controller. That explains the error you're seeing. get_customers() is a function in your model, but that's not sufficient for CI to work out what you mean Wink


In any case, you need to go thru the other things I've suggested, and see if you're making some headway. I'm orf to bed, and I'm not sure who's on the next shift. Wink
#10

[eluser]mdcode[/eluser]
I think I almost have it... But - you saw that one coming right? - while I get a populated select box, all it's populated with it this:
Code:
Resource id #33
Resource id #45
result_array (formatted as an option group)
result_object (formatted as an option group)
0
2
blank-line

The model is shown as this:
Code:
function get_customers()
    {
        /* customer selection query */
        $this->db->select('*');
        $this->db->from('customers');
        $this->db->order_by('customerName');
        $cust_query = $this->db->get();
        
        return $cust_query;
        
            foreach ($cust_query->result() as $row)
            {
            echo $row->customerid;
            echo $row->customerName;
            }
    }
The controller is this:
Code:
function add()
    {
        $data['siteTitle'] = $this->config->item('siteTitle');
        $data['customers'] = $this->projects_model->get_customers();
        $this->template->write_view('content', 'default/admin/projects_add', $data);
        $this->template->render();
    }
And the view:
Code:
echo form_dropdown('customer', $customers, '');

Now if I put the foreach loop in the controller as suggested, I can echo out the customer id's and names, but the select box still populates with the items above.

As some asides, I have been trying to follow the CI formatting procedures, and the only place where I have been using camelCase is in my field names, which I understood was ok and usual, unless you have seen something else that I have been doing wrong...??

Still, have a good night and hopefuly someone else can offer more insight as to what is going on.




Theme © iAndrew 2016 - Forum software by © MyBB