foreach($array as $key => $value){
$string = str_replace($key, $value, $string);
}
<?php
namespace app\Common\Mysql;
final class Connection
{
protected $link;
public function __construct() {
if ( is_null($this->link) ) {
try {
$attr = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
];
$this->link = new \PDO("mysql:host=localhost;dbname=dbname;charset=utf8", "user", "pass", $attr);
} catch (\PDOException $e) {
file_put_contents('log.txt', $e->getMessage(), FILE_APPEND);
}
}
}
/**
* @return \PDO;
*/
public function link() {
return $this->link;
}
}
<?php
namespace app\Common\Model;
use app\Common\Mysql\Connection;
abstract class BaseModel {
private $connection;
/**
* prefix for tables
*/
const PREFIX = "";
/**
* @var
*/
protected $table;
/**
* @var
*/
protected $key;
public function __construct(Connection $conn) {
$this->connection = $conn->link();
}
public function findAll() {
return $this->fetch( "SELECT * FROM " . self::PREFIX . $this->table );
}
/**
* @param array $args
* @param $sql
* @return array|string
*/
public function findBy(Array $args, $sql) {
$stmt = $this->connection->prepare($sql);
$data = "";
if ( $stmt->execute($args) ) {
while ($row = $stmt->fetch()) {
$data[] = $row;
}
}
return $data;
}
/**
* @param $query
* @return mixed
*/
protected function fetch($query) {
$stmt = $this->connection->query($query);
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
$data = "";
while ( $row = $stmt->fetch() ) {
$data[] = $row;
}
return $data;
}
protected function save(Array $array, $sql) {
$sth = $this->connection->prepare($sql);
return $sth->execute($array);
}
protected function deleteById($id) {
$id = (int)$id;
return $this->connection->exec('DELETE FROM ' . self::PREFIX . $this->table . ' WHERE id = '.$id.'');
}
}
function addSpaces(str){
return str.split('').join(' ');
}
alert(addSpaces('anyword'));
(function ($) {
'use strict';
$('[name=upload]').on('change', function (e) {
var selected = $('[name=upload]:checked'),
checkbox = $('#parts_clear');
if ('image_upload' === selected.attr('id')) {
checkbox.prop('disabled', true);
} else {
checkbox.prop('disabled', false);
}
});
}(jQuery));
mysql> select * from `SC_categories`;
+-------------+--------+
| category_ID | parent |
+-------------+--------+
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
| 4 | 0 |
| 5 | 4 |
| 6 | 2 |
| 7 | 6 |
+-------------+--------+
7 rows in set (0,00 sec)
mysql> SELECT `SC_categories`.* FROM `SC_categories` LEFT JOIN (SELECT DISTINCT(`parent`) FROM `SC_categories`) AS `list` ON (`SC_categories`.`category_ID` = `list`.`parent`) WHERE `list`.`parent` IS NULL;
+-------------+--------+
| category_ID | parent |
+-------------+--------+
| 3 | 2 |
| 5 | 4 |
| 7 | 6 |
+-------------+--------+
3 rows in set (0,00 sec)
public static function createXMLYML() {
$obj = new Obj();
$EUR = $obj->get_convert_cb("EUR");
....
}
$file = file('in.txt');
$data = array();
foreach($file as $i => $line)
{
$data[$i] = explode('|', $line);
}
if (preg_match("/photogallery\/[\d]+\/$/", $_SERVER['REQUEST_URI'])){
echo "$title_photo";
}
else {
echo $title;
}
if (preg_match("/photogallery\/[\d]+\/?$/", $_SERVER['REQUEST_URI'])){
echo "$title_photo";
}
else {
echo $title;
}