473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 != $currentlistene rs){ //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 $currentlistene rs 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 != $currentlistene rs){ //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='../WhosBeenListeni ng/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($minute s, 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($second s, 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 1527

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 != $currentlistene rs){ //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 $currentlistene rs 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 != $currentlistene rs){ //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='../WhosBeenListeni ng/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($minute s, 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($second s, 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 $currentlistene rs 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 < $currentlistene rs; $loop++)
{
}

Nov 21 '06 #2

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

Similar topics

11
2590
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 VBA code, or automation, in the JET world. Some who have not yet worked with MS-SQL may be interested. The script is indented in reality but I have...
7
5461
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 loop through the filtered GridView to identify the selected rows and assemble some XML to be sent to a stored proc. The problem I have is that when...
13
5001
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 several questions / senarios: * I created several stored procedures that take parameters and inserts the data into the appropriate tables. This was done...
5
1477
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 records, and i am not sure why. i know the script isn't perfect, as it is open to sql injection and stuff like that, but once the site is...
6
1806
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 Request.Querystring("id") = "" then TidF=Request.Form("TidF") Else
1
1840
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 is the looping is not displaying the all contents of the arrays. Do you have any idea what the problem is and how to fix the problem?
3
3715
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 retrieve the pointer to the first dataset in the list, remove it from the list, and do some high level analysis. The problem now is, how do I...
3
1895
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 type is displayed on is divided into 15 minutes time intervals
3
1943
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 and question2 is selected this code is working fine. But if None of answer of either question selected, it is not iterating through outer loop....
1
1986
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 want to find a combination of these 4 objects into two pairs, where the sum of a specific attribute of each vector in each pair comes closest to...
0
7428
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6014
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.