Looping problem?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
First php post...

Hi there,

I'm writing some code for my website to list people listening to my music. I  
have a loop for an array that dosen't seem to display all people.  most of  
the time it resuses to go past 11 even though there are more people.  I've  
created a small loop just to list the IP addresses as a test, which works.


 $loop = 0;                                                //dummy loop  
 while($loop != $currentlisteners){         //while loop for listeners
  echo "$loop - $hostname[$loop]<br>";

When I use the same loop again to get more detailed info, the $loop only  
counts from 0 to about 10 or sometimes more. It's never consistant. I never  
encountered this in any other language before. The $currentlisteners is  
never altered during the loop execution.  What I've done was echo $loop to  
see it's progress throughout the loop.  This is so frustrating.

 $loop = 0; //dummy loop variable
 while($loop != $currentlisteners){ //while loop for listeners

// Get the surfer's ip address
$addr = $hostname[$loop];

$ip = sprintf("%u", ip2long($addr));

// Load array with start ips
$row = 1;
while (($buffer = fgets($handle, 4096)) !== FALSE) {
  $array[$row] = substr($buffer, 1, strpos($buffer, ",") - 1);

// Locate the row with our ip using bisection
$row_lower = '0';
$row_upper = $row;
while (($row_upper - $row_lower) > 1) {
  $row_midpt = (int) (($row_upper + $row_lower) / 2);
  if ($ip >= $array[$row_midpt]) {
    $row_lower = $row_midpt;
  } else {
    $row_upper = $row_midpt;

// Read the row with our ip
$row = 1;
while ($row <= $row_lower) {
  $buffer = fgets($handle, 4096);
$buffer = str_replace("\"", "", $buffer);
$ipdata = explode(",", $buffer);

  echo "<td><font size=1><center>$loop <img  
src='../WhosBeenListening/Flags/$ipdata[4].gif' width='18' height='12'>  
$ipdata[6] - $hostname[$loop]</td>";
  echo "<td><font size=1><center>$useragent[$loop]</td>";

  $sec = $connecttime[$loop];
  $hours = intval(intval($sec) / 3600);
  $hms = str_pad($hours, 2, "0", STR_PAD_LEFT).':';
  $minutes = intval(($sec / 60) % 60);
  $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':'; // then add to $hms  
(with a leading 0 if needed)
  $seconds = intval($sec % 60);       // seconds by 60 and keep the  
  $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);  // add to $hms, again  
with a leading 0 if needed
  echo "<td><font size=1><center>$hms</td>";

  echo "<td><font size=1><center>$underruns[$loop]</td>";
  echo "</tr>";
 $loop++;  //update loop variable

Is there something wrong with the $loop variable itself?  It has not been  
declared (if I can use this term in PHP) in any way.  I am using PHP 5 on my  
computer at this time. Here is the link for my site (xex.ca) that shows the  
scripting results. If there are more than 10 people listening to that one  
perticular stream then you will see the problem. If I get it working then I  
will use the same code for all the streams to get a total info page. .  I am altering  
and adding to code I found on the net.

Any help is appriciated.

- Prometheus Xex

P.S. If this is the wrong group for this kind of help, then please redirect  
me to a more appropriate place.  

Re: Looping problem?

Promextheus Xex wrote:
Quoted text here. Click to load it

In addition to printing out $loop in each iteration, you should also
print out $currentlisteners at the beginning to make sure it has the
value you're expecting.

One thing I think you should do no matter what is change your while()
loop to a for() loop, like so:

for($loop = 0; $loop < $currentlisteners; $loop++)

Site Timeline