开发者

PHP Template Buffering in a Loop

I'm basically running a loop on a query and using PHP's buffering to insert a template file in the loop. The problem is if I have more than one result in my query I end up with two or more identical results (loop开发者_开发技巧s the same data rather than the next in the mysql result row).

the function...

function get_blocks(){
    $query = "SELECT * FROM `my_table`";
    $results = mysql_query($query);

    while($data = mysql_fetch_assoc($results){
        extract($data, EXTR_SKIP);
        ob_start();
        include(dirname(__FILE__) . '/templates/infoBlock.php');
        $returnString .= ob_get_clean();
    }
    echo $returnString;
}

The template looks like:

<div>
    Hi my name is <?php echo $name; ?>
    <br>
    and my age is <?php echo $age; ?>
</div>

So, if I have two records:

record 1
name: Joe
age: 42

record 2
name: Sam
age: 35

I end up with

Hi my name is Joe
and my age is 42


Hi my name is Joe
and my age is 42

instead of

Hi my name is Joe
and my age is 42


Hi my name is Sam
and my age is 35

I've already tried

echo ob_get_clean(); 

and taking $returnString out of the function to no avail...

thoughts?


I was overlooking the EXTR_SKIP parameter in the extract function. EXTR_SKIP causes any variables that are already set to NOT be overwritten (a bit of a security feature so far as I know)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜