fuel_model result into chunks
  • 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?

  • $cheeses should be an array already. Is the following not working for you?
    $cheeses = fuel_model('cheeses');
    $chunks = array_chunk($cheeses);
  • 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);

    It works, but with print_r($cheeses) or even var_dump($cheeses), I get blank screen.
    foreach on $cheeses, works though.
  • 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?
  • Yes, it echos the number. So how do I use array_chunk with $cheeses?
  • 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?
  • Hi,
    This is what I tried:
    $x = array_chunk($cheeses, 3);

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

    foreach($x as $y) {

    I get a white page.


    I get a white page.
  • array_chunk creates nested arrays so you'll need another foreach loop.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!