By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

ob_start(), ob_end_clean() and header() combination produces WARNING

P: n/a
Based upon the following articles:

http://www.experts-exchange.com/Web/..._20708448.html
http://www.experts-exchange.com/Web/..._20717295.html
http://groups.google.com/groups?hl=e...com%26rnum%3D2
http://groups.google.com/groups?hl=e...com%26rnum%3D4
http://www.phpfreaks.com/phpmanual/p...on.header.html

The class PHP script html.inc has as its constructor method a means by
which it has to call an outside function, preSelect, to determine
whether or not to insert " checked" (or in this case, " selected")
into the written dropdown. The existing function is written to ECHO
the string onto the page. Since preSelect is required within the
scope of my constructor method that produces dategroup dropdowns, I
knew I would have to encase the preSelect command inside output
buffering to capture it and send it to a variable.

$html = '<select name="' . $enumKeyValArray[$i][0] . '">';
foreach($monthArray as $innerKey => $innerVal) {
ob_start();
preSelect($enumKeyValArray[$i][1], $innerKey);
$preSelectText = ob_get_contents();
ob_end_clean();
$html .= '<option value="' . $innerKey . '" ' . $preSelectText
.. '>' . $innerKey . ' - ' . $innerVal . '</option>';
}
$html .= '</select>';

Everything works fine.. however, there is a bit of a bubble in my
logic.

There is a form produced, view_internship_applicant.html, that will
incorporate this class method into its parent script, viewinterns.php.
When the user clicks SUBMIT, they are redirected back to
viewinterns.php to do form processing. Everything is done just
right...

...until you get to

header("Location: viewinterns.php");

Then I get a WARNING thrown "Headers already included on ../html.inc
line 162" which happens to be this line:

ob_start();

When you clicked SUBMIT you went to the output buffering and then you
go to header(), which apparently you can't do both. My workaround was
to replace the header() commands in viewinterns.php to echo HTML meta
redirection tags instead, which works, but for some reason the page
now takes 15 - 20 seconds to pull up.

I'm not sure what I must do; I was told based on what I read to move
the ob_end_clean() command after header(), but that would fail the
entire scope of the existing class, which I lovingly provided for you
to see what I mean:

-----------------------------------------------------------------------------------------------

class DateGroupHTMLGenerator {
var $propertyArray, $htmlArray,
$hasMonth, $hasDay, $hasYear;

// CONSTRUCTOR
function DateGroupHTMLGenerator($classPropertyArray, $hasMonth = 1,
$hasDay = 1, $hasYear = 1) {

foreach(array('month', 'day', 'year') as $key => $val) $this->{'has'
.. ucfirst($val)} = ${'has' . ucfirst($val)};

$this->propertyArray = $classPropertyArray;

// CONVERT THE INPUT PARAMETER ARRAY INTO A 2-DIM ENUMERATIVE ARRAY
TO PRESERVE KEYS AND VALS AND HAVE ENUMERATION FOR SWITCH
$enumKeyValArray = array();
foreach($this->propertyArray as $key => $val)
array_push($enumKeyValArray, array($key, $val));

// GET THE VALUE OF THE LAST ELEMENT OF THE INPUT PARAMETER ARRAY
WHICH WILL BE THE TEXT FIELD LENGTH
$textFieldLengthArray =
array_values(array_reverse($this->propertyArray));
$textFieldLength = $textFieldLengthArray[0];

$this->htmlArray = array();

$monthArray = array('01' => 'January',
'02' => 'February',
'03' => 'March',
'04' => 'April',
'05' => 'May',
'06' => 'June',
'07' => 'July',
'08' => 'August',
'09' => 'September',
'10' => 'October',
'11' => 'November',
'12' => 'December'
);
for ($i = 0; $i < sizeof($enumKeyValArray); $i++) {
$html = '';
switch ($i) {
case '2': // TEXT FIELD SLOT IN INPUT ARRAY PARAMETER
if ($this->hasYear) {
if ($textFieldLength == 2) $html = '20';
$html .= '<input name="' . $enumKeyValArray[$i][0] . '"
value="' . str_replace('"', '&quot;', $enumKeyValArray[$i][1]) . '"
size="' . $textFieldLength . '">';
}
break;

case '3': // SIZE OF TEXT FIELD - NOTHING NEEDS TO BE DONE
// DO NOTHING
break;

case '0': // MONTH DROPDOWN
if ($this->hasMonth) {
$html = '<select name="' . $enumKeyValArray[$i][0] . '">';
foreach($monthArray as $innerKey => $innerVal) {
ob_start();
preSelect($enumKeyValArray[$i][1], $innerKey);
$preSelectText = ob_get_contents();
ob_end_clean();
$html .= '<option value="' . $innerKey . '" ' . $preSelectText
.. '>' . $innerKey . ' - ' . $innerVal . '</option>';
}
$html .= '</select>';
}
break;

case '1': // DAY DROPDOWN
if ($this->hasDay) {
$html = '<select name="' . $enumKeyValArray[$i][0] . '">';
for ($j = 1; $j <= 31; $j++) {
$myDay = ($j < 10) ? '0' . $j : $j;
ob_start();
preSelect($enumKeyValArray[$i][1], $myDay);
$preSelectText = ob_get_contents();
ob_end_clean();
$html .= '<option value="' . $myDay . '" ' . $preSelectText . '>'
.. $myDay . '</option>';
}
$html .= '</select>';
}
break;

default: // THIS WILL BE EXPANDED FOR FUTURE IMPLEMENTATION OF
DATEGROUP, FOR NOW IF ANYTHING ELSE IS FOUND DO NOTHING
// DO NOTHING
break;
}

$this->htmlArray[$i] = $html;
}
}

function getMonthDropdown() { // STRING "METHOD" TO PRODUCE MONTH
DROPDOWN
return $this->htmlArray[0];
}

function getDayDropdown() { // STRING "METHOD" TO PRODUCE DAY
DROPDOWN
return $this->htmlArray[1];
}

function getYearText() { // STRING "METHOD" TO PRODUCE YEAR TEXT
FIELD
return $this->htmlArray[2];
}

}

---------------------------
class DateGroupHTMLGenerator from .../html.inc included within
viewinterns.php

Has anyone here ever worked with output buffering and redirection
combinations in PHP before? What advice would you give me based on
what you see and what I have learned so far?

Thanx
Phil
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.