1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

loss of variable?

Discussion in 'PHP' started by pingu2k4, Jan 24, 2009.

  1. #1
    Hi, I can read successfully test my xml and a text files separately, but I'm coming into a problem where the variable is possibly being removed or deleted by the time I want to plug it into the bottom of the document.

    As you can see in the top part of the code "$xmlname" is set to the product name specified in the xml document. In the bottom of the document where I use strpos, and attempt to see if the variable $xmlname is equal to a line in the text document, I can't get it to return true unless I statically put a word in, instead of the variable. Any ideas?

    <?php
    
    $xmlFileData = file_get_contents('datafeed1.xml');
    //SimpleXML parser
    $xmlData = new SimpleXMLElement($xmlFileData);
    
    //Retrieving the product name
    $xmlname = $xmlData->transactions[0]->transaction[0]->transaction_details[0]->transaction_detail[0]->product_name;
    //Printing first product name
    print($xmlname);
    
    print(' - ');
    //Retrieving the product price
    $xmlprice = $xmlData->transactions[0]->transaction[0]->transaction_details[0]->transaction_detail[0]->product_price;
    //Printing first product price
    print($xmlprice);
    print "<br /><br />";
    
    $lines = file('file.txt');
    
    // Loop through array
    foreach ($lines as $line_num => $line) {
        echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
    }
    
    $string1 = $xmlname;
    $file = file_get_contents('file.txt');
    if(strpos($file, $string1) !== FALSE)
    echo 'The file contains the string';
    else
    echo 'The string is not within the file.'; 
    
    ?>
    PHP:
     
    pingu2k4, Jan 24, 2009 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    change:
    if(strpos($file, $string1) !== FALSE)
    echo 'The file contains the string';
    else
    echo 'The string is not within the file.';


    to:

    if (strpos($file, $string1) === false) {
    echo 'The string is not within the files.';
    } else {
    echo 'the file contains the string';
    }
     
    GreatMetro, Jan 24, 2009 IP
  3. pingu2k4

    pingu2k4 Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks! it works now :)
     
    pingu2k4, Jan 24, 2009 IP