Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2003, 02:20 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default String access by character bug in multi-dimensiona

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
:::::::::::::::::::::::::::::::::
 
Old September 6th, 2003, 05:57 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hooray! Problem solved, for anyone interested:

The solution to the problem came about after writing a test script based on only the relevant snipets that I provided below. The result, it behaved exactly as expected. So I returned to the original script. $this->value[$i] prints out a value of 'Array' but when tested for is_array() returns false. Surely that can't be right.

Then I deduced that somewhere in the script an operation was being preformed on $this->value[$i] that caused it to change and change enough that it wasn't considered an array anymore. Then I saw it...

$this->value[$i] = stripslashes($this->value[$i]);

stripslashes() was the culprit. And the fix...

if (!is_array($this->value[$i])) {

    $this->value[$i] = stripslashes($this->value[$i]);

}

Aye!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
trim a string to the first character jd_erd Access 2 May 6th, 2007 11:01 PM
Replacing a character from string itHighway Classic ASP Basics 5 March 14th, 2005 11:15 PM
Search for Character in String Kaynor09 Excel VBA 2 April 24th, 2004 11:13 AM
Wide Character String kochoo Classic ASP Components 0 October 12th, 2003 12:06 AM
How do I get rid of the last character in a string Lucy Classic ASP Professional 3 September 30th, 2003 05:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.