?
Solved

more php reference problems

Posted on 2006-10-30
9
Medium Priority
?
193 Views
Last Modified: 2008-03-04
The values in a custom object i have dont seem to be being accessed correctly and i think it is because i am not using references correctly in php (I am new to php).  Below is my code with comments showing the odd output.  Below the code is the output (read the description above the output to see what is wrong with it).

    function controlOutput(){
        //pull all the unique key/display values from tablename
        $output="";
        $controlName=$this->parentColumn->getControlName();
       
        $output=$output."<select name=\"".$controlName."\" id=\"".$controlName."\">";
       
        if($this->strKeyColumn!=""){
            $result=$this->queryDatabase();
            while($row = mysql_fetch_row($result))
            {
                $id = $row[0];
                $display = $row[1];
                $output=$output."<option value=\"$id\"";
                echo "Test2? - \$this->parentColumn->strCurValue={$this->parentColumn->strCurValue}<br>\n"; // <-this prints out blank
                //echo "\$id=$id\n";
                if($this->parentColumn->strCurValue==$id) $output=$output." selected";
                $output=$output.">$display</option>\n";
            }
            $output=$output."</select>";
        }
        return $output;
    } //end  function controlOutput()

function editOutput(){
        //pull all the unique key/display values from tablename
        $output="";
        $controlName="frm_".$this->strTableName;
       
        $output=$output."<form name=\"".$controlName."\" id=\"".$controlName."\" action=\"".$_SERVER['SCRIPT_NAME']."\" method=\"POST\">";
        $output=$output."<table cellpadding=0 cellspacing=0 border=0 align=left>";
       
        foreach ($this->arrColumns as $i => $value){
            $output=$output."<tr>";
            $output=$output."<td nowrap style=\"padding-bottom:3px\">".$value->descriptionOutput() . "</td>";
            $output=$output."<td nowrap style=\"padding-left:5px;padding-bottom:3px\">".$value->controlOutput() . "</td>";
            $output=$output."</td></tr>\n";
        }
       
        $output=$output."</table><br clear=\"left\" />";
       
        foreach ($this->arrHiddenFormVariables as $i => $value){
            $output=$output."<input type=\"hidden\" name=\"{$value[0]}\" id=\"{$value[0]}\" value=\"{$value[1]}\">";
        }
        $output=$output."<br /><input type=\"submit\" value=\"Save Changes\">";
        $output=$output."</form>";
       
        return $output;
    } // end function editOutput

      $objCurTable->strIdColumnValue=$curID;
      if(strtolower($curAction=="edit")){
           
        $objCurTable->updateFromDatabase();
         
        foreach($objCurTable->arrColumns as $foreachCurColumn){
            echo "Test? - \$foreachCurColumn->strCurValue{$foreachCurColumn->strDisplayText}={$foreachCurColumn->strCurValue}<br />\n";  <--actually holds the value
        }
      }

echo $objCurTable->editOutput();
---------------------------------------------------------------------------
The following is output generated from the code above.  Notice that during "Test2" the columns have values but during "Test" they do not:

Test? - $foreachCurColumn->strCurValueProgram Name=Example Entry
Test? - $foreachCurColumn->strCurValueProgram Description=
Test? - $foreachCurColumn->strCurValueProgram Website=
Test? - $foreachCurColumn->strCurValueThis program is also an instition=1
Test? - $foreachCurColumn->strCurValueProgram institution=376

Test2? - $this->parentColumn->strCurValue=
^^ The above line is repeated for each record in $result (in the controlOutput function)^^

Does anyone know why I am seeing this output and what changes to make so it see the strCurValues when it runs Test2 (in addition to "Test?")?
0
Comment
Question by:dave4dl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17835909
Where do you set the $this->parentColumn property?

-r-
0
 
LVL 15

Author Comment

by:dave4dl
ID: 17835962
Woops, i forgot to tell you where the parentColumn come from:

$this->objLink=&new clsLink($this);

    function clsLink(&$inputParent,$inputDisplayColumn=null,$inputKeyColumn=null, $inputTableName=null) {
        $this->strDisplayColumn=$inputDisplayColumn;
        $this->strKeyColumn=$inputKeyColumn;
        $this->strTableName=$inputTableName;
        $this->parentColumn=$inputParent;
    }
0
 
LVL 15

Author Comment

by:dave4dl
ID: 17835968
Man you reply fast!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:Roonaan
ID: 17836015
Try:

    function clsLink(&$inputParent,$inputDisplayColumn=null,$inputKeyColumn=null, $inputTableName=null) {
        $this->strDisplayColumn=$inputDisplayColumn;
        $this->strKeyColumn=$inputKeyColumn;
        $this->strTableName=$inputTableName;
        $this->parentColumn=&$inputParent;
    }

-r-
0
 
LVL 15

Author Comment

by:dave4dl
ID: 17836073
Thanks for the replies Roonaan,  I really appreciate your help today!

I changed the line that says "$this->parentColumn=$inputParent;" to "$this->parentColumn=&$inputParent;"
but it still prints out "Test2? - $this->parentColumn->strCurValue=" a number of times.
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 17836117
Try changing the foreach in editOutput from
        foreach ($this->arrColumns as $i => $value){
            $output=$output."<tr>";
            $output=$output."<td nowrap style=\"padding-bottom:3px\">".$value->descriptionOutput() . "</td>";
            $output=$output."<td nowrap style=\"padding-left:5px;padding-bottom:3px\">".$value->controlOutput() . "</td>";
            $output=$output."</td></tr>\n";
        }

To:

        foreach ($this->arrColumns as $i => $value){
            $value = &$this->arrColumns[$i]; // Foreach makes a copy, so we need to get a proper reference
            $output=$output."<tr>";
            $output=$output."<td nowrap style=\"padding-bottom:3px\">".$value->descriptionOutput() . "</td>";
            $output=$output."<td nowrap style=\"padding-left:5px;padding-bottom:3px\">".$value->controlOutput() . "</td>";
            $output=$output."</td></tr>\n";
        }


Or an alternative code:

reset($this->arrColumns);
while(list($i, $value) = each($this->arrColumns)) {
            $output=$output."<tr>";
            $output=$output."<td nowrap style=\"padding-bottom:3px\">".$value->descriptionOutput() . "</td>";
            $output=$output."<td nowrap style=\"padding-left:5px;padding-bottom:3px\">".$value->controlOutput() . "</td>";
            $output=$output."</td></tr>\n";
}

Foreach copies an array before iterating. while/each uses the actual array.

-r-
0
 
LVL 15

Author Comment

by:dave4dl
ID: 17836574
I tried the "while" loop since i didn't know how those behaved differently before but I had no luck.  I expect I am probably not giving you some critical piece of info so i uploaded my php files to https://filedb.experts-exchange.com/incoming/ee-stuff/1278-phpFiles.zip if you dont mind taking a look at the real thing (instead of the snippets on EE).

AddEditItem.php creates a table object (defined in clsTable.php) which has an array of column objects (defined in clsColumn.php) which contains a link object (defined in clsLink.php).

The values stored in the column object seem to persist but accessing it from the (child) link seems to be failing.

So i think the references in one direction and/or the other from the link are not working right.

What do you think?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17836738
Final resort might be to implement an explicit reference:

$this->objLink->parentColumn = &$this;

-r-
0
 
LVL 15

Author Comment

by:dave4dl
ID: 17903129
Thanks for your help roonaan.  I ended up just declaring another variable in the child class so that i wouldnt need a reference to the parent (even though it creates a duplicate).
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question