This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function secure_data($data) { | |
| if ($data) { | |
| if (is_array($data)) { | |
| foreach ($data as $key => $val) | |
| $data[$key] = htmlspecialchars(trim($val)); | |
| } | |
| else { | |
| $data = htmlspecialchars(trim($data)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //On the page for redirect | |
| if (!$isAuth) { | |
| $_SESSION['flash_message']='{YOU MESSAGE}'; | |
| header('Location: login.php'); | |
| exit(); | |
| } | |
| //On the page for redirect | |
| if (isset($_SESSION['flash_message'])) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function db_query($sql, $params = []) | |
| { | |
| // Подключаемся к базе (функция подключения есть в предыдущем сниппете) | |
| $db = db_connect(); | |
| // Подготавливаем запрос | |
| $query = $db->prepare(($sql)); | |
| // Выполняем запрос | |
| $query->execute($params); | |
| // Проверка на ошибки (функция в следующем сниппете) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Проверка запроса на ошибки | |
| function db_check_error($query) | |
| { | |
| //Вспомогательная константа класса PDO::ERR_NONE == 00000 (00000 = все ок, ошибок нет) | |
| $info = $query->errorInfo(); | |
| if ($info[0] != PDO::ERR_NONE) { | |
| exit($info[2]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Надо полученные дату и время "2015-12-12 12:12:12" превратить в "12.12.2015 в 12:12". | |
| <?php | |
| В качестве второго аргумента функция date принимает только unix timestamp. Чтобы получить unix timestamp из строки, которую отдает MySQL, нужно использовать функцию strtotime | |
| date('d.m.Y H:i', strtotime($x[0]['time'])); | |
| Пример: | |
| echo date("d-m-Y H:i:s", strtotime("2015-12-12 12:12:12")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <span class="timer">3</span> | |
| let _Seconds = $('.timer').text(), | |
| int; | |
| int = setInterval(function() { // запускаем интервал | |
| if (_Seconds > 0) { | |
| _Seconds--; // вычитаем 1 | |
| $('.timer').text(_Seconds); // выводим получившееся значение в блок | |
| } else { | |
| clearInterval(int); // очищаем интервал, чтобы он не продолжал работу при _Seconds = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Цикл в WordPress выглядит так: | |
| <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> | |
| <!-- Цикл WordPress --> | |
| <p>Выводим данные записи. Здесь работают функции для цикла, например, the_title() </p> | |
| <h2><?php the_title() ?></h2> | |
| <?php endwhile; else : ?> | |
| <p>Записей нет.</p> | |
| <?php endif; ?> | |
| Или можно записать так: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Создание | |
| function create_custom_table() | |
| { | |
| global $wpdb; | |
| require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | |
| $table_name = $wpdb->prefix . 'test_table'; | |
| $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset} COLLATE {$wpdb->collate}"; | |
| $sql = "CREATE TABLE {$table_name} ( | |
| id bigint(20) unsigned NOT NULL auto_increment, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Получим текущее время и разобьем его на составляющие (дни, часы, минуты): | |
| <?php | |
| $blogtime = current_time('mysql'); // вернет: 2005-08-05 10:41:13 | |
| list( $year, $month, $day, $hour, $minute, $second ) = preg_split( '([^0-9])', $blogtime ); | |
| // Теперь у нас переменные: | |
| // $year - текущий год | |
| // $month - текущий месяц | |
| // т.д. |