![]() |
Select options generated from Database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Select options generated from Database (/showthread.php?tid=6242) Pages:
1
2
|
Select options generated from Database - El Forum - 02-19-2008 [eluser]Brent M[/eluser] HI: I'm new to CI, but relatively experienced with PHP. The app I'm retooling to work with CI used to use INCLUDES to accomplish allot of what I needed. The item I'm having problems with is the generation of the selections options into an array to then be used in a form drop down in the view. I'm getting a syntax error around the '=>' usage. I believe I've got the code formatted properly [??], but then .. "Its always something stupid". The code in my Model file: Code: $this->load->database() ; The reported 'offence' is on the >> Error Line >> A hint, or even a kick in the pants to get me going in the right direction would be appreciated. Thanks for your help Brent Select options generated from Database - El Forum - 02-19-2008 [eluser]Phil Sturgeon[/eluser] Yea the => shouldnt be there at all. You are selecting one key of the row to show in the option text. Try something like this. Code: $acct_array[$query2->$row_id] = $query2->row()->FIELD_NAME ; Select options generated from Database - El Forum - 02-19-2008 [eluser]Brent M[/eluser] Thanks, but although the description [field DESCR] is what actually will show in the drop down, the option value needs to the account number [field ACCT ]. In my "include world" I had this handled like this. Code: function get_acct ($account) I just need to replicate this using CI. I need the ACCT as the Option Value, followed by the DESCR as text. Just using the DESCR field I don't think will do it alone. I will try your suggestion though to see what I do get, and perhaps work it out from there Thanks Select options generated from Database - El Forum - 02-20-2008 [eluser]Phil Sturgeon[/eluser] Well take out that weird key index you are setting, and use the $row variable thats already being set in your foreach. Code: $acct_array[$row->ACCT] = $row->DESCR; Select options generated from Database - El Forum - 02-20-2008 [eluser]Brent M[/eluser] OK.. That seems to make sense. I've put in the code and moved on to a new error. Code: A PHP Error was encountered Line 42 is the noted line below. Code: if ($query2->num_rows() > 0) Something else is not quiet right yet. Thanks for you help on this. Its appreciated. Select options generated from Database - El Forum - 02-20-2008 [eluser]Cadu de Castro Alves[/eluser] Code: if ($acct_num == $account) : You can write the above code as following: Code: $selected = ($acct_num == $account) ? "selected='selected'" : ""; Select options generated from Database - El Forum - 02-20-2008 [eluser]Phil Sturgeon[/eluser] Do a print_r($query2->result_array()) before the foreach to see if it has all the data. Select options generated from Database - El Forum - 02-20-2008 [eluser]Brent M[/eluser] I put the print_r code in as you asked and received this Code: Array ( [0] => Array ( [ACCT] => 5000 ) [1] => Array ( [ACCT] => 5100 ) Which would be an array listing of the ACCT [account] values , but not of the DESCR [descriptions]. The Select in this case would appear to not be providing the DESCR field. Is my analysis on this correct ? Here is the select stuff again. Code: $this->db->select('ACCT','DESCR'); And before anyone asks, Yes I've checked the Mysql table, name, and field names for spelling issues. The filter is working as the ACCT values selected are all 'CLASS' 500. Thanks. Select options generated from Database - El Forum - 02-20-2008 [eluser]BitRanch[/eluser] Quote: db->select only takes a single string (very carefully examine the example in the user guide), so you're just getting the ACCT column. Try this: Code: $this->db->select('ACCT, DESCR'); Select options generated from Database - El Forum - 02-21-2008 [eluser]Brent M[/eluser] Ah, that would be IT then. Gee, someone should the authors of 'Codeigniter for Rapid PHP Application Development' that the syntax in Chapter 4 (page 56 to be precise) is not up to spec. Now its got me wondering what else in there might not be quite right. Thanks. Now that I can actually get the missing field, I think I can muddle on through. |