#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0

    If Statement adds key in over and over.


    Sorry the title sucks, nto sure exactly what to say.

    I have this data coming in (just a partial sample)

    [XML]
    <Table>
    <ITEMNO>10037</ITEMNO>
    <IDESC>TRJ TA31ECOSG AGOG/RMR COMBO 4X32 GRN</IDESC>
    <IMFSEQ>6020</IMFSEQ>
    <IMFGNO>171</IMFGNO>
    <CSEQ>5702</CSEQ>
    <ITYPE>5</ITYPE>
    <SHDESC>Trijicon TA3IECOS-G ACOG Obj FOV Tube Dia </SHDESC>
    <UOM>EA</UOM>
    <PRC1>1280.46</PRC1>
    <CPRC>1280.46</CPRC>
    <QTYOH>10</QTYOH>
    <WTPBX>1.750</WTPBX>
    <ITUPC>719307303430</ITUPC>
    <MFGINO>TA31ECOSG </MFGINO>
    <SCNAM1>TRJ </SCNAM1>
    <SCNAM2>SC4E </SCNAM2>
    <CATCD>S</CATCD>
    <MFPRTYP xml:space="preserve"> </MFPRTYP>
    <MFPRC>0.00</MFPRC>
    <CATID>46</CATID>
    <TXTREF>3311</TXTREF>
    <PICREF>0</PICREF>
    <ITBRDNO>171</ITBRDNO>
    <IMODEL>ACOG </IMODEL>
    <IPURPOSE>Enhance Accuracy </IPURPOSE>

    [/XML]


    This code

    PHP Code:
    $url_to_work_on=file_get_contents($url_value);//get the data
    $wrapping_xml=simplexml_load_string($url_to_work_on);//clean it up
    $wrapping_xml=str_replace("&#x1A","",$wrapping_xml);//clean out goofy characters
    $final_data_to_parse=simplexml_load_string($wrapping_xml);

    //&#x1A

    //echo $wrapping_xml;

    $child=$final_data_to_parse->children();//Theis gets the children of the main data (i.e, this retruns "TABLE" since its NewDataSet>Table
    $parenttag=$child->getName();//holds the root XML tag name

    for($i=0;$i<count($final_data_to_parse->$parenttag);$i++)
        {
        foreach(
    $child->children() as $child2)//This goes through TABLE and gets the sub names
            
    {
            
    $value=$child2->getName();//This gets the sub tag
            
            
            //echo "$value<br />\n";
            
    Echo $value." ".$final_data_to_parse->{$parenttag}[$i]->$value."<br />\n";
       }

    Yields Clean results i.e

    ITEMNO 10037
    IDESC TRJ TA31ECOSG AGOG/RMR COMBO 4X32 GRN
    IMFSEQ 6020
    IMFGNO 171
    CSEQ 5702
    ITYPE 5
    SHDESC Trijicon TA3IECOS-G ACOG Obj FOV Tube Dia
    UOM EA
    PRC1 1280.46
    CPRC 1280.46
    QTYOH 10
    WTPBX 1.750
    ITUPC 719307303430
    MFGINO TA31ECOSG
    SCNAM1 TRJ
    SCNAM2 SC4E
    CATCD S
    MFPRTYP
    MFPRC 0.00

    At some point, I want to strip out the item number and hold it, so that I can update the SQL record based on what key I'm pulling out, so that my insert statement will be like INSERT INTO datatable (ITEMNO,$CURRENTKEYNAMEFROMFOREACH) VALUES ($ITEMNO,$CURRENTKEYATA)

    Im adding an IF, to say iif the curreny key is ITEMNO, then store that value through the foreach as I add data to the database. If I add that if and the code looks like below:

    PHP Code:

    for($i=0;$i<count($final_data_to_parse->$parenttag);$i++)
        {
        foreach(
    $child->children() as $child2)//This goes through TABLE and gets the sub names
            
    {
            
    $value=$child2->getName();//This gets the sub tag
            
            
            //echo "$value<br />\n";
            
    Echo $value." ".$final_data_to_parse->{$parenttag}[$i]->$value."<br />\n"///This echoes the data with the tag first
            
    If($value='ITEMNO')
                {
                
    $ITEMNO=$final_data_to_parse->{$parenttag}[$i]->$value;
                echo 
    "Found My Item Number ! it's ".$ITEMNO."<br />\n";    
                }
         }

    Then my return is

    ITEMNO 10037
    Found My Item Number ! it's 10037
    IDESC TRJ TA31ECOSG AGOG/RMR COMBO 4X32 GRN
    Found My Item Number ! it's 10037
    IMFSEQ 6020
    Found My Item Number ! it's 10037
    IMFGNO 171
    Found My Item Number ! it's 10037
    CSEQ 5702
    Found My Item Number ! it's 10037
    ITYPE 5
    Found My Item Number ! it's 10037
    SHDESC Trijicon TA3IECOS-G ACOG Obj FOV Tube Dia
    Found My Item Number ! it's 10037
    UOM EA
    Found My Item Number ! it's 10037
    PRC1 1280.46
    Found My Item Number ! it's 10037
    CPRC 1280.46


    If I do it like below:

    PHP Code:
    for($i=0;$i<count($final_data_to_parse->$parenttag);$i++)
        {
        foreach(
    $child->children() as $child2)//This goes through TABLE and gets the sub names
            
    {
            
    $value=$child2->getName();//This gets the sub tag
            
            
    If($value='ITEMNO')
                {
                
    $ITEMNO=$final_data_to_parse->{$parenttag}[$i]->$value;
                echo 
    "Found My Item Number ! it's ".$ITEMNO."<br />\n";    
                }
        Echo 
    $value." ".$final_data_to_parse->{$parenttag}[$i]->$value."<br />\n"///This echoes the data with the tag first    
     
    }

    It returns:


    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037
    Found My Item Number ! it's 10037
    ITEMNO 10037

    Notice it doesn't move to the next key value, it just sees ITEMNO over and over

    Why is it only seeing the ITEMNO key in the last case and seeing it over and over in the prior?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0
    Apparently needed double equal

    PHP Code:
    for($i=0;$i<count($final_data_to_parse->$parenttag);$i++)
        {
        foreach(
    $child->children() as $child2)//This goes through TABLE and gets the sub names
            
    {
            
    $value=$child2->getName();//This gets the sub tag
            
    If($value=='ITEMNO')
                {
                
    $ITEMNO=$final_data_to_parse->{$parenttag}[$i]->$value;
                echo 
    "-----------------Found My Item Number ! it's ".$ITEMNO."<br />\n";    
                }
            
            
    //echo "$value<br />\n";
            
    Echo $value." ".$final_data_to_parse->{$parenttag}[$i]->$value."<br />\n"///This echoes the data with the tag first
            
            
            
    }
        } 
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    use an IDE like Netbeans. It warns you of "stupid" mistakes like this.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    For a little cleanup, consider:
    PHP Code:

    foreach($final_data_to_parse->$parenttag as $parent){
        foreach(
    $child->children() as $child2){ //This goes through TABLE and gets the sub names 
            
    if ( ($value $child2->getName()) == 'ITEMNO' ){
                echo 
    "-----------------Found My Item Number ! it's ".$parent->$value."<br />\n";     
            } 
            echo 
    $value." ".$parent->$value."<br />\n"//This echoes the data with the tag first 
        
    }

    Easier loop, fewer variables, fewer lines, etc. May or may not be applicable if you only provided the key elements of the code, but thought I'd toss it out all the same.
    LinkedIn: Dave Mittner
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0
    Originally Posted by dmittner
    For a little cleanup, consider:
    PHP Code:

    foreach($final_data_to_parse->$parenttag as $parent){
        foreach(
    $child->children() as $child2){ //This goes through TABLE and gets the sub names 
            
    if ( ($value $child2->getName()) == 'ITEMNO' ){
                echo 
    "-----------------Found My Item Number ! it's ".$parent->$value."<br />\n";     
            } 
            echo 
    $value." ".$parent->$value."<br />\n"//This echoes the data with the tag first 
        
    }

    Easier loop, fewer variables, fewer lines, etc. May or may not be applicable if you only provided the key elements of the code, but thought I'd toss it out all the same.

    That was awesome! Works great! Much cleaner! Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo