/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=php]<?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($handle, 4096);
$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;
?>[/code]

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 FilesApache GroupApache2htdocsAAINan.php on line 24

Notice: Undefined index: 1 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 30
end

start run 3
node: 1002
Buffer:
Buf_arr: Array arr

Notice: Undefined index: 5 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 24

Notice: Undefined index: 1 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 30
end

start run 4
node: 1003
Buffer:
Buf_arr: Array arr

Notice: Undefined index: 5 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 24

Notice: Undefined index: 1 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 30
end

start run 5
node: 1004
Buffer:
Buf_arr: Array arr

Notice: Undefined index: 5 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 24

Notice: Undefined index: 1 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 30
end

start run 6
node: 1005
Buffer:
Buf_arr: Array arr

Notice: Undefined index: 5 in E:Program FilesApache GroupApache2htdocsAAINan.php on line 24

Notice: Undefined index: 1 in E:Program FilesApache GroupApache2htdocsAAINan.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[/CODE]

Does anyone know why this isn’t working or what I am doing wrong?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@felgallMay 04.2007 — $buf_arr = explode("t", $buffer);
echo "Buf_arr: " . $buf_arr . " arr &lt;br&gt;";
// 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'].
Copy linkTweet thisAlerts:
@towerboyauthorMay 05.2007 — 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.
Copy linkTweet thisAlerts:
@hyperliskMay 05.2007 — 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:
[code=php]
$callsign = $buf_arr['1'];
[/code]


It should be:
[code=php]
$callsign = $buf_arr[1];
[/code]


The explode() function returns an array consisting of numerical keys, not string keys. It's just a simple mistake.
Copy linkTweet thisAlerts:
@towerboyauthorMay 05.2007 — 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?
Copy linkTweet thisAlerts:
@towerboyauthorMay 05.2007 — anyone????
Copy linkTweet thisAlerts:
@yinterceptMay 06.2007 — 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.

[code=php]for( $node = "1000"; $node <= "1005"; $node += "1"){
echo '<br />Node '.$node.': ';
var_dump($node);
}[/code]


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.
Copy linkTweet thisAlerts:
@towerboyauthorMay 07.2007 — yintercept,

OK Cool. Using:
[code=php]settype($node, "string");[/code]
Makes it all work great.

Thank you so much for the different aspects that you opened up about PHP to me. I never knew.
×

Success!

Help @towerboy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...