A curious dilemma:
I have a few classes that I developed for implementing HTML based forms. So far the whole thing works beautifully. I ran into a bug a little while ago and am perplexed in just how to go around it.
The bug involves accessing a multi-dimensional array through a for loop, and well I'll just post these examples:
Script 1 is within a class that inherits from a parent and grandparent class.
Code:
$_POST["paypal"] = 1;
$this->index[$i] = $i;
$this->description[$i] = "Which methods of payment would you like to accept?";
$this->type[$i] = "checkbox";
$this->value[$i] = array();
$this->value[$i][0] = $_POST["paypal"];
# When this value is outputted it behaves as expected
# Outputting the value of 1
echo $_POST["paypal"]."<br />";
echo $this->value[$i][0]."<br />";
$this->value[$i][1] = $_POST["yahoo"];
$this->value[$i][2] = $_POST["ecount"];
$this->value[$i][3] = $_POST["postal_mail"];
$this->options[$i][0] = 1;
$this->options[$i][1] = 1;
$this->options[$i][2] = 1;
$this->options[$i][3] = 1;
$this->option_text[$i][0] = "<a href=\"https://www.paypal.com/affil/pal=6ZVJSG5LDFFH6\" target=\"_blank\">Paypal</a>";
$this->option_text[$i][1] = "<a href=\"http://paydirect.yahoo.com\" target=\"_blank\">Yahoo! Pay Direct</a>";
$this->option_text[$i][2] = "<a href=\"http://www.ecount.com\" target=\"_blank\">Ecount</a>";
$this->option_text[$i][3] = "Postal Mail";
$this->name[$i][0] = "paypal";
$this->name[$i][1] = "yahoo";
$this->name[$i][2] = "ecount";
$this->name[$i][3] = "postal_mail";
$i++;
Not that its really relevant but the third class is basically a configuration file that accesses static variables in the prior two. In this case all relevant variables are static variables set in the grandfather class.
Being a 1800 line file I'll just post the relevant parts which access this array as set in the accessing class
Code:
# snip snip
case "checkbox":
if ($this->do_action == "create_form") {
$form .= $this->checkbox_input($i);
} else {
if (is_array($this->option_text[$i])) {
for ($r = 0; each($this->option_text[$i]); $r++) {
echo $this->value[$i][$r]."<br />";
echo $this->options[$i][$r]."<br />";
if ($this->value[$i][$r] == $this->options[$i][$r]) {
$form .= $this->option_text[$i][$r].": ".$this->bool_true[$i]."<br />";
} else {
$form .= $this->option_text[$i][$r].": ".$this->bool_false[$i]."<br />";
}
}
} else {
if ($this->value[$i] == $this->options[$i]) {
$form .= $this->option_text[$i];
} else {
$form .= $this->bool_false[$i];
}
}
}
break;
# snip snip
Essentially in the for loop above I expect the accessing loop to substitue the relevant values if the use has checked the box. Instead of the expected output I get the following:
($this->value[$i][0]) A
($this->options[$i][0]) 1 (options output as expected)
($this->value[$i][1]) r
($this->options[$i][1]) 1
($this->value[$i][2]) r
($this->options[$i][2]) 1
($this->value[$i][3]) a
($this->options[$i][3]) 1
Well I began to recognize a pattern going on here, each value will eventually spell out Array, well remembering the section on 'String access by character' in the PHP manual
http://www.php.net/manual/en/language.types.string.php I remembered the deprecated bracket syntax used for doing this. However, strangely enough, this is not consistent with the other multi-dimensional arrays that are being used. And explicitly defining the $this->value[$i] as an array did not help.
Code:
function checkbox_input ($i) {
$input = "";
if (!is_array($this->name[$i])) {
if (isset($this->default_option[$i]) && !empty($this->default_option[$i])) {
if (!isset($this->options[$i]) || empty($this->options[$i])) {
$this->options[$i] = $this->default_option[$i];
}
}
if ($this->options[$i] == $this->value[$i]) {
$checked = " checked=\"checked\"";
}
$input .= "\n<input type=\"checkbox\" name=\"{$this->name[$i]}\" value=\"{$this->options[$i]}\"".$checked." />".$this->option_text[$i]."\n";
} else {
if (isset($this->default_option[$i]) && !empty($this->default_option[$i])) {
if (!isset($this->value[$i]) || empty($this->value[$i])) {
$this->value[$i] = $this->default_option[$i];
}
}
for ($n = 0; each($this->name[$i]); $n++) {
$option = "\n<input type=\"checkbox\" name=\"{$this->name[$i][$n]}\" value=\"{$this->options[$i][$n]}\"";
echo "--------- checkbox function -----------<br />";
echo $this->value[$i][$n]."<br />";
echo $this->options[$i][$n]."<br />";
if ($this->value[$i][$n] == $this->options[$i][$n]) {
$option .= " checked=\"checked\"";
}
$option = $option." />{$this->option_text[$i][$n]}<br />\n";
$input .= $option;
}
}
return $input;
}
The above function for creating the checkbox elements also behaves this way, spelling out the same values in the same order. So what's the consensus, is this a bug in PHP or could this be done a better way?
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::