diff --git a/app/app.php b/app/app.php index f89c77d..c4e18d0 100644 --- a/app/app.php +++ b/app/app.php @@ -1,35 +1,48 @@ "10.90.1.201", - "database" => "opendata", + "database1" => "opendata", + "database2" => "niimko", "user" => "niimko_user", "password" => "MOhA17FeboXE" ]; -$client = new Client(); -$response = $client->get('https://marsu.ru/sveden/education/eduChislen.php'); -$html = $response->getBody()->getContents(); - -$parser = new SvedenParser($html, '//tr[@itemprop="eduChislen"]//'); -$data = $parser->getDataTable(); - -$db = new Database( - "mysql:host={$dbconfig['host']};dbname={$dbconfig['database']}", +$dbOpendata = new Database( + "mysql:host={$dbconfig['host']};dbname={$dbconfig['database1']}", $dbconfig['user'], $dbconfig['password'] ); -// print_r($data); +$dbNiimko = new Database( + "mysql:host={$dbconfig['host']};dbname={$dbconfig['database2']}", + $dbconfig['user'], + $dbconfig['password'] +); -// $db->insert('sveden_table_education', $data); -$data = $db->select('sveden_education_contingent'); +$client = new Client(); +$response = $client->get('https://marsu.ru/sveden/education/eduChislen.php'); +$html = $response->getBody()->getContents(); +$parser = new ContingentParser($html, '//tr[@itemprop="eduChislen"]//'); +$contingent = $parser->getDataTable(); +$specializations = $dbNiimko->selectWhere('s_specs', ['id', 'kod'], ['oopkodes' => 'gos3p']); + +foreach ($contingent as $key => $con) { + $needle = $con['spec_code']; + foreach ($specializations as $spec) { + if ($needle == $spec['kod']) { + $contingent[$key] += ['spec_id' => $spec['id']]; + } + } +} +// $dbOpendata->insert('sveden_education_contingent', $data); +// $dbOpendata = $db->select('sveden_education_contingent'); echo "
";
-print_r($data);
+print_r($contingent);
 echo "
"; \ No newline at end of file diff --git a/app/library/SvedenParser.php b/app/library/ContingentParser.php similarity index 90% rename from app/library/SvedenParser.php rename to app/library/ContingentParser.php index 3024297..1b9a898 100644 --- a/app/library/SvedenParser.php +++ b/app/library/ContingentParser.php @@ -1,9 +1,9 @@ parse(); - $spec = new Specialization(); $records = array(); for ($i = 0; $i < $data['numberAll']->length; $i++) { - $spec->update( + $contingentRow = new ContingentRow( $data['eduCode']->item($i)->textContent, $data['eduName']->item($i)->textContent, $data['eduLevel']->item($i)->textContent, $data['eduForm']->item($i)->textContent, (int)$data['numberAll']->item($i)->textContent ); - $records[] = $spec->getData(); + $records[] = $contingentRow->getData(); } return $records; } diff --git a/app/library/ContingentRow.php b/app/library/ContingentRow.php new file mode 100644 index 0000000..632da2d --- /dev/null +++ b/app/library/ContingentRow.php @@ -0,0 +1,33 @@ +eduCode = $eduCode; + $this->eduName = $eduName; + $this->eduLevel = $eduLevel; + $this->eduForm = $eduForm; + $this->contingent = $contingent; + } + + public function getData() : array + { + return [ + "spec_code" => $this->eduCode, + "spec_name" => $this->eduName, + "edu_level" => $this->eduLevel, + "edu_forms"=> $this->eduForm, + "contingent" => $this->contingent + ]; + } +} \ No newline at end of file diff --git a/app/library/Database.php b/app/library/Database.php index b0bac0c..ac17fbe 100644 --- a/app/library/Database.php +++ b/app/library/Database.php @@ -6,9 +6,10 @@ use PDOException; class Database extends PDO { - public function __construct($dsn, $username, $password) + public function __construct(string $dsn, string $username, string $password) { try { + $dsn .= ";charset=utf8"; parent::__construct( $dsn, $username, @@ -28,20 +29,22 @@ class Database extends PDO public function insert(string $table, array $data) : void { - $stmt = $this->prepare(" - INSERT INTO sveden_table_education - (spec_code, spec_name, edu_forms, edu_level, contingent) - VALUES - (:spec_code, :spec_name, :edu_forms, :edu_level, :contingent)" - ); - foreach ($data as $row) { - try { - $stmt->bindParam(':spec_code', $row['eduCode']); - $stmt->bindParam(':spec_name', $row['eduName']); - $stmt->bindParam(':edu_forms', $row['edoForms']); - $stmt->bindParam(':edu_level', $row['eduLevel']); - $stmt->bindParam(':contingent', $row['contingent']); + $keys = array_keys($data); + $sql = "INSERT INTO $table ("; + foreach ($keys as $key) { + $sql .= "$key,"; + } + $sql = substr_replace($sql, ")", -1); + $sql .= " VALUES ("; + foreach ($keys as $key) { + $sql .= ":$key,"; + } + $sql = substr_replace($sql,"", -1); + $stmt = $this->prepare($sql); + foreach ($data as $key => $row) { + try { + $stmt->bindParam(":$key", $row[$key]); $stmt->execute(); } catch (PDOException $e) { echo "Ошибка запроса: " . $e->getMessage() . "\n"; @@ -49,7 +52,7 @@ class Database extends PDO } } - public function select(string $table) : array + public function select(string $table) : array|null { $stmt = $this->prepare("SELECT * FROM $table"); try { @@ -61,4 +64,34 @@ class Database extends PDO return $array; } } + + public function selectWhere( + string $table, + array $atributes, + array $filters + ) : array|null { + // Строим запрос + $sql = "SELECT "; + foreach ($atributes as $atribute) { + $sql .= "$atribute,"; + } + $sql = substr_replace($sql," ", -1); + $sql .= "FROM $table WHERE "; + foreach ($filters as $key => $filter) { + $sql .= "$key = :$key "; + } + $stmt = $this->prepare($sql); + foreach ($filters as $key => $filter) { + $stmt->bindParam(":$key", $filter); + } + // Выполняем + try { + $stmt->execute(); + $array = $stmt->fetchAll(PDO::FETCH_ASSOC); + } catch (PDOException $e) { + echo "Ошибка запроса: " . $e->getMessage() . "\n"; + } finally { + return $array; + } + } } \ No newline at end of file diff --git a/app/library/Specialization.php b/app/library/Specialization.php deleted file mode 100644 index 7e30c03..0000000 --- a/app/library/Specialization.php +++ /dev/null @@ -1,43 +0,0 @@ -eduCode = $eduCode; - $this->eduName = $eduName; - $this->eduLevel = $eduLevel; - $this->eduForm = $eduForm; - $this->contingent = $contingent; - } - - public function getData() : array - { - return [ - "eduCode" => $this->eduCode, - "eduName" => $this->eduName, - "eduLevel" => $this->eduLevel, - "edoForms"=> $this->eduForm, - "contingent" => $this->contingent - ]; - } -} \ No newline at end of file diff --git a/index.php b/index.php index 21fae64..e4dd67b 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,3 @@ select() -#1 /home/developer/sp/index.php(4): require_once('...') -#2 {main} - thrown in /home/developer/sp/app/library/Database.php on line 61