CodeIgniter Forums
dropdown from database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: dropdown from database (/thread-78196.html)



dropdown from database - antiserious - 12-12-2020

Model:
public function getAllUnitsDropDown()
{
    $builder = $this->db->table('units');
    $query = $builder->select('unit_id, unit_name');

    if ($query->countAll() > 0) {
        foreach ( $query->getResultArray() as $row ) {
            $data [$row ['unit_id']] = $row ['unit_name'];
        }
    }
    $query->free_result ();
    return $data;
}


result:
Error

Call to undefined method CodeIgniter\Database\MySQLi\Builder::getResultArray()



RE: dropdown from database - vitnibel - 12-12-2020

Hi.

You forgot to call function get() after select() and you don't need to call countAll() function to check if the request was successful.

PHP Code:
public function getAllUnitsDropDown()
{
    $builder $this->db->table('units');
    $query $builder->select('unit_id, unit_name')->get();

    $data = [];

    if ($query) {
        foreach ( $query->getResultArray() as $row ) {
            $data [$row ['unit_id']] = $row ['unit_name'];
        }
    }

    $query->freeResult();

    return $data;




RE: dropdown from database - antiserious - 12-13-2020

Thanks.its works.