Drop-down with option group |
[eluser]mdcode[/eluser]
Hopefully this one is going to be easier than the last but it's not quite working the same way as the normal drop-downs on my form. Here's the issue: I am using the form helper to create a drop-down menu with results pulled from the database, however those results should be pulled from two tables, one for types, and one for subtypes. Here's tha table schema: Code: +-----------+-------------+ Now this query using straight SQL in phpmyadmin works: Code: SELECT a.ssid, a.substrate, b.subtypeid, b.ssid, b.subType FROM lip_sstype AS a, lip_sssubtype AS b WHERE a.ssid = b.ssid ORDER BY a.substrate, b.subType ASC Here is my model: Code: function get_substrates($substrate = '') Here is my controller: Code: $substrates = $this->projects_model->get_substrates(); And finally the error I am getting: Code: Call to a member function num_rows() on a non-object I'm guessing there is something wrong in the query in my model, but however I have been trying to formulate the query (I've tried too many to remember or list), it fails with errors like the above, or "Unknown table" when the table is there and checked to being there 3 times, or "Ambiguous column 'ssid'" but I think I get that one. Anywho, this is the result that I would like to see when looking at the source code of the page: Code: <select>
[eluser]bretticus[/eluser]
Please post your complete model. Is it named "projects_model" or is it "Projects_model" with the initial cap?
[eluser]mdcode[/eluser]
Apologies for the lack of reply, apart from the weekend, I have been off sick but that's by the by... Here is the complete model file named "projects_model.php": Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]mdcode[/eluser]
Ok, I'm attempting to go back to basics on this so have come back to the base SQL query that works and is tested to be working in phpmyadmin: Code: $this->db->query("SELECT ss.*, st.* FROM sstype AS ss, sssubtype AS st WHERE ss.ssid = st.ssid ORDER BY ss.substrate, st.subType"); Code: A Database Error Occurred Code: A Database Error Occurred I could really use some help here, I'm not feeling too good and this is not making things any better for me. Thanks.
[eluser]bretticus[/eluser]
You're returning a string from get_substrates(). You can't call a method ( num_rows() ) on a non-object (a string.)
[eluser]mdcode[/eluser]
But the string is just a variable name to display the result, i.e. Code: $string = 'Paper - Thermal';
[eluser]bretticus[/eluser]
I haven't looked at your code in depth but if I were you I'd make two calls (one to each table) and then return each as an array. Use CI's database class to call result_array(). Then use the native php function array_combine to build a new associative array. Return that and send it to your view, It'll be ready made for using the form helper function: form_dropdown(). Now, if you need opt groups and such, I suppose you'll have to build the select out yourself.
[eluser]bretticus[/eluser]
[quote author="mdcode" date="1237875445"]But the string is just a variable name to display the result, i.e. Code: $string = 'Paper - Thermal'; Are you still using this code: Code: $substrates = $this->projects_model->get_substrates(); If get_substrates() returns a string, you most definitely cannot call $substrates->num_rows().
[eluser]mdcode[/eluser]
In the controller, yes I am, but with the errors that I am recieving right now it won't make a difference what's in the controller as it's not even getting that far, since it apparently can't run a perfectly valid query in the model.
[eluser]mdcode[/eluser]
[quote author="bretticus" date="1237875570"]I haven't looked at your code in depth but if I were you I'd make two calls (one to each table) and then return each as an array. Use CI's database class to call result_array(). Then use the native php function array_combine to build a new associative array. Return that and send it to your view, It'll be ready made for using the form helper function: form_dropdown(). Now, if you need opt groups and such, I suppose you'll have to build the select out yourself.[/quote] I'll have to look into this and try it out, and by this I take it by your experience that this cannot be done in the one or two functions as all the others queries are? Optgroups aren't really essential, but it would make things easier when viewing the drop-down and I'm wondering if this is a let down of the CI form helper function...? |
Welcome Guest, Not a member yet? Register Sign In |