Select box

Permalink 1 user found helpful
Hi,

Im trying to create a selectbox that shows every item on the table but instead of showing all 10 values correctly, it shows "ARRAY" for each of the 10 items.

Code from the controller:
function getChurch()
       {
          $db = Loader::db();
          $items = array();
          $items=$db->GetArray("SELECT * FROM {$this->btChurch}");
          return $items;   
      }

Code from the form:
<td><?php echo t('Church')?>&nbsp;</td>
<td><?php echo $form->select('nomEglise', $Obj->getChurch(), 'p',array('style' => 'width: 200px')); ?></td>


Thanks for your help.

 
itrio replied on at Permalink Reply
itrio
The call to get array will give you one element for each row found, and each element is itself a new array with the values for each field in the row.

Example output from GetArray:
array('0'=>array('id'=>1, 'name'=>'Lars'), '1'=>array('id'=>2, 'name'=>'Truls'))

I recommend to look at the output by writing the array to webbrowser:
print_r($items);


Another example, if you need all church names in one array, you have to do something like this with your output from function GetArray:
$churchNames = array(); // init array
// Go through all rows
foreach ($items as $row) {
  $churchNames[] = $row['churchName'];
}


God bless! ;)
Xenavar replied on at Permalink Reply
That works, thank you.

However, I need to save the ID of the church selected in my database table, not the name. What is the best way to do that?
Xenavar replied on at Permalink Reply
Bump. I am at work and I can't find the answer by myself.

Current code from the form:
<td><?php echo t('Church')?>&nbsp;</td>
<td><?php echo $form->select('churchID', $Obj->getChurch(), 'p',array('style' => 'width: 200px')); ?></td>

Current code from the controller:
function getChurch()
       {
          $db = Loader::db();
          $items = array();
          $item=$db->GetArray("SELECT * FROM {$this->btChurch}");
          foreach ($item as $row) {
           $items[] = $row['churchName'];
          }
          return $items;
      }


It works as it shows the name of church in the list, but won't save as it doesn't know the ID.
itrio replied on at Permalink Reply
itrio
I am not sure how your script works, but to get the church ID from the array $items, you can
// 0 is here the row number, 0 is first, the last is count($items)-1;
$churchID = $items[0]['churchID'];

or in the foreach loop:
$churchID = $row['churchID'];


I suspect that you need the webpage to show a list of the churches, but the value returned from the selection is supposed to be the churchID?
Xenavar replied on at Permalink Reply
Yes, exacly. Can you detail the code a little bit more?
Xenavar replied on at Permalink Reply
paalgg, what you said is exacly what it needs to do. Could you help me more?

This is what I am trying to do...
<?php
    function getChurch()
       {
          $db = Loader::db();
          $item=$db->GetArray("SELECT * FROM {$this->btChurch}");
          foreach ($item as $row) {
           $items[] = array($row['churchID'] => $row['churchName']);
          }
          return $items;
      }

And in the form...
<td><?php echo t('Church')?>&nbsp;</td>
<td><?php echo $form->select('churchID',$Obj->getChurch(),array('style' => 'width: 200px')); ?></td>
Gingebean replied on at Permalink Reply
Gingebean
Hi,

I know this is old post but here is a way to do it

$items[$row['churchID']] = $row['churchName']
.

GB
INTcommunications replied on at Permalink Reply
INTcommunications
Which version of this ended up working? ( all the code )