Page 2 of 2

Posted: Thu Jun 30, 2005 8:12 am
by mhouldridge
Should I be outputting $found_ip instead?


thanks,

Posted: Thu Jun 30, 2005 9:50 am
by pickle
Yep. That's what the for loop is for, generating an IP and seeing if it's assigned or not. If it isn't it's stored in $found_ip.

Posted: Thu Jun 30, 2005 10:44 am
by mhouldridge
yes - I see now...

er... When I try to output the found_ip i get the following error;

Notice: Undefined variable: found_ip in F:\Auditwebsite\IP2.php on line 81


Im not sure why it is saying this, as I have define found_IP as $found_ip = ('192.168.1.'.$counter);

Posted: Thu Jun 30, 2005 10:52 am
by pickle
Hmm. That error is displayed likely because you are referencing $found_ip outside of it's scope (the for loop). However, I've never had a problem with that before. Try declaring $found_ip = FALSE; right before the if(!$result) line.

Posted: Thu Jun 30, 2005 11:34 am
by mhouldridge
Bit lost now (have been from the start to be honest)

Here is the last par tof my code, please could you shed some light on it;

Code: Select all

while($row = mysql_fetch_assoc($result)){
							$ips[] = $row['IP'];
							} 
							
							for	($counter = 0; 	
							$counter >= 255; 															
							++$counter){
							$found_ip = 0;
							if(!in_array('194.131.241.'.$counter,$ips))							
								{
								$found_ip = ('194.131.241.'.$counter);								
																
							}}
							
								echo'<pre>';	
								print_r($found_ip);
								echo '</pre>'																								
							
							?>

Posted: Thu Jun 30, 2005 11:53 am
by pickle

Code: Select all

//assuming $result gets all ips already stored in the db,
//this loop goes through the result set and puts all the
//ips in the $ips array
while($row = mysql_fetch_assoc($result))
{
  $ips[] = $row['IP'];
} 

//this for loop loops through each possible ip 
//in the 194.131.241.X range

//$counter needs to start at 1.  You had it at 0, 
//and the original code I gave had it at 'i' (WRONG!)
$found_ip = 0; //moved this declaration outside the for loop
for ($counter = 1; $counter >= 255; ++$counter)
{
  //for each dynamically created ip, check if it exists
  //in the $ips array.
  if(!in_array('194.131.241.'.$counter,$ips))                            
  {
    //if it doesn't, then the current IP is free
    //to use
    $found_ip = ('194.131.241.'.$counter);
    break;//no sense continuing if we've found what we need                                
   }
}
                                                                                               
//$found_ip is a string, so we call echo, rather than print_r
echo $found_ip
Does this clear things up?

Posted: Fri Jul 01, 2005 5:58 am
by mhouldridge
Yeah, that's good, clears everything up....

With this new code my result is simply zero. I can see that the array has the listed taken IPs, so the query is working and pulling them into the $ips variable.

hmmm....

Your help so far has been fantastic, thanks again.

Posted: Fri Jul 01, 2005 9:00 am
by mhouldridge
It's sorted now,

The following code works;

Code: Select all

while($row = mysql_fetch_assoc($result)){
							$ips[] = $row['IP'];
							} 
							
						$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[] = '194.131.241.'.$counter;  }
						}
							
							echo'<pre>';
							print_r($available_ips); 
							echo '</pre>'