Переименовал классы и добавил выборку из второй БД
This commit is contained in:
parent
1f96b88ac2
commit
0b56cd37b5
43
app/app.php
43
app/app.php
@ -1,35 +1,48 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
use App\Library\SvedenParser;
|
||||
use App\Library\ContingentParser;
|
||||
use App\Library\Database;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
$dbconfig = [
|
||||
"host" => "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 "<pre>";
|
||||
print_r($data);
|
||||
print_r($contingent);
|
||||
echo "</pre>";
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
|
||||
use App\Library\Specialization;
|
||||
use App\Library\ContingentRow;
|
||||
|
||||
class SvedenParser
|
||||
class ContingentParser
|
||||
{
|
||||
private \DOMXPath $xpath;
|
||||
private string $template;
|
||||
@ -44,18 +44,17 @@ class SvedenParser
|
||||
public function getDataTable() : array
|
||||
{
|
||||
$data = $this->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;
|
||||
}
|
33
app/library/ContingentRow.php
Normal file
33
app/library/ContingentRow.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
// Специальность, направление подготовки
|
||||
class ContingentRow
|
||||
{
|
||||
public function __construct(
|
||||
private string $eduCode,
|
||||
private string $eduName,
|
||||
private string $eduLevel,
|
||||
private string $eduForm,
|
||||
private int $contingent
|
||||
) {
|
||||
if ($contingent < 0) {
|
||||
throw new \Exception("Недействительная численность обучающихся!");
|
||||
}
|
||||
$this->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
|
||||
];
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
// Специальность, направление подготовки
|
||||
class Specialization
|
||||
{
|
||||
// Код специальности
|
||||
private string $eduCode;
|
||||
// Название специальности, направления подготовки
|
||||
private string $eduName;
|
||||
//Уровень образования
|
||||
private string $eduLevel;
|
||||
//Формы обучения
|
||||
private string $eduForm;
|
||||
// Общая численность обучающихся
|
||||
private int $contingent;
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function update(
|
||||
string $eduCode,
|
||||
string $eduName,
|
||||
string $eduLevel,
|
||||
string $eduForm,
|
||||
int $contingent
|
||||
) : void {
|
||||
$this->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
|
||||
];
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
<?php
|
||||
require_once 'log/log.php';
|
||||
require_once 'vendor/autoload.php';
|
||||
require_once 'app/app.php';
|
@ -1,4 +0,0 @@
|
||||
<?php
|
||||
ini_set('display_errors', 0);
|
||||
ini_set('log_errors', 1);
|
||||
ini_set('error_log', 'log/php-error.log');
|
@ -1,7 +0,0 @@
|
||||
[08-Aug-2024 10:24:24 UTC] PHP Warning: Undefined variable $array in /home/developer/sp/app/library/Database.php on line 61
|
||||
[08-Aug-2024 10:24:24 UTC] PHP Fatal error: Uncaught TypeError: App\Library\Database::select(): Return value must be of type array, null returned in /home/developer/sp/app/library/Database.php:61
|
||||
Stack trace:
|
||||
#0 /home/developer/sp/app/app.php(31): App\Library\Database->select()
|
||||
#1 /home/developer/sp/index.php(4): require_once('...')
|
||||
#2 {main}
|
||||
thrown in /home/developer/sp/app/library/Database.php on line 61
|
Loading…
x
Reference in New Issue
Block a user