How to Delete Array Element by Key in PHP
How to Delete Array Element by Key in PHP

In this short article, we’ll look at a couple of examples of removing array elements by their key.

Full code listing below:

$array = [
	1 => 'One',
	'Two' => 3,
	'Three' => 'Three',
	4 => NULL,
];
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

if(isset($array['Two']))
{
	unset($array['Two']);
}
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

if(isset($array[4]))
{
	unset($array[4]);
}
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

if(array_key_exists(4, $array))
{
	unset($array[4]);
}
echo "<pre dir='ltr'>";print_r($array); echo "</pre>";

read more...

How to Save Options to Database in WordPress
How to Save Options to Database in WordPress

One of the simplest ways to store data in WP is to use the database table called “wp_options”. Typically, this table is used to store plugin or theme data that is not related to meta values (such as for posts, users, or taxonomies).

WordPress provides a set of built-in functions and filters to interact with this table.

Let’s take a look at a small working example:

$option_key = 'my_option_var';

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>get_option + default value</b>";
$result = get_option($option_key, 'default_value');
var_dump($result);
echo "<hr>";

echo "<b>add_option with string</b>";
$result = add_option($option_key, 'option_value', '', false);
var_dump($result);
echo "<hr>";

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>update_option with array</b>";
$result = update_option($option_key, array(
	1 => 'One',
	'Two' => 3,
	'Three' => 'Three',
));
var_dump($result);
echo "<hr>";

echo "<b>get_option</b>";
$result = get_option($option_key);
var_dump($result);
echo "<hr>";

echo "<b>delete_option</b>
";
$result = delete_option($option_key);
var_dump($result);
echo "<hr>";
die;

read more...

How to expand Autocomplete in jQuery by clicking on a button
How to expand Autocomplete in jQuery by clicking on a button

The jQuery framework includes a component called Autocomplete, which displays suggestions (input options) directly below the text input field as it’s being typed. Similar to how autocomplete works in search engines like Google or Yandex.

In one of the projects, there was a task to display the full list of available options when clicking a button.

Let’s take a closer look at how to implement this. The first thing we need to do is include all the necessary scripts and libraries:

<link rel="stylesheet" href="../bower_components/jquery-ui/themes/base/all.css">
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/jquery-ui/jquery-ui.min.js"></script>

read more...

How to disable Bootstrap in Yii2
How to disable Bootstrap in Yii2

By default, Yii2 comes bundled with the Bootstrap framework. After installing any configuration (basic or advanced), you’ll see a start page styled with Bootstrap. In this case (and possibly in all others), there’s nothing wrong with that, since it's just a placeholder, not a production website. But what if the client or studio decides to move away from such a popular solution like Bootstrap and opts for something like MaterializeCSS instead? In that case, you’ll need to disable Bootstrap and connect the MaterializeCSS framework (or any other you prefer).

The first thing we need to do is get rid of the built-in Bootstrap setup provided by Yii developers.

The second step is to disable Bootstrap support in the asset file (in basic, it’s located at /assets/AppAsset.php):

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yiiwebYiiAsset',
        'yiibootstrapBootstrapAsset',
    ];
}

Remove the line:

'yiibootstrapBootstrapAsset',

read more...

How to establish a connection to a database in the Yii2 framework
How to establish a connection to a database in the Yii2 framework

A simple and trivial task that a beginner Yii2 developer may encounter. Let's start with the fact that depending on the project configuration, the database can be set up in different ways. Let’s consider the two options provided by the framework:

  1. basic (simple setup)
  2. advanced (more complex setup)

In both cases, the database configuration (setting the prefix/tablePrefix, username/username, password/password, dsn, etc.) is done the same way. It all depends on the file location and the database connection class. In this example, we’ll look at how to connect to a MySQL database, as it's one of the most popular options.

In the basic template, the database configuration file can be found at: “/config/db.php”.

In the advanced template, things are a bit more complex (hence the name — for large-scale projects). There may be more than one configuration file (for backend, frontend, etc.). It all depends on how you initially structured your application. Since both admin and frontend parts usually work with the same database, I typically place the database configuration in the “/common/config/db.php” directory. Then I reference it in the “/common/config/main.php” config. That way, both backend and frontend will automatically inherit the database settings.

read more...