My For loop headache, please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

My For loop headache, please help

Post by mhouldridge »

Hi,

Here is my for loop for showing available IP addresses within my database. It creates an array with the taken ones and looks for available ones within the range ---- WELL, AT LEAST THAT IS WHAT IT SHOULD DO :-)

Code: Select all

$found_ip = 0;
for($counter = 1; $counter >= 255; ++$counter){
if(!in_array('192.168.241.'.$counter,$ips)){
$found_ip = ('192.168.241.'.$counter);									
 
 }
}
							
echo $found_ip;
The echo $found_ip outputs a result of zero. It appears to be taking the variable from the first line without using the For loop. I need to combine the $found_ip with the For loop somehow.

Please help
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

Your for loop was wrong.

Code: Select all

for($counter = 1; $counter <= 255; $counter++){
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

Code: Select all

$found_ips=ARRAY();
$available_ips=ARRAY();

for($counter = 1; $counter <= 255; $counter++)
{
  if(in_array('192.168.241.'.$counter,$ips))
  {
    $found_ips[] = '192.168.241.'.$counter;
  }
  else
  {
    $available_ips[] = '192.168.241.'.$counter;
  }
}
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Excellent - that's done it.

Thanks for your help.

ps - How long did it take you to understand php?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

mhouldridge wrote:Excellent - that's done it.

Thanks for your help.

ps - How long did it take you to understand php?
How long is a piece of string? When does one "understand" PHP? You're constantly learning, everybody is. You just gain knowledge and experience ;)
Post Reply