Welcome Guest, Not a member yet? Register   Sign In
list database result in array
#1

[eluser]fRkSsK[/eluser]
hi,

this is a default code

Code:
$this->load->helper('html');

$list = array(
            'red',
            'blue',
            'green',
            'yellow'
            );

$attributes = array(
            'class' => 'boldlist',
            'id'    => 'mylist'
            );

echo ul($list, $attributes);

i want to list these in database table. i tried;

Code:
$xxx = $this->db->get('table1');
$list = $xxx->result();

    $attributes = array(
        'class' => 'boldlist',
        'id'    => 'mylist'
        );
            
echo ul($list, $attributes);

but it's not work. can you check this?
#2

[eluser]Dam1an[/eluser]
I'm assuming the problem is that the ul function expects a single dimensional array, and you're passing it a 2D array
#3

[eluser]Unknown[/eluser]
Re-sort your database result array first. And by the way $xxx->result(); returns a class not an array.
#4

[eluser]fRkSsK[/eluser]
so,
how can i change database results to array from class?
or
how does this code work?
#5

[eluser]Dam1an[/eluser]
I think it would actually be easier to just do it yourself then changing the db result to work with the ul function
Something like*
Code:
function make_ul($items, $field) {
    if(sizeof($items) == 0) {
        return null;
    }
    
    $str = '<ul>';
    
    foreach($items as $item) {
        str .= '<li>'.$item->$field.'</li>';
    }
    
    return $str.'</ul>';
}

// Call it like this
echo make_ul($xxx->result(), 'field');

Where field is the name of the database field you want to print in the list

* Not tested




Theme © iAndrew 2016 - Forum software by © MyBB