472,951 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

Looping problem?

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.

ie:

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

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.

EG:
--------------------------------------------------------
$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);
$row++;
}

// 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
rewind($handle);
$row = 1;
while ($row <= $row_lower) {
$buffer = fgets($handle, 4096);
$row++;
}
$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
remainder
$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.
http://74.113.12.208:29992/Xex/Radio...castdetail.php. 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.
Nov 21 '06 #1
1 1495

Promextheus Xex wrote:
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.

ie:

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

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.

EG:
--------------------------------------------------------
$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);
$row++;
}

// 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
rewind($handle);
$row = 1;
while ($row <= $row_lower) {
$buffer = fgets($handle, 4096);
$row++;
}
$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
remainder
$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.
http://74.113.12.208:29992/Xex/Radio...castdetail.php. 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.

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++)
{
}

Nov 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Lyle Fairfield | last post by:
The stored procedure script below is an example of how looping, case statements and output parameters can be used in MS-SQL stored procedures to accomplish things for which we may have had to use...
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
13
by: JayCallas | last post by:
I know this question has been asked. And the usual answer is don't use cursors or any other looping method. Instead, try to find a solution that uses set-based queries. But this brings up...
5
by: Jeff | last post by:
Hey gang. I have a script that gets stats from a mssql db, and then inserts those stats into a temp table. where i can work with them as i wish. the problem is it isn't looping through all the...
6
by: Luke - eat.lemons | last post by:
Hi, Im pretty new to asp so all light on this question would be great. Basically i need to test to see what value is set (where to retrieve the data from) so ive done it like this: If...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
3
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
3
by: assgar | last post by:
Hi I am having problem with my loping. I don't know if I have chosen the correct approach. GOAL: I need to insert into a table event types for a specific date range. The calendar the event...
3
by: chiku1523 | last post by:
Hi, Please find the following code. In function setAnswers, I am looping with each question. I have inner loop, which is looping for each answers of the questions. If any of the answer for question...
1
by: Robocop | last post by:
Having just started using C again after some years off, i've been stumped by a problem i think someone more experienced could probably solve pretty easily. I have these 4 objects (vectors), and i...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.