CodeIgniter Forums
Form field data pulled from database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form field data pulled from database (/thread-16796.html)

Pages: 1 2 3


Form field data pulled from database - El Forum - 03-16-2009

[eluser]jedd[/eluser]
CI User Guide has some style guidelines. I tend to agree with most of them .. but, obviously, it's entirely your call. I go my own way with indenting, for example, because I really dislike the way the CI libraries do it. Meh.

Your form is failing because dropdown requires an array, and you are passing an object to it.

var_dump is your friend. Use it. A lot. Everywhere. And often.

I'd suggest - modify your model to return an array rather than an object. The CI manual page regarding database queries is your friend here.


Form field data pulled from database - El Forum - 03-16-2009

[eluser]mdcode[/eluser]
I do apologize for my ignorance on this jedd, and all, I really do appreciate the help. I have just tried sending an array as the result but no change, the drop down is still as above. However, I have put in the var_dump (I admit I have never heard of this before) and it has text on the page but I have no idea what it means or says:

Code:
object(CI_DB_mysql_result)#17 (7) { ["conn_id"]=>  resource(33) of type (mysql link persistent) ["result_id"]=>  resource(45) of type (mysql result) ["result_array"]=>  array(0) { } ["result_object"]=>  array(0) { } ["current_row"]=>  int(0) ["num_rows"]=>  int(2) ["row_data"]=>  NULL }

I'm presuming that it is picking up data as the num_rows is showing as 2, which is precisely the number of customers there are in the customers table.


Form field data pulled from database - El Forum - 03-17-2009

[eluser]jedd[/eluser]
[quote author="mdcode" date="1237285045"]I have just tried sending an array as the result but no change, the drop down is still as above. [/quote]

OOC, how did you send an array? Be precise.

With your var_dump - you can also use print_r - you often want to echo "<pre>" first - do you know this HTML? It tells the browser that what follows is preformatted text, so it displays it as is, without wrapping it only very long lines, etc.

Quote:[code]object(CI_DB_mysql_result)#17 (7) { ...

What this says .. is that it's an object.

The form function you are trying to use doesn't just have a slight preference for, or feels marginally happier about having, an array.

It really does need an array.

Consequently your model will need to use a different db call to get the model OR you have to convert the model to an array in there somehow. Hint - the first option is easier.


Form field data pulled from database - El Forum - 03-17-2009

[eluser]mdcode[/eluser]
Hey jedd, thanks for getting back to me. Looking through the CI documentation, I followed your advice at attempting (apparently poorly since you say it's still wrong), to pass an array result to the controller to display in the view, like this:

Model:
Code:
function get_customers()
    {
        /* customer selection query */
        $this->db->select('*');
        $this->db->from('customers');
        $this->db->order_by("customerName", "ASC");
        $cust_query = $this->db->get();
        
            foreach ($cust_query->result_array() as $row)
            {
            $row['customerid'];
            $row['customerName'];
            }
            
        return $cust_query;
    }

I changed the basic query since I'm more familiar with MySQL query strings as well (I just wanted to learn the AR way and use it). The controller is I think still the same but here it is again as it stands:

Controller:
Code:
$data['customers'] = $this->projects_model->get_customers();

And I know the <pre> part of HTML but have never used it; still, I have put it in the view along with print_r and this is what is shown in the view:

View:
Code:
CI_DB_mysql_result Object
(
    [conn_id] => Resource id #33
    [result_id] => Resource id #45
    [result_array] => Array
        (
            [0] => Array
                (
                    [customerid] => 1
                    [customerName] => Company 1
                )

            [1] => Array
                (
                    [customerid] => 2
                    [customerName] => Company 2
                )

        )

    [result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] => 2
    [row_data] =>
)



Form field data pulled from database - El Forum - 03-18-2009

[eluser]jedd[/eluser]
[quote author="mdcode" date="1237343455"]
Model:
Code:
function get_customers()
    {
        /* customer selection query */
        $this->db->select('*');
        $this->db->from('customers');
        $this->db->order_by("customerName", "ASC");
        $cust_query = $this->db->get();
        
            foreach ($cust_query->result_array() as $row)
            {
            $row['customerid'];
            $row['customerName'];
            }
            
        return $cust_query;
    }
[/quote]

Okay .. what do you believe your indented foreach code there will actually do? I mean, what's the intent of that block, and do you think it delivers?


I suggested using arrays as they're easier for people who aren't used to dealing with objects or AR approaches - so you either need to assign those $row data above to a new array variable and return that OR use the $query->result_array() in the original call (and replace the ->db-> style AR calls) at the same time, OR you modify your view to cope with cycling through an object rather than an array.

Any of these options are open to you - so you pick which one you like the sound of the most, and we'll move forward with that one. At the moment I feel we're trying multiple approaches to the one basic problem.


Form field data pulled from database - El Forum - 03-18-2009

[eluser]TheFuzzy0ne[/eluser]
If those are the only two fields you require, you'd be better off with a model like this:
Code:
function get_customers()
{
    /* customer selection query */
    $this->db->select('customerid, customerName');
    $this->db->order_by("customerName", "ASC");
    $res = $this->db->get('customers');
        
    return $res->result_array();
}

The above code is untested.


Form field data pulled from database - El Forum - 03-18-2009

[eluser]mdcode[/eluser]
Admittedly it's early in the day and I've only had one coffee, either that or I'm just dense, but I don't entirely get what you're asking me here. The foreach loop is to loop through the results and have them ready to be placed in the drop down; if I echo them, sure I get the results but they are just echoed at the top of the page - it delivers, just incorrectly.

I would be interested in how to modify the view in such a way though.

I have just tried Fuzzy's method and it almost works. When the drop down is active, the results display like this:
Code:
0 --- Displayed as an optgroup
1
Company 1
1 --- Displayed as an optgroup
2
Company 2
Basically, I only want only the name of the company to appear in the list (there will potentially be about 50 companies in this list) and when inputting the project into the database, the id of the selected company will get inserted into the project row.


Form field data pulled from database - El Forum - 03-18-2009

[eluser]jedd[/eluser]
Quote:The foreach loop is to loop through the results and have them ready to be placed in the drop down;

I don't know what you think 'have them ready' means.

The foreach loop you have shown does nothing - no variables are assigned, no data are changed. You are saying 'for each data in here, this data is here'. The code is moot, but nonetheless unsettling.

Fuzzy's code is option 2 from the list of three options I suggested we explore. If you're happy with that .. then it could be time to post the current state of your code again. Before you do, however, you may want to experiment some more with var_dump (or print_r) calls, to enquire on the nature of the variable you're passing into your view, just before you feed it into your drop_down function. The <pre> stuff I mentioned before was purely as an aid for readability - the print_r and var_dump output tends to be easier if you go into PREFORMATTED mode first, that's all.



Quote:
Code:
1 --- Displayed as an optgroup
2
Company 2
Basically, I only want only the name of the company to appear in the list (there will potentially be about 50 companies in this list) and when inputting the project into the database, the id of the selected company will get inserted into the project row.

Gotcha. Show us the bit in your controller where you call get_customers(), and the bit where you transfer that data to your view, and the bit in your view where you're trying to display this with form_dropdown().


Form field data pulled from database - El Forum - 03-18-2009

[eluser]mdcode[/eluser]
SUCCESS!!!

Viewing another load of source code, I finally have something that I can work with and it works the way I expected it to. More importantly, I think I can understand why this works and all my previous attempts failed, though I'm going to come up against some stiff opposition later on in the form too. Basically this is the code:

Controller:
Code:
function add()
    {
        $data['siteTitle'] = $this->config->item('siteTitle');
        
        /* grab the customers */
        $customers = $this->projects_model->get_customers();
        
            if ($customers->num_rows() > 0)
            {
                foreach ($customers->result() as $customer)
                {
                $data['values']['customers'][$customer->customerid] = $customer->customerName;
                }
            }
        
        $this->template->write_view('content', 'default/admin/projects_add', $data);
        $this->template->render();
    }

Model:
Code:
function get_customers()
    {
        $query = $this->db->get('customers');
        
        return $query;
    }
    
    function get_customer_name($id = '')
    {
        $query = $this->db->get_where('customers', array('customerid' => $id));
        
        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            
            return $row->customerName;
        }
    }

And View:
Code:
echo form_dropdown('customer', $values['customers']);

And this is the source code that it produces (a drop down list with customer/company id's as the values, but dispaying the corresponding customer name instead:
Code:
<select name="customer">
<option value="1">Company 1</option>
<option value="2">Company 2</option>
</select>

As ever, thanks to you jedd and the Fuzzy 0ne for your continued efforts in helping this noob. Credits to the community! In fact, is there any kind of reputation or thanks giving feature on here, I can't readily see one... I might be blind as well as dense though?


Form field data pulled from database - El Forum - 03-18-2009

[eluser]jedd[/eluser]
[quote author="mdcode" date="1237441879"]SUCCESS!!![/quote]

Bloody marvellous!


Quote:As ever, thanks to you jedd and the Fuzzy 0ne for your continued efforts in helping this noob. Credits to the community! In fact, is there any kind of reputation or thanks giving feature on here, I can't readily see one... I might be blind as well as dense though?

Nit-picking other people's code is reward enough. Wink

Actually, on that note, do you mind if I ... ?

Can you please post your database schema? I'll rattle up some comparison code, using a different approach.