Thursday, 24 November 2022

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid


Tip #1)

Children do not learn things, they emitate. So, try to do things by yourself, your child will follow you.
Let us see how can we lure a kid into learning Mathematics.

Take a paper and pen or pencil.
Do some simple mathematical problem.
After solving the problem, show happiness on your face.
This way kid will think that this thing is joyful and will follow you.

Saturday, 20 August 2022

Write Smart code in PHP arrays using in built functions.

We have a requirement to fetch data from database and display user names and their salaries.

Output required:

Morgan's salary: 6000, Jermy's salary: 5500, Phil's salary: 5500, Anthony's salary: 5500, John's salary: 5500, Norman's salary: 5500

There are 2 ways of doing this that generate the same result.

<?php

$databseArray = [];

$databseArray[] = [1, 'Morgan', 6000];

$databseArray[] = [2, 'Jermy', 5500];

$databseArray[] = [3, 'Phil', 5500];

$databseArray[] = [4, 'Anthony', 5500];

$databseArray[] = [5, 'John', 5500];

$databseArray[] = [6, 'Norman', 5500];


echo 'Approach 1';

echo '<br/>';

if (! empty($databseArray)) {

 $str = '';

 $cntr = 0;

 $len = count($databseArray);

 foreach ($databseArray as $databseRow) {

  $str .= ' '.$databseRow[1] ."'s". ' salary: '.$databseRow[2];

  if ($cntr < ($len-1))

  $str .= ', ';

  ++$cntr;

 }

}

echo $str;

echo '<br/>--------------------------------------------------------------<br/>';

echo 'Approach 2';

echo '<br/>';

$outputArr = [];

if (! empty($databseArray)) {

 foreach ($databseArray as $databseRow) {

  $outputArr[] = ' '.$databseRow[1] ."'s". ' salary: '.$databseRow[2];

 }

}

echo implode(', ', $outputArr);

Friday, 19 August 2022

Design Pattern in PHP

<?php

class Booklibrary {

 private $bookName = '';

 private $bookAuthor = '';

 const BR = '<br/>';

 public function __construct($name = '', $author = '') {

  $this->bookName = $name;

  $this->bookAuthor = $author;

 }

 public function getNameAndAuthor() {

  return $this->bookName . ' - ' . $this->bookAuthor . self::BR;

 }

}

class BooklibraryFactory {

 public static function create($name, $author) {

  return new Booklibrary($name, $author);

 }

}

$bookOne = BooklibraryFactory::create('Shiv Khera', 'You can win');

$bookTwo = BooklibraryFactory::create('Norman Vincent Peale', 'The Power Of Positive Thinking.');


echo $bookOne->getNameAndAuthor();

echo $bookTwo->getNameAndAuthor();

Monday, 15 August 2022

ReactJS useState() method explained

 The ReactJS useState() method is used to save and set state variables in the React Components.

For this to use, we need to import useState() from React.

The useState() method returns an array.

It has two parameters:

1) state variable

2) function to change state variable.


For example:


const [count, setCount] = useState(0);

Here we are destructing the useState() array with two variables.

the count is variable and setCount is a method.

// Import useState from React

import {useState} from 'react';


function App() {

 const [count, setCount] = useState(0);

}


We can set the count using setCount() method.

Tuesday, 5 July 2022

My First Component to Toggle Color and Color Name

 import { Fragment, useState, useReducer } from 'react';

function reducer(state, action) {
  console.log(state);
  console.log(action);
  switch (action.type) {
    case 'Green':
      return action.type;
      break;
    case 'Red':
      return action.type;
      break;
    case 'Yellow':
      return action.type;
      break;
    case 'Pink':
      return action.type;
      break;
  }
  return state;
}
function Toggle() {
  //const [state, setState] = useState('pink');
  const [state, dispatch] = useReducer(reducer, 'Pink');
  return (
    <Fragment>
      The color is{' '}
      <span style={{ backgroundColor: state, padding: '5px' }}> {state} </span>
      <button
        onClick={() => {
          dispatch({ type: 'Green' });
        }}
      >
        Green
      </button>
      <button
        onClick={() => {
          dispatch({ type: 'Red' });
        }}
      >
        Red
      </button>
      <button
        onClick={() => {
          dispatch({ type: 'Yellow' });
        }}
      >
        Yellow
      </button>
      <button
        onClick={() => {
          dispatch({ type: 'Pink' });
        }}
      >
        Pink
      </button>
    </Fragment>
  );
}
export default Toggle;






Tuesday, 17 May 2022

Drupal 8 programmatically get list of themes installed

 To get list of themes on a Drupal site programmatically, please use following code:


  $modulesList = \Drupal::service('extension.list.theme')->reset()->getList();
  if (! empty($modulesList)) {
    echo "<strong>Themes List:</strong>";
    echo "<br/>";
    foreach ($modulesList as $moduleName => $moduleObj) {
      echo "<br/>";
      echo '<strong>Machine Name: </strong>' . $moduleName;
      echo "<br/>";
      echo '<strong>Humand Readable Name: </strong>' . $moduleObj->info['name'];
      echo "<br/>";
      echo '<strong>Description: </strong>' . $moduleObj->info['description'];
      echo "<br/>";
      echo '<strong>Package: </strong>' . $moduleObj->info['package'];
      echo "<br/>";
      echo "-----------------------------------------------------------------";
    }
  }


Output:


Themes List:

Machine Name: bartik
Humand Readable Name: Bartik
Description: A flexible, recolorable theme with many regions and a responsive, mobile-first layout.
Package: Core
-----------------------------------------------------------------
Machine Name: claro
Humand Readable Name: Claro
Description: A clean, accessible, and powerful Drupal administration theme.
Package: Core
-----------------------------------------------------------------
Machine Name: classy
Humand Readable Name: Classy
Description: A base theme with sensible default CSS classes added. Learn how to use Classy as a base theme in the Drupal 8 Theming Guide.
Package: Core
-----------------------------------------------------------------
Machine Name: seven
Humand Readable Name: Seven
Description: The default administration theme for Drupal 8 was designed with clean lines, simple blocks, and sans-serif font to emphasize the tools and tasks at hand.
Package: Core
-----------------------------------------------------------------
Machine Name: stable
Humand Readable Name: Stable
Description: A default base theme using Drupal 8.0.0's core markup and CSS.
Package: Core
-----------------------------------------------------------------

Drupal 8 get List of modules programmatically.

To get list of modules programmatically, please use following code:


  $modulesList = \Drupal::service('extension.list.module')->reset()->getList();
  if (! empty($modulesList)) {
    echo "<strong>Modules List:</strong>";
    echo "<br/>";
    foreach ($modulesList as $moduleName => $moduleObj) {
      echo "<br/>";
      echo '<strong>Machine Name: </strong>' . $moduleName;
      echo "<br/>";
      echo '<strong>Humand Readable Name: </strong>' . $moduleObj->info['name'];
      echo "<br/>";
      echo '<strong>Description: </strong>' . $moduleObj->info['description'];
      echo "<br/>";
      echo '<strong>Package: </strong>' . $moduleObj->info['package'];
      echo "<br/>";
      echo "-----------------------------------------------------------------";
    }
  }

Output:

Modules List:

Machine Name: action
Humand Readable Name: Actions
Description: Perform tasks on specific events triggered within the system.
Package: Core
-----------------------------------------------------------------
Machine Name: aggregator
Humand Readable Name: Aggregator
Description: Aggregates syndicated content (RSS, RDF, and Atom feeds) from external sources.
Package: Core
-----------------------------------------------------------------
Machine Name: automated_cron
Humand Readable Name: Automated Cron
Description: Provides an automated way to run cron jobs, by executing them at the end of a server response.
Package: Core
-----------------------------------------------------------------
Machine Name: ban
Humand Readable Name: Ban
Description: Enables banning of IP addresses.
Package: Core
-----------------------------------------------------------------
Machine Name: basic_auth
Humand Readable Name: HTTP Basic Authentication
Description: Provides the HTTP Basic authentication provider
Package: Web services
-----------------------------------------------------------------
......

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...