www.webdeveloper.com
Results 1 to 8 of 8

Thread: for(loop) {Undefined index: 5}.....

  1. #1
    Join Date
    Apr 2005
    Location
    Oregon
    Posts
    144

    for(loop) {Undefined index: 5}.....

    Hi,
    I have spent that past 3 days trying to get this to work and finding out why it isn't. I have a "for" loop that starts out at '1000' and goes to '1005' and incriments by '1'. Inside the loop is a script that open a text file that has several lines of text. The text is tab-delimited. Here is the code.

    PHP Code:
    <?php
    $final_output 
    "";
    $runs "1";
    for( 
    $node "1000"$node <= "1005"$node += "1"){

    echo 
    "start run " $runs "<br>";
    echo 
    "node: " $node "<br>";

    // Opens the file and serches for the requested node number
      
    $handle fopen('localstatus.txt' "r") or die("Can't open file....");
      while(!
    feof($handle)){
      
    $buffer fgets($handle4096);
      
    $pos strpos($buffer$node);
      if (
    $pos !== FALSE && $pos == '0'){break;}
      }
    echo 
    "Buffer: " $buffer "<br>";
    // Closes the file
      
    fclose($handle);
      
    // Sorts the gathered data
      
    $buf_arr explode("\t"$buffer);
      echo 
    "Buf_arr: " $buf_arr " arr <br>";
    // Assigns the status of the node to a string
      
    $status $buf_arr['5'];
    if(
    $status == 'Busy') {
    $status 'BUSY';
    }
      
    // Assigns the node callsign to a string
      
    $callsign $buf_arr['1'];
      
    // Assembles first part of output
      
    $own_node = ($callsign ' Node ' $node);
      
    // Based on what the status was, the finnal output and color ar being set
      
    switch ($status) {
      case 
    'BUSY':
      
    $node_status $own_node ' is ' $status;
      
    $color '#FF0000';
      break;
    case 
    'OFFLINE':
      
    $node_status $own_node ' is ' $status;
      
    $color '#FF0000';
      break;
    case 
    'DOWN':
      
    $node_status $own_node ' is ' $status;
      
    $color '#FF0000';
      break;
    case 
    'IDLE':
      
    $node_status = ($own_node ' is ' $status);
      
    $color '#00FF00';
      break;
    default:
      if (
    $status <= '8999') {
      
    $node_status $own_node ' is connected to Node ' $status;
      
    $color '#FF0000';
      }
      elseif (
    $status >= '9000') {
      
    $node_status $own_node ' is connected to Reflecter ' $status;
      
    $color '#FFFF00';
      }
      break;
      }
      
    // Verifies the node number proccessed is the same node that was requested
      
    $verify $buf_arr['0'];
      if (
    $verify !== $node) {
      
    $node_status 'Sorry, there was no data found for Node ' $node;
      
    $color '#33CCFF';
      }
      
    $final_output .= "<font color=$color>";
      
    $final_output .= "$node_status";
      
    $final_output .= "</font><br>";
      echo 
    "end <br><br>";
      
    $runs ++;
      }

        echo 
    "<center><b>IRLP Node Status Script</B><br><br>";
        
    // Displays your nodes to you page
        
    echo $final_output;
    ?>
    The problem I am having is the first time through it works fine but every time after that '$buffer' is always empty. This is the output to the browser.

    Code:
    start run 1
    node: 1000
    Buffer: 1000 VE7RHS Vancouver BC Canada IDLE 1000 1998-11-12 49.269892 -123.25002 1178224118 145.2700 -600.0000 100.0 ve7ltd http://www.ars.ams.ubc.ca 1178221826 O 
    Buf_arr: Array arr 
    end 
    
    start run 2
    node: 1001
    Buffer: 
    Buf_arr: Array arr 
    
    Notice: Undefined index: 5 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 24
    
    Notice: Undefined index: 1 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 30
    end 
    
    start run 3
    node: 1002
    Buffer: 
    Buf_arr: Array arr 
    
    Notice: Undefined index: 5 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 24
    
    Notice: Undefined index: 1 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 30
    end 
    
    start run 4
    node: 1003
    Buffer: 
    Buf_arr: Array arr 
    
    Notice: Undefined index: 5 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 24
    
    Notice: Undefined index: 1 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 30
    end 
    
    start run 5
    node: 1004
    Buffer: 
    Buf_arr: Array arr 
    
    Notice: Undefined index: 5 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 24
    
    Notice: Undefined index: 1 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 30
    end 
    
    start run 6
    node: 1005
    Buffer: 
    Buf_arr: Array arr 
    
    Notice: Undefined index: 5 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 24
    
    Notice: Undefined index: 1 in E:\Program Files\Apache Group\Apache2\htdocs\AAIN\an.php on line 30
    end 
    
    
    IRLP Node Status Script
    
    VE7RHS Node 1000 is IDLE
    Sorry, there was no data found for Node 1001
    Sorry, there was no data found for Node 1002
    Sorry, there was no data found for Node 1003
    Sorry, there was no data found for Node 1004
    Sorry, there was no data found for Node 1005
    Does anyone know why this isn't working or what I am doing wrong?
    Jeremy

  2. #2
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Code:
    $buf_arr = explode("\t", $buffer);
      echo "Buf_arr: " . $buf_arr . " arr <br>";
    // Assigns the status of the node to a string
      $status = $buf_arr['5'];
    You have a numerical array $buff_arr created by the explode and are then trying to access it as an associative array looking for an element in the array named '5' to copy to status but none of the entries have names. If you are trying to get the sixth element then you want $buf_arr[5] and not $buf_arr['5'].
    Stephen

  3. #3
    Join Date
    Apr 2005
    Location
    Oregon
    Posts
    144
    OK, But I don't understand what that means or what to do. I am not trying to get this handed to me on a golden platter but I need to know what to do with this info now.
    Jeremy

  4. #4
    Join Date
    Mar 2006
    Location
    California
    Posts
    306
    He's saying that, as of now, you are trying get element '5' (Note that it is a string), but what you should be trying to get is element 5 (Note that it is a number)

    It's the same thing on this section:
    PHP Code:
      $callsign $buf_arr['1']; 
    It should be:
    PHP Code:
      $callsign $buf_arr[1]; 
    The explode() function returns an array consisting of numerical keys, not string keys. It's just a simple mistake.
    JS > PHP > Everything

  5. #5
    Join Date
    Apr 2005
    Location
    Oregon
    Posts
    144
    I see, however I did change that ( I am calling info from 3 keys, '0', '1' and '5' )and there is no difference in the output to the browser. Is there something else?

    <EDIT>
    I'm going to go out on a limb here but, I don't really thing that is the problem. If you look at the the script on line 16 where I echo the $buffer, it echos on the first go around but it is empty for all the rest. So, there is no data loaded into $buf_arr to get anything from the Keys. Does this make sense? Why is $buffer empty after the first time?
    Last edited by towerboy; 05-04-2007 at 08:15 PM.
    Jeremy

  6. #6
    Join Date
    Apr 2005
    Location
    Oregon
    Posts
    144
    anyone????
    Jeremy

  7. #7
    Join Date
    May 2007
    Location
    Denver
    Posts
    19
    It looks to me like the problems are related to data types. '1' is not the same thing as 1. '1' is a string with chr(49). 1 is the number one.

    '1001' is a string of characters chr(49).chr(48).chr(48).chr(49).

    PHP converts between types whenever it hits an arithmetic statement. If you had the command $x = '1001' + '1'; PHP would convert '1001' to 1001 and '1' to 1, then give you the answer 1002. Java and C++ give '10011' .

    In the line ($node += "1"), PHP changes the type of "1" to an integer then does the addition.

    The command var_dump() shows the type of a variable. You might sprinkle it throughout your code and you will find that things are changing types right and left in your code.

    Running the opening for loop with var_dump.

    PHP Code:
    for( $node "1000"$node <= "1005"$node += "1"){ 
      echo 
    '<br />Node '.$node.': ';
      
    var_dump($node);

    This returns:

    Node 1000: string(4) "1000"
    Node 1001: int(1001)
    Node 1002: int(1002)
    Node 1003: int(1003)
    Node 1004: int(1004)
    Node 1005: int(1005)

    In the very first iteration of the loop, $node is a four digit string. In the second iteration, it is a number.

    Although PHP is loosely typed, you really need to learn the difference between types to master the language. Quite frankly, the very first line of the code horrifies me:

    for( $node = "1000"; $node <= "1005"; $node += "1")

    that line should be

    for( $node = 1000; $node <= 1005; $node++)

    Anyway, the answer to debugging this problem is to follow the type of each variable throughout the program.

  8. #8
    Join Date
    Apr 2005
    Location
    Oregon
    Posts
    144
    yintercept,
    OK Cool. Using:
    PHP Code:
    settype($node"string"); 
    Makes it all work great.
    Thank you so much for the different aspects that you opened up about PHP to me. I never knew.
    Jeremy

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