www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] SXML: Finding the greatest value

  1. #1
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695

    resolved [RESOLVED] SXML: Finding the greatest value

    Name is assigned by $name
    If the greatest number is under 10 it is assigned 1
    If it's under 20 its 2
    and 30 is 3

    Error on line 7 for invalid arg
    rest will output 3s but no $name
    I doubt either is right, anyone
    mind pointing me in the right direction?

    sample xml
    Code:
    <document>
    <row>
      <Col0></Col0 >
      <Col1>Anthony Shetler</Col1 >
      <Col2>-</Col2 >
      <Col3></Col3 >
      <Col4>-</Col4 >
      <Col5></Col5 >
      <Col6>-</Col6 >
      <Col7></Col7 >
     </row>
     <row>
      <Col0></Col0 >
      <Col1>Anthony Van Engelen</Col1 >
      <Col2>-</Col2 >
      <Col3></Col3 >
      <Col4>-</Col4 >
      <Col5></Col5 >
      <Col6>135</Col6 >
      <Col7>http://www./catalog/product/view/id/106494/category/1352/</Col7 >
     </row>
     <row>
      <Col0></Col0 >
      <Col1>Antwuan Dixon</Col1 >
      <Col2>-</Col2 >
      <Col3></Col3 >
      <Col4>186</Col4 >
      <Col5>http://couchtour./aurora_sched.htm?utm_source=zumiez&amp;utm_medium=zumiez&amp;utm_content=event-schedule&amp;utm_campaign=ct-video-event-schedule</Col5 >
      <Col6>163</Col6 >
      <Col7>http://www./catalog/product/view/id/33367</Col7 >
     </row>
     <row>
      <Col0></Col0 >
      <Col1>Arto Saari</Col1 >
      <Col2>-</Col2 >
      <Col3></Col3 >
      <Col4>-</Col4 >
      <Col5></Col5 >
      <Col6>-</Col6 >
      <Col7></Col7 >
     </row>
    </document>
    php
    PHP Code:
    <?php
    $file 
    '1.xml';

    if (
    file_exists($file)) {
        
    $sxml simplexml_load_file($file);
     
     foreach (
    $xml->document->row as $x){
     
        
    $name = (string)$x->col1
        
    $x = (string)$x->col2
        
    $y = (string)$x->col4
        
    $z = (string)$x->col6;

        $
    $maxValue=max($x,max($y,$z));

            
    $rank=NULL// blank the rank

            
    if($maxValue <= '10'){ $rank='1'; }
            if(
    $maxValue <= '20'){ $rank='2'; }
            if(
    $maxValue <= '30'){ $rank='3'; }
            if(
    $maxValue '31') { $rank='0'; }
            
                
    $name = (string)$name;
                
    $rank = (string)$rank;
            
        echo 
    "$name is $rank<br />";
        }

     } else {
        exit(
    'Failed to open $file.');
    }
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,607
    You use $sxml at line 5, but $xml at line 7. I think you'll also have problems within the foreach loop, as you are creating an object named $x in each iteration, then clobbering it with a scalar value on this line:
    PHP Code:
    $x = (string)$x->col2
    So the lines that follow for $y and $z will fail.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    Still having an issue on line 7 Nog,
    is it something to do with the document->row maybe?

    PHP Code:
    <?php
    $file 
    '1.xml';

    if (
    file_exists($file)) {
        
    $sxml simplexml_load_file($file);
     
     foreach (
    $sxml->document->row as $key){
     
        
    $name $key->col1
        
    $x $key->col2
        
    $y $key->col4
        
    $z $key->col6;

        $
    $maxValue=max($x,max($y,$z));

            
    $rank=NULL// blank the rank

            
    if($maxValue <= '10'){ $rank='1'; }
            if(
    $maxValue <= '20'){ $rank='2'; }
            if(
    $maxValue <= '30'){ $rank='3'; }
            if(
    $maxValue '31') { $rank='0'; }
            
                
    $name = (string)$name;
                
    $rank = (string)$rank;
            
        echo 
    "$name is $rank<br />";
        }

     } else {
        exit(
    'Failed to open $file.');
    }
    ?>

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,607
    Yeah, you don't need the top-level element "document". Just use $sxml->row.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    Why do you think that $name does not pull from col1 and maxValue always ends up as 3?

  6. #6
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    I hope this can help someone else, here's were I finished at:

    PHP Code:
    <?php
    $file 
    '1.xml';

    if (
    file_exists($file)) {
      
    $s simplexml_load_file($file);

    foreach (
    $s->row as $s){

    $name $s->Col1;

        
    $x = ($s->Col2 != '-') ? $s->Col2 NULL;
        
    $y = ($s->Col4 != '-') ? $s->Col4 NULL;
        
    $z = ($s->Col6 != '-') ? $s->Col6 NULL;

     
    $maxValue=max($x,$y,$z) . ' ';
     
            
    $rank=NULL// blank the rank

            
    if($maxValue 30){ $rank=3; }
            if(
    $maxValue 20){ $rank=2; }
            if(
    $maxValue 10){ $rank=1; }
            if(
    $maxValue 31){ $rank=0;}
        echo 
    "$maxValue :: Name: $name Rank: $rank<br />";
        }

    } else {
        exit(
    'Failed to open $file.');
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles