PHP Help

Associate
Joined
4 May 2003
Posts
582
Guys.

I have a database table that contains a field, lets call it x. The field x contains a n ip address number.

I wish to retrieve this and analyse only the first octet.

For instance in the IP 192.168.0.1 I only want to analyse the 192 part. I have been using the PHP command split to do this. This I believe splits each IP address into an array containing four values (one for each octet).

I have the following sql select statement.

select x from table_name;

I then use mysql_fetch_array to retrieve the IP address values.

This is where I get stuck.

I have two more variables $y and $z which are integers, I wish to evaluate each value I retrieved from the select statement to see if it is greater that 65. If it is greater than 65 I wish $y to increase by one and if it is less that 65 I wish $z to increase by one.

Does Anyone have any pointers? I am pulling my hair out here :D

Thank You In Advance
 
Associate
Joined
5 Jun 2004
Posts
1,338
Location
Hythe, Hants
If I understand you correctly, which i'm not to sure I do :s
PHP:
<?php
$y = row['y'];
$z = row['z'];
if ($y > 65 | $z > 65) {
$y++;
} else {
$z++;
}
// now just update the database.
?>
 
Last edited:
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
Code:
// Perform query and get IP address(s)

// Get individual octets
$octets = explode(".", $ip);
foreach ($ip_octets as $octet)
{
	if ($octet > 65)
	{
		$y++;
	}

	if ($octet < 65)
	{
		$z++;
	}
}

:)
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Iquisitor, your's will analyse all four octets, the OP only wants to look at the 192 part (first octet) :)

Code:
<?php

$oct = intval(subtstr($ip, 0, strpos('.'));

if ($oct > 65) {
    $y++;
} else {
    $z++;
}

?>

btw - what do you want done if the first octet == 65? :)

:p
 

Nim

Nim

Associate
Joined
17 Oct 2002
Posts
1,450
Location
Twickenham
If you have a variable $ip, then the following line will increment $y if the first octet is >65, and increment $z otherwise.
Code:
intval(substr($ip, 0, strpos($ip, '.'))) > 65 ? $y++ : $z++;
Since you're getting the variable out of a database, chances are that the variable is in something like $row['ip'], instead, so you'll need to allocate that to $ip first.

If you just post your code, we can sort it out in about 2 minutes :)
 
Associate
OP
Joined
4 May 2003
Posts
582
Nim said:
If you have a variable $ip, then the following line will increment $y if the first octet is >65, and increment $z otherwise.

Code:
intval(substr($ip, 0, strpos($ip, '.'))) > 65 ? $y++ : $z++;

Since you're getting the row out of a database, chances are that the variable is in something like $row['ip'], instead, so you'll need to allocate that to $ip first.

If you just post your code, we can sort it out in about 2 minutes :)

My Code Is As Follows:

Code:
Code To Connect To Database;

$sql = select x from table;

$result = mysql_query($sql);

$ip_address = mysql_fetch_array($result);

Thats As Far As I can get, I now need to enter the code to split the IP address so I only look at the first octet and then the code to increment $y or $z.
 
Associate
Joined
23 May 2005
Posts
812
Location
Hove
Code:
while ($ip_address = mysql_fetch_array($result)) {
$octet = intval(substr($ip_address['x'], 0, strpos($ip_address['x'],'.')));
$y = 1;
$z = 100;
if ($octet > 65) {
    $y++;
} else {
    $z++;
}
}

or using the explode function that you said you were using earlier

Code:
while ($ip_address = mysql_fetch_array($result)) {
$octet = explode('.', $ip_address['x']);
$y = 1;
$z = 100;
if ($octet[0] > 65) {
    $y++;
} else {
    $z++;
}
}
 
Last edited:
Back
Top Bottom