Kod pochodzi "PHP5 Zaawansowane programowanie"(został podpięty pod MySQL) - w przeglądarce pojawia się błąd:
Cytat
Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\php5\class.DataManager.php on line 128
WTF? Już nie wiem jak do tego podejść próbowałem z przodu i tyłem do przodu, ale nic nie buczy.
Poniżej umieszczam poprawiony przeze mnie kod:
<?php
require_once('class.Entity.php'); //potrzebne później
require_once('class.Individual.php');
require_once('class.Organization.php');
class DataManager
{
private static function _getConnection
() {
return $hDB;
}
$hDB = mysqli_connect('localhost', 'root', 'cu9DQppl', 'rozd3')
or
die("Błąd połączenia z bazą danych!"); return $hDB;
}
public static function getAddressData
($addressID) { $sql = "SELECT * FROM adres WHERE adres_id ='". $addressID."'";
$res = mysqli_query(DataManager::_getConnection(), $sql);
if(! ($res && mysqli_num_rows($res))) {
die("Błąd odczytu danych dla adresu pocztowego'" . $addressID . "'"); }
return mysqli_fetch_assoc($res);
}
public static function getEmailData
($emailID) {
$sql = "SELECT * FROM email WHERE email_id ='". $emailID ."'";
$res = mysqli_query(DataManager::_getConnection(), $sql);
if(! ($res && mysqli_num_rows($res))) {
die("Błąd odczytu danych dla adresu email'" . $emailID . "'"); }
return mysqli_fetch_assoc($res);
}
public static function getPhoneNumberData
($phoneID) {
$sql = "SELECT * FROM telefon WHERE telefon_id ='". $phoneID ."'";
$res = mysqli_query(DataManager::_getConnection(), $sql);
if(! ($res && mysqli_num_rows($res))) {
die("Błąd odczytu danych dla numeru telefonu'" . $phoneID . "'"); }
return mysqli_fetch_assoc($res);
}
public static function getEntityData
($entityID) {
$sql = "SELECT * FROM jednostka WHERE jednostka_id = '". $entityID ."'";
$res = mysqli_query(DataManager::_getConnection(),$sql);
if(! ($res && mysqli_num_rows($res)))
die("Błąd odczytu jednostki '" . $entityID . "'");
return mysqli_fetch_assoc($res);
}
public static function getAddressObjectsForEntity
($entityID) { $sql = "SELECT adres_id FROM adres WHERE jednostka_id ='". $entityID ."'";
$res = mysqli_query(DataManager::_getConnection(), $sql);
if(!$res)
die("Błąd odczytu adresów dla jednostki'" . $entityID . "'");
if(mysqli_num_rows($res))
{
while($rec = mysqli_fetch_assoc($res))
$objs[] = new Address($rec['adres_id']);
return $objs;
}
else
}
public static function getEmailObjectsForEntity
($entityID) {
$sql = "SELECT email_id FROM email WHERE jednostka_id ='". $entityID ."'";
$res = mysqli_query(DataManager::_getConnection(), $sql);
if(!$res)
die("Błąd odczytu adresów email dla jednostki'" . $entityID . "'");
if(mysqli_num_rows($res))
{
while($rec = mysqli_fetch_assoc($res))
$objs[] = new EmailAddress($rec['email_id']);
return $objs;
}
else
}
public static function getEmployer
($individualID) {
$sql = "SELECT organizacja_id FROM zatrudniony WHERE osoba_id ='". $individualID ."'";
$res = mysqli_query(DataManager::_getConnection(),$sql);
if(! ($res && mysqli_num_rows($res)))
die("Błąd odczytu pracodawcy dla osoby '".$individualID."'");
$row = mysqli_fetch_assoc($res);
if($row)
return new Organization($row['organizacja_id']);
else
return null;
}
public static function getEmployees
($orgID) {
$sql = "SELECT osoba_id FROM zatrudniony WHERE organizacja_id = '". $orgID ."'";
$res = mysqli_query(DataManager::_getConnection(), $sql);
if(! ($res && mysqli_num_rows($res)))
die("Błąd odczytu informacji o zatrudnionych dla organizacji'" . $orgID .`
"'");
if(mysqli_num_rows($res))
{
while($row = mysqli_fetch_assoc($res))
$objs[] = new Individual($row['osoba_id']);
return $objs;
}
else
}
public static function getAllEntitiesAsObjects
() {
$sql = "SELECT jednostka_id, ctype FROM jednostka";
$res = mysqli_query(DataManager::_getConnection(), $sql);
if(!$res)
die("Bład pobierania wszystkich jednostek!");
if(mysqli_num_rows($res))
{
while($row = mysqli_fetch_assoc($res))
{
if($row['typ'] == '1')
$objs[] = new Individual($row['jednostka_id']);
elseif ($row['typ'] == '0')
$objs[] = new Organization($row['jednostka_id']);
else
die("Nieznany typ jednostki" . $row['typ'] . "!"); }
return $objs;
}
else
}
}
?>
skrypt testowy
<?php
require_once('class.DataManager.php'); // ten plik dołącza pozostałe
function println($data) {
}
$arContacts = DataManager::getAllEntitiesAsObjects();
foreach($arContacts as $objEntity) {
if(get_class($objEntity) == 'Individual') {
print "<h1>Osoba -' {$objEntity->__toString()}</h1>"; } else {
print "<h1>Organizacja - {$objEntity->__toString()}</h1>"; }
if($objEntity->getNumberOfEmails()) {
// Posiada adresy email, wypisz nagłówek
print "<h2>Adresy email</h2>";
for($x=0; $x < $objEntity->getNumberOfEmails(); $x++) {
println($objEntity->emails($x)->__toString());
}
}
if($objEntity->getNumberOfAddresses()) {
// Posiada adresy pocztowe
print "<h2>Adresy pocztowe</h2>";
for($x=0; $x < $objEntity->getNumberOfAddresses(); $x++) {
println($objEntity->addresses($x)->__toString());
}
}
if($objEntity->getNumberOfPhoneNumbers()) {
// Posiada numery telefonu
print "<h2>Numery telefonu</h2>"; for($x=0; $x < $objEntity->getNumberOfPhoneNumbers(); $x++) {
println($objEntity->phonenumbers($x)->__toString());
}
}
}
?>
Ten post edytował lDoran 7.08.2010, 17:03:34