menu_category
~~~~~~~
id
name
order
menu_items
~~~~~~~~~~~
id
menu_category_id
name
url
order
SELECT
`c`.`id` as `id`,
`c`.`name` as `name`,
`i`.`id` as `item_id`,
`i`.`name` as `item_name`,
`i`.`url` as `item_url`
FROM `menu_category` as `c`
LEFT JOIN `menu_items` as `i` ON `i`.`menu_category_id`=`c`.`id`
ORDER BY `c`.`order`, `i`.`order`
$menu = [];
foreach ($results as $res) {
$menu[$res['id']]['name'] = $res['name'];
$menu[$res['id']]['items'][] = [
'name' => $res['item_name'],
'url' => $res['item_url'],
]
}
[1]=>[
'name' => 'Категория 1',
'items' => [
['name'=>'Ссылка 11', 'url'=>'http://site11.ru'],
['name'=>'Ссылка 12', 'url'=>'http://site12.ru'],
]
],
[2]=>[
'name' => 'Категория 2',
'items' => [
['name'=>'Ссылка 21', 'url'=>'http://site21.ru'],
['name'=>'Ссылка 22', 'url'=>'http://site22.ru'],
]
],
echo '<ul>';
foreach($menu as $catId=>$cat) {
echo '<li>'.$cat['name'];
if (isset($cat['items'])) {
echo '<ul>';
foreach($cat['items'] as $item) {
echo '<li><a href="'.$item['url'].'">'.$item['name'].'</a></li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
$start = new DateTime('01.08.2014');
$end = new DateTime('30.08.2015 23:59');
$interval = new DateInterval('P1D');
$dateRange = new DatePeriod($start, $interval, $end);
$weekNumber = 1;
$weeks = array();
foreach ($dateRange as $date) {
$weeks[$weekNumber][] = $date->format('Y-m-d');
if ($date->format('w') == 0) {
$weekNumber++;
}
}
echo '<pre>';
print_r($weeks);
echo '</pre>';
$func = 'trim';
if (function_exists($func)) echo call_user_func($func,' Test ');
$_POST['function'] = 'trim';
$array = [' a', 'b '];
$allowedFunctions = ['trim'];
if (function_exists($_POST['function']) && in_array($_POST['function'], $allowedFunctions)) {
$results = array_map($_POST['function'], $array);
print_r($results);
}
<?php
include 'simple_html_dom.php';
$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$cacheFile = dirname(__FILE__).'/'.md5($url);
if (!file_exists($cacheFile)) {
if ($html = file_get_contents($url)) {
file_put_contents($cacheFile, $html);
} else die('Can\'t get html');
} else {
$html = file_get_contents($cacheFile);
}
$items = [];
$dom = str_get_html($html);
if ($domElements = $dom->find('div.covGroupBoxContent')){
foreach($domElements as $domBox) {
if (!strpos($domBox->plaintext, 'Key stats')) continue;
if ($domBoxElements = $domBox->find('div.covSmallHeadline')) {
$item = [];
foreach($domBoxElements as $k=>$element) {
if (!$k) continue;
$item[] = $element->plaintext;
if ($k%2==0) {
$items[] = $item;
$item = [];
}
}
}
}
}
echo '<pre>';
print_r($items);
echo '</pre>';
Array
(
[0] => Array
(
[0] => Maps played
[1] => 539
)
[1] => Array
(
[0] => Wins / draws / losses
[1] => 363 / 8 / 168
)
[2] => Array
(
[0] => Total kills
[1] => 50715
)
[3] => Array
(
[0] => Total deaths
[1] => 45101
)
[4] => Array
(
[0] => Rounds played
[1] => 14083
)
[5] => Array
(
[0] => K/D Ratio
[1] => 1.12
)
[6] => Array
(
[0] => Best player (Average rating)
[1] => olofmeister (1.15)
)
)
$inputArr = file('input', FILE_SKIP_EMPTY_LINES); // читаем из файла все данные
$outputArr = []; // пустой выходной массив
foreach ($inputArr as $line) { // читаем построчно входные данные
list($ip, $mask) = explode('/', trim($line)); // разбиваем строку на $ip и $mask, по разделителю '/'
$ipAsLongInt = ip2long($ip); // Преобразуем ip адрес в десятичное число
$ipAsBinary = decbin($ipAsLongInt); // Преобразуем число в бинарное число
$ipAsBinaryWithCorrectLength = str_pad($ipAsBinary, 32, 0, STR_PAD_LEFT); // дополняем слева нулями до длинны 32 символа
$outputLine = substr($ipAsBinaryWithCorrectLength, 0, $mask); // Копируем из строки только первые $mask символов
$outputArr[] = $outputLine;
}
$outputRaw = join(PHP_EOL, $outputArr); // Соединяем все строки из массива символом перевода строки
file_put_contents('output', $outputRaw); // Записываем данные в выходной файл
class Sql {
private $connectionData = [];
public function __construct(array $connectionData) {
$this->$connectionData = $connectionData;
}
public function connect(){
$ret = false;
$dbLink = mysql_connect(
$this->connectionData['host'],
$this->connectionData['username'],
$this->connectionData['password']
);
if (mysql_select_db($this->connectionData['dbname'])) {
$ret = $dbLink;
}
return $ret;
}
public function get_results($sql) {
$ret = false;
$results = mysql_query($sql);
if (mysql_num_rows($results)) {
$ret = [];
while ($row = mysql_fetch_assoc($results)) {
$ret[] = $row;
}
}
return $ret;
}
}
class ArticlesModel {
private $dbLink = null;
public function __construct($dbLink) {
$this->dbLink = $dbLink;
}
public function getAll(){
return $this->dbLink->get_results('SELECT * FROM `articles`');
}
}
$connectionData = [
'host' => 'localhost',
'username' => 'mysql',
'pass' => 'mysql',
'dbname' => 'mysite',
];
$dbLink = (new Sql($connectionData))->connect();
if (!$dbLink) trigger_error("Can't connect to DB", E_USER_ERROR);
$articlesModel = new ArticlesModel($dbLink);
if (!$articlesModel) trigger_error("Can't create articles model", E_USER_ERROR);
if ($articles = $articlesModel->getAll()) {
foreach ($articles as $article) {
echo '<h2>'.$article['title'].'</h2>';
echo '<p> '.$article['text'].'</p>';
echo '<hr />';
}
}
function fix_session_register(){
function session_register(){
$args = func_get_args();
foreach ($args as $key){
$_SESSION[$key]=$GLOBALS[$key];
}
}
function session_is_registered($key){
return isset($_SESSION[$key]);
}
function session_unregister($key){
unset($_SESSION[$key]);
}
}
if (!function_exists('session_register')) fix_session_register();