SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    For Loop - Variable Scope Issue

    i set up a function to update an xml file.

    this works perfectly...

    PHP Code:
    $i=0;
    $j=$i+1;
    $query $xpath->query($new_text[$i]);
    $node $query->item(0);
    $node->nodeValue $new_text[$j];
    file_put_contents($xml_file$doc->saveXML()); 
    $new_text[0] is the xml path and $new_text[1] is the new text to insert into the node at the end of the path.

    however, this only iterates once.

    since i have a form that will update a number of values, this won't work. well, it could work but the code would be excessive.

    so i tried to loop the code like so...

    PHP Code:
    for($i=0$i $num_elements$i+=2){

        
    $j $i+1;
        
    $query $xpath->query($new_text[$i]);
        
    $node $query->item(0);
        
    $node->nodeValue $new_text[$j];
    }

    file_put_contents($xml_file$doc->saveXML()); 
    as you can see, it is the exact same code, but this loop won't work. it effectively does nothing.

    i'm pretty sure i lose at least some of the variables available to the function ($xml_file, $doc, $xpath, $new_text).

    i tried...

    PHP Code:
    for($i=0$i $num_elements$i+=2){

        
    $j $i+1;
        
    $query $GOBAL['xpath']->query($GLOBAL['new_text'][$i]);
        
    $node $query->item(0);
        
    $node->nodeValue $GLOBAL['new_text'][$j];
    }

    file_put_contents($GLOBAL['xml_file'], $GLOBAL['doc']->saveXML()); 
    and i get the white screen of death after submitting the form.

    can i get this to work? if so, how?

    tia...

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see some errors in your variable names

    you typed $GOBAL and $GLOBAL


    the correct variable is $GLOBALS (with an S)

    btw- id recomend developing with notices turned on, php will notify you when you try to use an undefined variable.
    error_reporting(E_ALL);

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    i see some errors in your variable names

    you typed $GOBAL and $GLOBAL


    the correct variable is $GLOBALS (with an S)

    btw- id recomend developing with notices turned on, php will notify you when you try to use an undefined variable.
    error_reporting(E_ALL);
    thanks for the heads up.

    i updated the code to read:

    PHP Code:

    for($i=0$i $num_elements$i+=2){

        
    $j $i+1;
        
    $query $GOBALS['xpath']->query($GLOBALS['new_text'][$i]);
        
    $node $query->item(0);
        
    $node->nodeValue $GLOBALS['new_text'][$j];

    it still failes to work - still get the WSOD.

    i've never been able to get

    error_reporting(E_ALL);

    to work on my winxp cgi install. i do have an IDE that reports errors, though. in the case, the IDE doesn't print out any fatal errors.

    is there a standard syntax for getting objects into for loops (in my case, the dom and the xpath objects? i probably mispoke before when i said variables. i probably should've said objects.

    tia...

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i still see a GOBALS in there

    $GOBALS['xpath']

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skeeterbug
    thanks for the heads up.

    i updated the code to read:

    PHP Code:

    for($i=0$i $num_elements$i+=2){

        
    $j $i+1;
        
    $query $GOBALS['xpath']->query($GLOBALS['new_text'][$i]);
        
    $node $query->item(0);
        
    $node->nodeValue $GLOBALS['new_text'][$j];

    it still failes to work - still get the WSOD.

    i've never been able to get

    error_reporting(E_ALL);

    to work on my winxp cgi install. i do have an IDE that reports errors, though. in the case, the IDE doesn't print out any fatal errors.

    is there a standard syntax for getting objects into for loops (in my case, the dom and the xpath objects? i probably mispoke before when i said variables. i probably should've said objects.

    tia...
    i mispelled $GLOBALS again. i've done it right and it doesn't work.

    PHP Code:

    function update_xml_file($xml_file$doc$xpath$new_text){

        
    // $new_text is array with query path and new text value pairs.
         
    $num_elements count($new_text);

        for(
    $i=0$i $num_elements$i+=2){

            
    $j $i+1;
            
    $query $GLOBALS['xpath']->query($GLOBALS['new_text'][$i]);
            
    $node $query->item(0);
            
    $node->nodeValue $GLOBALS['new_text'][$j];
            } 
        
    file_put_contents($xml_file$doc->saveXML());

    ps - just saw that clam was on it before i realized it. good eyes!

    it still doesn't work, though.

    i even tried a while loop...

    PHP Code:
    function update_xml_file($xml_file$doc$xpath$new_text){

        
    // $new_text is array with query path and new text value pairs.
         
    $num_elements count($new_text);

        
    $i=0;
        while ( 
    $i $num_elements ) {
            
    $j $i+1;
            
    $query $xpath->query($new_text[$i]);
            
    $node->nodeValue $new_text[$j];
            
    $i+=2;
        }
        
    file_put_contents($xml_file$doc->saveXML());

    i no longer get the WSOD, though. i think that had to do with $GOBALS.

    it displays the screen, but it doesn't update my xml file.

    i think i'm losing $doc (DOMDocument object) and $xpath (DOMXpath object).

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my next step would be to start using some debug echo's
    echo all your variables out at each point they are used.

    if you do this for both your working and non working code, it should help you find the problem by comparison

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    my next step would be to start using some debug echo's
    echo all your variables out at each point they are used.

    if you do this for both your working and non working code, it should help you find the problem by comparison
    thanks, i will. i usually do that, but, i'm dealing with objects here, so the complexity goes up a bit. i still should be able to learn something, though.

    is it your understanding that $doc and $xpath should be available inside this function's for or while loop?

    tia...

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah i just realized our previous attempts we didnt catch $doc, it would have had to be GLOBALS['doc']->....

    now that your passing them into the function instead of using the GLOBALS array, that might cause another problem. functions usually make a copy of the variable passed to them. since its an object you dont want a copy.

    either go back and make them all GLOBALS again, including $doc this time, or try this

    function update_xml_file(&$xml_file, &$doc, &$xpath, &$new_text){


    & tells php not to copy, but to make a reference.

  9. #9
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    ah i just realized our previous attempts we didnt catch $doc, it would have had to be GLOBALS['doc']->....

    now that your passing them into the function instead of using the GLOBALS array, that might cause another problem. functions usually make a copy of the variable passed to them. since its an object you dont want a copy.

    either go back and make them all GLOBALS again, including $doc this time, or try this

    function update_xml_file(&$xml_file, &$doc, &$xpath, &$new_text){


    & tells php not to copy, but to make a reference.
    wheeeew, eyes glazed over.

    reviewed the code and founf my error. i consolidated two pages into one and i screwed up my path on several of the variables. the variable i was testing had the wrong path. uggggh!

    this works perfect - today, anyway

    PHP Code:

    function update_xml_file($xml_file$doc$xpath$new_text){

        
    // $new_text is array with query path and new text value pairs.
         
    $num_elements count($new_text);

        for(
    $i=0$i $num_elements$i+=2){

            
    $j $i+1;
            
    $query $xpath->query($new_text[$i]);
            
    $node $query->item(0);
            
    $node->nodeValue $new_text[$j];
            }
        
    file_put_contents($xml_file$doc->saveXML());

    this also works.

    PHP Code:

    function update_xml_file($xml_file$doc$xpath$new_text){

        
    // $new_text is array with query path and new text value pairs.
         
    $num_elements count($new_text);

        for(
    $i=0$i $num_elements$i+=2){

            
    $j $i+1;
            
    $query $GLOBALS['xpath']->query($GLOBALS['new_text'][$i]);
            
    $node $query->item(0);
            
    $node->nodeValue $GLOBALS['new_text'][$j];
            }
        
    file_put_contents($GLOBALS['xml_file'], $GLOBALS['doc']->saveXML());

    since i don't need to use $GLOBALS, i won't.

    thanks for the help. i'm going to blame this bonehead move on beign sick for the past 4 days. that's my story and i'm going to stick to it.

    thank you very much for helping. it is much appreciated.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •