0

I have browsed around the suggested titles and found some answers but nothing that really worked, and so I turn to you...

I have a function that uses ob_start() to call a file from which the contents are used as a skeleton. Once the contents have been retrieved I use ob_end_clean().

I only seem to be getting output from the first time I call the function and nothing more afterwards. I have included a code dump in case I am doing something wrong.

I have also included a sample of what is returned from my database call ($dl->select ...) I have also made sure that data is indeed being passed back from the database where I am expecting it to.

Array
(
[0] => Array
    (
        [rev_id] => 7
        [rev_temp_tree_id] => 2
        [rev_tree_id] => 
        [rev_status] => 
        [rev_last_updated_by] => 0
        [rev_authorized_by] => 
        [rev_date_updated] => 1334600174
        [rev_date_reviewed] => 
        [rev_update_type] => 1
        [temp_tree_id] => 2
        [temp_tree_bag_size] => 250
        [temp_tree_botanical_id] => 
        [temp_tree_stem_size] => 0
        [temp_tree_crown] => 0
        [temp_tree_price] => 0
        [temp_tree_height] => 0
        [temp_tree_plant_date] => 0
        [temp_tree_review_id] => 
        [temp_tree_comments] =>  
        [temp_tree_marked_move] => 0
        [temp_tree_initial_location] => 
        [temp_tree_coord] => 
        [temp_tree_name] => TEST
        [temp_tree_sale_status] => 0
        [temp_tree_open_ground] => 
        [temp_tree_block] => 0
        [temp_tree_row] => 0
    )

)

and the code...

<?php

function print_trees($trees){
 $return = '';

 ob_start();
 include_once('skeletons/tree.html');
 $tree_skeleton= ob_get_contents();
 ob_end_clean();

 $search=array("[tree_id]", "[tree_name]", "[Classes]", "[rev_id]");    
 foreach($trees as $t){     
    $replace=array($t['temp_tree_id'], $t['temp_tree_name'].' ['.$t['temp_tree_id'].']', 'temp_tree', $t['rev_id']);
    $return.=str_replace($search,$replace,$tree_skeleton);
 }
 return $return;
}

switch ($_GET['mode']){
case 'trees' : 
    $db_status = '';

    $new_trees = $dl->select('tree_review AS tr LEFT JOIN temp_tree_trees AS tt ON tr.rev_temp_tree_id=tt.temp_tree_id', 'tr.rev_update_type="1"');
    echo '<h2>New Trees</h2>';
    if($dl->totalrows>0){
        echo print_trees($new_trees);
    }
    else{
        echo 'no new trees for review';
    }
    echo '<br /><br />';

    $new_trees = $dl->select('tree_review AS tr LEFT JOIN temp_tree_trees AS tt ON tr.rev_tree_id=tt.temp_tree_id', 'tr.rev_update_type="2"');
    echo '<h2>Updated Trees</h2>';
    if($dl->totalrows>0){
        echo print_trees($new_trees);
    }
    else{
        echo 'no update trees for review';
    }
    echo '<br /><br />';

    $new_trees = $dl->select('tree_review AS tr LEFT JOIN temp_tree_trees AS tt ON tr.rev_tree_id=tt.temp_tree_id', 'tr.rev_update_type="3"');
    echo '<h2>Moved Trees</h2>';
    if($dl->totalrows>0){
        echo print_trees($new_trees);
    }
    else{
        echo 'no moved trees for review';
    }
    echo '<br /><br />';

    $new_trees = $dl->select('tree_review AS tr LEFT JOIN temp_tree_trees AS tt ON tr.rev_tree_id=tt.temp_tree_id', 'tr.rev_update_type="4"');
    echo '<h2>Duplicated Trees</h2>';
    if($dl->totalrows>0){
        echo print_trees($new_trees);
    }
    else{
        echo 'no duplicated trees for review';
    }

break;
}
?>

Any help would be appreciated.

Thanks in advance.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Nick
  • 93
  • 1
  • 1
  • 6

2 Answers2

2

I believe it could be one of two things:

You might be having trouble with the fact that you're representing a multi-dimensional array as a string in the HTML file, and then attempting to operate on that with a string replace. You might be better off representing the file as a data structure (XML, JSON) and parsing apart that way - this will let you skip output buffering entirely.

Alternately, I'm not sure if $new_trees is an array of objects or something else. If it's an array of objects, the foreach() loop isn't going to work correctly i.e. it should be $t->temp_tree_id vs. $t['temp_tree_id']

gadhra
  • 21
  • 1
1

thanks for your comments.

I found out what the problem was, it was rather stupid actually. The file that I am importing as a skeleton is included using include_once, so once I try to call it again it won't let me.

@minitech I updated my code as you suggested, thanks.

@gadhra the content in the skeleton file is plain html and im using keywords to replace the content from the db into the html. I should have attached the html along with my code.

Thanks again :)

Nick
  • 93
  • 1
  • 1
  • 6