Skip to content

Instantly share code, notes, and snippets.

@platinize
Last active February 28, 2018 14:43
Show Gist options
  • Save platinize/adff80773aed6e760bc11dfee02c1ade to your computer and use it in GitHub Desktop.
Save platinize/adff80773aed6e760bc11dfee02c1ade to your computer and use it in GitHub Desktop.
Php13Tasks.php
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
include 'WorkWithDatabase.php';
include 'WorkWithXML.php';
$db=new WorkWithDatabase('root','','localhost','utf8','dz11');
$wXml=new WorkWithXML('root','','localhost','utf8','dz11');
$dataBaseConnectionsWithParams=$db->connectWithParams('localhost','dz11','root','');
$table = 'users_dz11';
?>
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
include 'Connect.php';
include 'transitionalXML.php';
$xml = new SimpleXMLELement($xmlstr);
$wXml->getTableSetXML($table, $xml);
$wXml->createXML('result.xml', $xml->asXML());
$wXml->setXMLToTable($table, 'forBd.xml');
?>
<?php
$xmlstr = <<<XML
<?xml version='1.0' ?>
<users>
</users>
XML;
?>
<?php
class WorkWithDatabase {
protected $user;
protected $pass;
protected $pdo;
protected $host;
protected $db;
protected $charset;
public function __construct($user,$pass,$host,$charset,$db) {
$this->user = $user;
$this->pass = $pass;
$this->host = $host;
$this->charset = $charset;
$this->db = $db;
$this->pdo = $this->connectWithParams($this->host,$this->db,$this->user,$this->pass);
}
public function connectWithParams($host,$db,$user,$pass){
$params='mysql:host='.$host.';dbname='.$db;
$opt = array(
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC
);
return new PDO($params,$user,$pass,$opt);
}
public function getColumnName($tableName) {
$stm=$this->pdo->query('SELECT COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ="'.$tableName.'"');
$users = $stm->fetchAll(PDO::FETCH_COLUMN);
unset($users[0]);
return $users;
}
public function getAll($tableName) {
$stm=$this->pdo->query('SELECT * From '.$tableName);
$users = $stm->fetchAll();
return $users;
}
public function add($tableName, $arr) {
$sql = '';
foreach ($arr as $key => $value) {
$sqlVal = '(';
foreach ($value as $key => $val) {
$sqlVal .= ($sqlVal == '(')? '"'.$val.'"' : ', "'.$val.'"';
}
$sql .= ($sql == '')? $sqlVal.')' : ', '.$sqlVal.')';
}
$stm=$this->pdo->prepare('INSERT INTO '.$tableName.'('.implode(",", $this->getColumnName($tableName)).') VALUES '.$sql);
return $stm->execute();
}
}
?>
<?php
class WorkWithXML extends WorkWithDatabase {
public function getTableSetXML($tableName, $xml){
$i = 0;
foreach ($this->getAll($tableName) as $key => $value) {
$xml->addChild('item');
$xml->item[$i]->addAttribute('id', $value['id']);
unset($value['id']);
foreach ($value as $key => $val) {
$xml->item[$i]->addChild($key, $val);
}
$i++;
}
}
public function createXML($fileName, $val = ''){
$file = fopen($fileName, 'w+');
fwrite($file, $val);
fclose($file);
}
public function setXMLToTable($tableName, $fileName){
$file = file_get_contents($fileName);
$xml = new SimpleXMLELement($file);
$this->add($tableName, $xml);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment