I am treading into some new territory working with API’s and classes. I have worked some with including files so I understand kind of how that works.
I don’t really understand how API’s and classes work. I downloaded the API class file for an ip geolocation so I can see where the people who come to my site are from but I have run into a programming problem.
I am logging the ip addresses in a MySQL database with some basic PHP, then I have a button on my control page that shows all the ip addressds. What I am trying to do is display the ip address and next to it the country.
I developed and tested a basic function using the API and thought I would be easy to implement with my current code that just loops through my database displaying the ip addresses. When i tried this i the following error:
Fatal error: Cannot redeclare class ip2location_lite
and the code that produced the error:
[code]
<html>
<head>
<link rel=”stylesheet” type=”text/css” href=”/index_files/format.css” />
<title>SZ</title>
</head>
<body>
<div id=”wrapper”>
<div id=”header”>
<h1>SZ</h1>
</div>
<div id=”main”>
<div id=”content”>
<table id=”table” >
<tr>
<th>Post Number</th>
<th>Title</th>
</tr>
</table>
<?php
//declaration of variables
$con = mysql_connect(“localhost”,”username”,”password”) or die(“Connection Lost”);
$db = “database”;
$table = “table”;
//function to list entries
mysql_select_db($db, $con);
$sql = “SELECT * FROM $table”;
$result = mysql_query($sql) or die(“List Entries Error:” . mysql_error());
function ip_locate($ip)
{
include(‘path_to_class/ip2locationlite.class.php’);
$ipLite = new ip2location_lite;
$ipLite->setKey(‘personal_key’);
$locations = $ipLite->getCity($ip);
$errors = $ipLite->getError();
if(!empty($locations) && is_array($locations))
{
foreach($locations as $field => $val)
{
$field . ” : ” . $val . “<br />”;
}
}
}
while($row = mysql_fetch_array($result))
{
$number = $row[‘Number’];
$ip = $row[‘ip_address’];
$date = date(“F d Y”, $row[‘timestamp’]);
?>
<table id=”table” >
<tr>
<td id=”post_no”><?php echo $number; ?></td>
<td id=”posttitle”><?php echo $ip; ?></td>
<td id=”ipaddress”><?php echo $date; ?></td>
<td id=”location”><?php echo ip_locate($ip); ?></td>
</tr>
</table>
<?php
}
?>
</div>
</div>
</div>
</body>
</html>
i get that i am calling the function multiple times and that redeclares the class but i don’t know how else to make it work. i tried taking the class declaration out of the loop but then it would give other errors.