Welcome Guest, Not a member yet? Register   Sign In
Select options generated from Database
#1

[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() ;
        $this->db->select('report');
        $this->db->where('Company','1');
        $this->db->from('policy') ;
        $query = $this->db->get() ;

        if ($query->num_rows() > 0) ;
        {
             $row = $query->row_array('report') ;
             $data['report'] = $row['report'] ;
        }

        $this->db->select('ACCT','DESCR');
        $this->db->where('CLASS','500');
        $this->db->from('accts') ;
        $query2 = $this->db->get() ;

//          ('0' => '--Select One--') ;
        if ($query2->num_rows() > 0)
        {     foreach ($query2->result() as $row )
         {
>> Error Line >>   $acct_array[$query2->$row_id] = $query2->row_array('ACCT' => 'DESCR') ;

         }
        }
        $this->load->view('load_post_view',$data);

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
#2

[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 ;
#3

[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)
{

   $xlink = getlink('link') ;

   $sql = "select ACCT, DESCR from accts where CLASS = 500 ";
   $accts = mysql_query("$sql") ;

    echo  "<option selected value='0'>--Select One--</option>\n" ;

   while (list($acct_num, $acct_descr) = mysql_fetch_row($accts)) {

   if ($acct_num == $account) :
    echo  "<option selected value='$acct_num'>$acct_descr</option>\n" ;
   else   :
    echo  "<option value='$acct_num'>$acct_descr</option>\n" ;
   endif  ;
   }
   mysql_close($xlink);
}

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
#4

[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;
#5

[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

Severity: Notice

Message: Undefined property: stdClass::$DESCR

Filename: controllers/load_post.php

Line Number: 42

Line 42 is the noted line below.

Code:
if ($query2->num_rows() > 0)
        {     foreach ($query2->result() as $row )
         {
//         $acct_array[$query2->$row_id] = $query2->row_array('ACCT' => 'DESCR') ;
>> Line 42 >>        $acct_array[$row->ACCT] = $row->DESCR;
         }
        }
        $this->load->view('load_post_view',$data);

Something else is not quiet right yet. Thanks for you help on this. Its appreciated.
#6

[eluser]Cadu de Castro Alves[/eluser]
Code:
if ($acct_num == $account) :
    echo  "<option selected value='$acct_num'>$acct_descr</option>\n" ;
else :
    echo  "<option value='$acct_num'>$acct_descr</option>\n" ;
endif;

You can write the above code as following:

Code:
$selected = ($acct_num == $account) ? "selected='selected'" : "";

echo "<option value='$acct_num' $selected>$acct_descr</option>\n";
#7

[eluser]Phil Sturgeon[/eluser]
Do a print_r($query2->result_array()) before the foreach to see if it has all the data.
#8

[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 )
[2] => Array ( [ACCT] => 5200 ) [3] => Array ( [ACCT] => 5110 ) [4] => Array ( [ACCT] =>
5120 ) [5] => Array ( [ACCT] => 5130 ) [6] => Array ( [ACCT] => 5140 ) [7] => Array ( [ACCT]
=> 5145 ) [8] => Array ( [ACCT] => 5125 ) [9] => Array ( [ACCT] => 5101 ) [10] => Array (
[ACCT] => 5102 ) [11] => Array ( [ACCT] => 5103 ) [12] => Array ( [ACCT] => 5104 ) [13] =>
Array ( [ACCT] => 5126 ) [14] => Array ( [ACCT] => 5135 ) [15] => Array ( [ACCT] => 5105 )
[16] => Array ( [ACCT] => 5106 ) [17] => Array ( [ACCT] => 5107 ) [18] => Array ( [ACCT] =>
5136 ) [19] => Array ( [ACCT] => 5250 ) [20] => Array ( [ACCT] => 5010 )
[21] => Array ( [ACCT] => 5400 ) [22] => Array ( [ACCT] => 5300 ) )

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');
        $this->db->where('CLASS','500');
        $this->db->from('accts') ;
        $query2 = $this->db->get() ;

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.
#9

[eluser]BitRanch[/eluser]
Quote:
Code:
$this->db->select('ACCT','DESCR');

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');
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB