Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-05-30 02:36:04

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Output buffering - foreach?

Does anyone know why this doesn’t work (from this thread)? It seems like foreach isn’t executed.

<?php
if (txpinterface == 'admin')
{
    register_callback('jmd_form_toggle_init', 'form', '', 1);
}

function jmd_form_toggle_init($event, $step)
{
    ob_start('jmd_form_toggle');
}
//add headers to the form table
function jmd_form_toggle($buffer)
{
    $rs = safe_rows("name, type", "txp_form", "name != '' ORDER BY type");
    $forms = array();
    foreach ($rs as $form)
    {
        if (!array_key_exists($form['type'], $forms))
        {
            $forms[$form['type']] = array($form['name']);
        }
        else
        {
            array_push($forms[$form['type']], $form['name']);
        }
    }

    $out .= tr(tda(sLink('form', 'form_create', gTxt('create_new_form')), 
          ' colspan="2"'));

    $types = array_keys($forms);
    foreach ($types as $type)
    {
        $out .= '<tr>
            <th colspan="2" onclick="toggleDisplay(\'' . $type . '\')">
            ' . $type . '</th></tr><tbody id="' . $type . '">';
        for ($i = 0; $i < count($forms[$type]); $i++)
        {
            $form = $forms[$type][$i];
            $out .= '<tr><td>';
            if (gps('name') == $form)
            {
                $out .= $form;
            }
            else
            {
                $out .= href($form, '?event=form&amp;name=' . $form);
            }
            $out .= '<td><input type="checkbox" name="selected_forms[]" 
                value="' . $form . '"/>
                </td></tr>';
        }
        $out .= '</tbody>';
    }
    $out .= '</table>';
    $pattern = '<tr><td colspan="3" style="height:30px">(.*)<input type="hidden" name="event" value="form" />';
    //FIXME: preg_replace, yo
    return ereg_replace($pattern, $out, $buffer);
}
?>

Offline

#2 2008-06-14 15:50:47

net-carver
Archived Plugin Author
Registered: 2006-03-08
Posts: 1,648

Re: Output buffering - foreach?

Jon-Michael,

sorry for the delay in posting a reply but only just saw this one.

If you are on php5 or above you will need to do this at the head of your output buffer routine…

function jmd_form_toggle($buffer)
{
 	global $DB;
	$DB = new DB;
	$rs = safe_rows("name, type", "txp_form", "name != '' ORDER BY type");
	...

… as objects (ie the global DB object used by safe_rows ) are destroyed before the output buffer routines are called. As it stands, on php5, you’ll get an empty $rs back and your foreach won’t do a thing.

If you want it php4/php5 then use…

	if( !isset( $DB) )
		$DB = new DB;

… instead of the $DB = new DB; line.

edit: Added php4/5 example.

Last edited by net-carver (2008-06-14 16:09:09)


Steve

Offline

#3 2008-06-15 16:03:34

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: Output buffering - foreach?

D’oh! That did it – thanks Steve!

Offline

#4 2008-06-15 16:17:21

net-carver
Archived Plugin Author
Registered: 2006-03-08
Posts: 1,648

Re: Output buffering - foreach?

You’re welcome.

That behavior change from php 4 to 5 had me stumped for ages with the MLP pack.


Steve

Offline

Board footer

Powered by FluxBB