fuel_model result into chunks

edited February 2018 in Modules
I am using fuel_model in my block like this:
$cheeses = fuel_model('cheeses');

I would like to break the result I get into chunks with php_chunk, but it doesn't work.

How can I convert the result into an array?

Thx

Comments

  • edited 5:47AM
    $cheeses should be an array already. Is the following not working for you?
    $cheeses = fuel_model('cheeses'); $chunks = array_chunk($cheeses);
  • edited 5:47AM
    Hi,
    For some reason it doesn't work:
    $cheeses = array_chunk($cheeses);
    gives me blank screen.

    Even if I try: print_r($cheeses) I get blank screen.

    If I try this:
    $x[0] = array('name'=>'Bob', 'age'=>10);
    $x[1] = array('name'=>'Brad', 'age'=>20);
    $x[2] = array('name'=>'Gill', 'age'=>30);
    $x[3] = array('name'=>'Steve', 'age'=>40);
    $x = array_chunk($x, 3);
    print_r($x);

    It works, but with print_r($cheeses) or even var_dump($cheeses), I get blank screen.
    foreach on $cheeses, works though.
  • edited 5:47AM
    The model records have a reference to the $CI object in it which causes a recursive loop when printed out to the screen and may explain your blank screen. Does echo count($cheeses); display anything?
  • edited 5:47AM
    Yes, it echos the number. So how do I use array_chunk with $cheeses?
  • edited 5:47AM
    Is this line the one causing the blank screen:
    $cheeses = array_chunk($cheeses);
    Or is it a different line? If you are able to get a count value then it sounds like it's an array value that could call array_chunk. Is the error further down?
  • edited 5:47AM
    Hi,
    This is what I tried:
    $x = array_chunk($cheeses, 3);

    1.
    foreach($x as $y) {
    echo $y->cheese_name;
    }
    I get this error on the echo line:
    Message: Trying to get property of non-object

    2.
    foreach($x as $y) {
    print_r($y);
    }

    I get a white page.

    3.
    print_r($x);

    I get a white page.
  • edited February 2018
    array_chunk creates nested arrays so you'll need another foreach loop.
Sign In or Register to comment.