Wednesday, 6 October 2021

Arrays filtering in MERN Mongo , express, react and node

let array1=[1,2,3];
let array2=[1,2,4,5,6,7,7,4,3,2,1,4,5,5,2,3,3,1,2,9,8,6,4,3,2,7,1] 

remove array1 element from array2,expected out=[4,5,6,7,7,4,4,5,5,9,8,6,4,7]

 

Solution : 

let array1=[1,2,3];
let array2=[1,2,4,5,6,7,7,4,3,2,1,4,5,5,2,3,3,1,2,9,8,6,4,3,2,7,1];


let output = array2.filter((item)=> !array1.includes(item))

console.log(output);

Thursday, 30 September 2021

Unable to install or uninstall any applications in Windows 10 due to wrong environment variable settings

 Recently I got a peculiar problem, my machine working perfectly fine and I am installing the necessary apps one after another as per the requirements..

I have my Java, Netbeans, Sql developer , Oracle , XAMP studio,  Visual studio and sql server database. Everything is fine until one day.. 

On the day when I started my machine I found a weird behavior, MS word not working, CCleaner not opening, unable to run the applications and unable to uninstall any of the applications

After some troubleshooting, found that the TEMP variable (Environment variables) is assigned wrong, In previous versions whenever environment variables is to be set, It became practice to set the both PATH and TEMP variables with the same paths.. especially for JAVA, PHP, MYSQL etc., based on this practice the variables were set for the applications.. but in one the post found that the command %temp% should work in RUN command. (i.e., press   WindowsKey + R - for run dialog box, and type the command as %temp%) 

In my system, the command throwing error, because of environment variable TEMP is given path for JAVA,PHP and MYSQL . So I removed the paths of these application and made the variable as it is like the previous path. 

The error rectified and able to install the applications, please make a note to set the environment variable PATH only for the applications.  


Friday, 17 September 2021

Installing XAMP and Creating YII application using PHP , setting database connection and GII control usage

Download xamp software from the website and install in machine by following steps

XAMP software link : https://www.apachefriends.org/download.html

During the installation process, select the required components like MySQL, FileZilla ftp server, PHP, phpMyAdmin or leave the default options and click the Next button. 

  • Uncheck the Learn more about bitnami option and click Next button.
  • Choose the root directory path to set up the htdocs folder for our applications. For example ‘C:\xampp’. I have chosen 'C:\xampp' as my xamp root folder. The installed path of the xamp is taken as root directory (say xamp root directory/folder ) 
  • Click the Allow access button to allow the XAMPP modules from the Windows firewall.
  • After the installation process, click the Finish button of the XAMPP Setup wizard.
  • Now the XAMPP icon is clearly visible on the right side of start menu. Show or Hide can be set by using the control panel by clicking on the icon.
  • To start Apache and MySql, just click on the Start button on the control panel. 
     

control-panel-xampp

Note: Suppose Apache is not starting, it means some other service is running at port 80. In this case, stop the other service temporarily and restart it.


Making server request: Open your web browser and check whether the XAMPP service has properly installed or not. 

Type in the URL: http://localhost. If you are able to see the default page for XAMPP, you have successfully installed your XAMPP Server.


To Check if PHP is Working: All the website related files are organized in a folder called htdocs and then run index.php file by using 

http://localhost/index.php or http://localhost.


Note: For every new website or application, its always better to create a different folder inside htdocs, to keep it organized and avoid confusion.
For example, if we create a folder helloworldapp and then create a file named as ‘helloWorld.php’. All the contents related to it are put inside the folder 
helloworldapp’.

So the root ‘URL’ of the website will be ‘http://localhost/helloworldapp/’. So any home page is accessed by typing the root URL in the browser. To see the output, just type ‘http://localhost/helloworldapp/helloWorld.php’.


Generally web servers look for index file (landing page) namely index.html or index.php in the root of the website folder. Go to /xampp/htdocs/ folder and create a file with .php extension (test.php) and type or copy the below code and save it.
 

<?php
phpinfo();
?>

Now open your browser and go to “http://localhost/test.php” if you see the 
page same as below then PHP has successfully installed. 
 

test-screen-phpinfo

Note: In XAMPP, the configuration files of Apache, MySQL, PHP are located in xampp root folder. For any configuration file changes, you need to restart Apache and MySQL


Path Setting : 


Now PHP path to be set in windows environment variables so that the installed mysql/php or other selected services can be accessed through command prompt.

Right click on MyComputer (This Computer) and choose Properties --> Advanced System settings --> Environment variables

 (or)

Start by typing in Environment Variables in your Search and click on the Edit the system environment variables result.

In the System Properties window click Environment Variables. Make sure that you’re on the Advanced tab.

Scroll down to the Path variable and click Edit.



Click on the Browse button.

Navigate to C:/xampp/php and click OK.

You will now see the path to php inside the window. Click OK to exit.

Restart your Command Prompt window if it’s open and type in:

php -v

You should see the PHP version being displayed.

You can now use php from CMD (Command Prompt)

YII Installation 
Download YII from the https://www.yiiframework.com website
I have used YII framework version 1.1.14 
download the zip folder and extract the contents (Here I renamed the folder name as yii instead of yii-1.1.14) and copy the folder to the XAMP_Root_Directory/htdocs folder.

Now to create the application in YII run the following command

C:\xampp\htdocs\yii\framework>yiic webapp c:\xampp\htdocs\myFirstApp

[Note: My xamp root location is c:\xampp and I have copied the YII folder in htdocs, if you have done the same the above statement executes as it is, otherwise you have change the paths and folder names accordingly].

Now the above statement asks the following query type 'yes' like me :)

Create a Web application under 'C:\xampp\htdocs\myFirstApp'? (yes|no) [no]:yes

A flow statements executes , which means YII creating the application with the folder name as "myFirstApp" in the specified location, i.e., htdocs

Now open the link :http://localhost/myFirstApp in your favorite browser, make sure that the Apache is running in your xamp control panel (my default listening port is 80 so I haven't specified the port number in URL if Apache running on different port then specify the port number in the URL as http://localhost:port_number/application_name )


The application will be opened as follows : 


The default credentials to login are  defined in C:\xampp\htdocs\myFirstApp\protected\components folder UserIdentity.php file.. the default credentials are admin => admin (or) demo => demo (obviously admin is more privileged)

To give database connection, check the file 

C:\xampp\htdocs\myFirstApp\protected\config\main.php and modify the connection parameters accordingly i.e., edit the following part


Uncomment the above and specify the connection strings accordingly( If your database is mysql make sure the mysql running in the XAMP control panel, and also path settings done for mysql before procedding)



Now after connection open the "gii" to create models, controllers and views . To set the gii password in the same C:\xampp\htdocs\myFirstApp\protected\config\main.php file edit the following part, If it is commented, uncomment the part.. 


Here I have given the password as application name i.e.,
myFirstApp , once this is done open the following link in your browser

http://localhost/myFirstApp?r=gii

The following screen will be displayed, now provide the password i.e., myFirstApp


and click Enter button


Now you can generate the model/crud/controller/form/model or module whatever you want, but make sure the DB Connection is proper before performing this operation.

Happy Coding!!!






.

Thursday, 16 September 2021

Create YII Application using PHP and in lamp or xamp

 

Creating Your First Yii Application

To give you an initial experience with Yii, in this section we describe how to create your first Yii application. We will use yiic (command line tool) to create a new Yii application and Gii (powerful web based code generator) to automate code creation for certain tasks. For convenience, we assume that YiiRoot is the directory where Yii is installed, and WebRoot is the document root of our Web server.

Run yiic on the command line as follows:

% YiiRoot/framework/yiic webapp WebRoot/testdrive

Note: When running yiic on Mac OS, Linux or Unix, you may need to change the permission of the yiic file so that it is executable. Alternatively, you may run the tool as follows,

% cd WebRoot
% php YiiRoot/framework/yiic.php webapp testdrive

This will create a skeleton Yii application under the directory WebRoot/testdrive. The application has a directory structure that is needed by most Yii applications.

Without writing a single line of code, we can test drive our first Yii application by accessing the following URL in a Web browser:

http://hostname/testdrive/index.php

As we can see, the application has four pages: the homepage, the about page, the contact page and the login page. The contact page displays a contact form that users can fill in to submit their inquiries to the webmaster, and the login page allows users to be authenticated before accessing privileged contents. See the following screenshots for more details.

Home page

Home page

Contact page

Contact page

Contact page with input errors

Contact page with input errors

Contact page with success message

Contact page with success message

Login page

Login page

The following diagram shows the directory structure of our application. Please see Conventions for a detailed explanation.

testdrive/
   index.php                 Web application entry script file
   index-test.php            entry script file for the functional tests
   assets/                   containing published resource files
   css/                      containing CSS files
   images/                   containing image files
   themes/                   containing application themes
   protected/                containing protected application files
      yiic                   yiic command line script for Unix/Linux
      yiic.bat               yiic command line script for Windows
      yiic.php               yiic command line PHP script
      commands/              containing customized 'yiic' commands
         shell/              containing customized 'yiic shell' commands
      components/            containing reusable user components
         Controller.php      the base class for all controller classes
         UserIdentity.php    the 'UserIdentity' class used for authentication
      config/                containing configuration files
         console.php         the console application configuration
         main.php            the Web application configuration
         test.php            the configuration for the functional tests
      controllers/           containing controller class files
         SiteController.php  the default controller class
      data/                  containing the sample database
         schema.mysql.sql    the DB schema for the sample MySQL database
         schema.sqlite.sql   the DB schema for the sample SQLite database
         testdrive.db        the sample SQLite database file
      extensions/            containing third-party extensions
      messages/              containing translated messages
      models/                containing model class files
         LoginForm.php       the form model for 'login' action
         ContactForm.php     the form model for 'contact' action
      runtime/               containing temporarily generated files
      tests/                 containing test scripts
      views/                 containing controller view and layout files
         layouts/            containing layout view files
            main.php         the base layout shared by all pages
            column1.php      the layout for pages using a single column
            column2.php      the layout for pages using two columns
         site/               containing view files for the 'site' controller
            pages/           containing "static" pages
               about.php     the view for the "about" page
            contact.php      the view for 'contact' action
            error.php        the view for 'error' action (displaying external errors)
            index.php        the view for 'index' action
            login.php        the view for 'login' action

Application generator described above also supports creation of files needed by Git version control system. The following command would create necessary .gitignore (e.g. content of the assets and runtime shouldn't be tracked) and .gitkeep (forces tracking of initially empty but important directories) files:

% YiiRoot/framework/yiic webapp WebRoot/testdrive git

Another supported VCS is Mercurial: pass the hg value as third parameter in case you're using this VCS. This feature is available since version 1.1.11.

1. Connecting to Database

Most Web applications are backed by databases. Our test-drive application is not an exception. To use a database, we need to tell the application how to connect to it. This is done in the application configuration file WebRoot/testdrive/protected/config/main.php, highlighted as follows,

return array(
    ......
    'components'=>array(
        ......
        'db'=>array(
            'connectionString'=>'sqlite:protected/data/testdrive.db',
        ),
    ),
    ......
);

The above code instructs Yii that the application should connect to the SQLite database WebRoot/testdrive/protected/data/testdrive.db when needed. Note that the SQLite database is already included in the skeleton application that we just generated. The database contains only a single table named tbl_user:

CREATE TABLE tbl_user (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    username VARCHAR(128) NOT NULL,
    password VARCHAR(128) NOT NULL,
    email VARCHAR(128) NOT NULL
);

If you want to try a MySQL database instead, you may use the included MySQL schema file WebRoot/testdrive/protected/data/schema.mysql.sql to create the database.

Note: To use Yii's database feature, we need to enable the PHP PDO extension and the driver-specific PDO extension. For the test-drive application, we need to turn on both the php_pdo and php_pdo_sqlite extensions.

2. Implementing CRUD Operations

Now comes the fun part. We would like to implement CRUD (create, read, update and delete) operations for the tbl_user table we just created. This is also commonly needed in practical applications. Instead of taking the trouble to write the actual code, we will use Gii -- a powerful Web-based code generator.

Info: Gii has been available since version 1.1.2. Before that, we could use the aforementioned yiic tool to accomplish the same goal. For more details, please refer to Implementing CRUD Operations with yiic shell.

Configuring Gii

In order to use Gii, we first need to edit the file WebRoot/testdrive/protected/config/main.php, which is known as the application configuration file:

return array(
    ......
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),
 
    'modules'=>array(
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'pick up a password here',
        ),
    ),
);

Then, visit the URL http://hostname/testdrive/index.php?r=gii. We will be prompted for a password, which should be the one that we just entered in the above application configuration.

Generating the User Model

After login, click on the link Model Generator. This will bring us to the following model generation page,

Model Generator

Model Generator

In the Table Name field, enter tbl_user. In the Model Class field, enter User. Then press the Preview button. This will show us the new code file to be generated. Now press the Generate button. A new file named User.php will be generated under protected/models. As we will describe later in this guide, this User model class allows us to talk to the underlying database tbl_user table in an object-oriented fashion.

Generating CRUD Code

After creating the model class file, we will generate the code that implements the CRUD operations about the user data. We choose the Crud Generator in Gii, shown as follows,

CRUD Generator

CRUD Generator

In the Model Class field, enter User. In the Controller ID field, enter user (in lower case). Now press the Preview button followed by the Generate button. We are done with the CRUD code generation.

Accessing CRUD Pages

Let's enjoy our work by browsing the following URL:

http://hostname/testdrive/index.php?r=user

This will display a list of user entries in the tbl_user table.

Click the Create User button on the page. We will be brought to the login page if we have not logged in before. After logging in, we see an input form that allows us to add a new user entry. Complete the form and click the Create button. If there is any input error, a nice error prompt will show up which prevents us from saving the input. Back on the user list page, we should see the newly added user appearing in the list.

Repeat the above steps to add more users. Notice that the user list page will automatically paginate the user entries if there are too many to be displayed in one page.

If we login as an administrator using admin/admin, we can view the user admin page with the following URL:

http://hostname/testdrive/index.php?r=user/admin

This will show us the user entries in a nice tabular format. We can click on the table header cells to sort the corresponding columns. We can click on the buttons on each row of data to view, update or delete the corresponding row of data. We can browse different pages. We can also filter and search to look for the data we are interested in.

All these nice features come without requiring us to write a single line of code!

User admin page

User admin page

Create new user page

Create new user page

Monday, 19 July 2021

Reset IsIdentity of table to particular Value in SQL SERVER

 Syntax for setting to particular value is

 

DBCC CHECKIDENT ('table_name', RESEED, 100)  

-- Supply table name, here 100 is identity seed value

To Know the current seed value of the table use the below query


 DBCC CHECKIDENT ('table_name') -- Displays the current seed value