473,379 Members | 1,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 1514

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.