<?php
class A
{
protected $sql = null;
public function setSQL()
{
$this->sql = "SELECT * FROM users WHERE id = 123";
return $this;
}
public function getSQL()
{
return $this->sql;
}
}
class B extends A
{
public function addSQL()
{
$this->sql .= " AND name = 'Alex'";
return $this;
}
}
$a = new A();
$b = new B();
echo $b->setSQL()->addSQL()->getSQL();
<?php
$query = "SELECT DATE(`time`) `Day`, SUM(`Profit`) `DaylyProfit`
FROM `history`
WHERE `type` = 1 AND `time` > DATE_SUB(CURRENT_DATE, INTERVAL 6 DAY)
GROUP BY `Day`
ORDER BY `Day` DESC;";
$result = $mysqli->query($query);
$all = $result -> fetch_all(MYSQLI_ASSOC);
echo json_encode($all);
<?php
$otp1start = "2021-11-10";
$otp1end = "2021-11-20";
$now_date = date("Y-m-d");
if ($now_date > $otp1end) {
echo "Отпуск уже прошел";
} elseif ($now_date >= $otp1start) {
echo "Сотрудник в отпуске";
} else {
echo "Отпуск еще не наступил";
}
?>
<?php
$board = [
'a' => [
'1' => '',
'2' => '',
'3' => '',
'4' => '',
'5' => '',
'6' => '',
'7' => '',
'8' => '',
],
'b' => [
'1' => 'белый конь',
'2' => '',
'3' => '',
'4' => '',
'5' => '',
'6' => '',
'7' => '',
'8' => '',
],
// ---
'g' => [
'1' => '',
'2' => '',
'3' => '',
'4' => '',
'5' => '',
'6' => '',
'7' => '',
'8' => '',
],
'h' => [
'1' => '',
'2' => '',
'3' => '',
'4' => '',
'5' => '',
'6' => '',
'7' => '',
'8' => '',
]
];
// ход конем b1 -> a3
$board['a']['3'] = 'белый конь';
$board['b']['1'] = '';
print_r($board);
<?php
$start = microtime(TRUE);
sleep(3);
$diff = microtime(TRUE) - $start;
printf('Execution time: %s s', $diff);
echo preg_replace('/<p>[^<]+</', '<p>qwe<', '<div>123</div>');
echo PHP_EOL;
echo preg_replace('/<p>[^<]+</', '<p>qwe<', '<p>123</p>');
echo PHP_EOL;
echo preg_replace('/<p>[^<]+</', '<p>qwe<', '<p>123<a href="/123">123</a></p>');
echo PHP_EOL;
$result = mysqli_query($link, "SELECT * FROM mytable");
if ($result) {
echo "ok";
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
}
SELECT
GREATEST(0, -- return 0 in case negative balance
SUM(
IF(`status` = 1 AND `date_bonus_to` > NOW() , `points`, 0) -- bonus added and not expired
- IF(`status` = 2, `points`, 0) -- bonus used
)
) `balance`
FROM `ps_bonus_account`
WHERE
`id_customer` = 518 AND
`paid` = 1
GROUP BY `id_customer`;
<?php
$htmlstr = '<p><span>В связи с временными ограничениями направляем пояснительные документы к Указам правительства Москвы и МО<br />Скачать файлы вы можете в СФМ в разделе <a href="http://test.com/announce/view?id=1" target="_blank" rel="noopener">рекламные материалы.</a></span></p>';
$dom = new DOMDocument();
$dom->loadHTML($htmlstr);
$href = $dom->getElementsByTagName('a')[0]->getAttribute('href');
printf("href: %s", $href);
print_r(parse_url($href));
CREATE TABLE categories (
id int auto_increment primary key,
name varchar(64)
);
<?php
function getRightCategories($db)
{
$result = $db->query('SELECT id, name FROM categories');
return $result->fetchAll(PDO::FETCH_KEY_PAIR);
}
$categories = getRightCategories($pdo);
print_r($categories);
<?php
$htmlstr = '<script type="text/javascript" charset="utf-8" async src="https://api-maps.yandex.ru/services/constructor/1.0/js/?um=constructor%3Adb7dbcf66a9b7b9da6551f0e57faacc7d56b5eefea90c5a357479ff1bcc47211&width=100%25&height=400&lang=ru_RU&scroll=true"></script>';
$dom = new DOMDocument();
$dom->loadHTML($htmlstr);
$src = $dom->getElementsByTagName('script')[0]->getAttribute('src');
printf("Script src: %s", $src);
$Subscriptions->end_date = date("Y-m-d H:i:s", strtotime($Subscriptions->end_date . " +1 month"));
$subscription->save();
$stmt = $connection->prepare(
"SELECT * FROM users WHERE email = ? OR phone = ?"
);
$stmt->bind_param("ss", $email, $phone);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows() > 0) {
echo "Вы уже оставили заявку";
} else {
echo "Новая заявка";
}
CREATE TABLE users (
email varchar(255),
phone varchar(255),
UNIQUE KEY (email),
UNIQUE KEY (phone)
);
$stmt = $connection->prepare(
"INSERT INTO users (email, phone) VALUES (?, ?)"
);
$stmt->bind_param("ss", $email, $phone);
try {
$stmt->execute();
} catch (Exception $e) {
die("Вы уже оставили заявку");
}
echo "Заявка сохранена в системе";
update routes set
seq = if(trunk_id = 118, 0, trunk_id)
where route_id = 16;
update routes set
seq = if(trunk_id = 118, 0, seq+1)
where route_id = 16;
// convert array to apopriate form
$name_colors = array_combine(
array_values($arr['name']),
array_values($arr['color']),
);
// print select options
foreach($name_colors as $name=>$color) {
printf('<option name="chapter" style="color:%s" class="user-chapters" >%s</option>'.PHP_EOL, $color, $name);
}
echo preg_replace('/\A\/\//i', 'https://', 'https://exzmple1.com');
echo preg_replace('/\A\/\//i', 'https://', '//www.example2.com');