RSS

Custom helper in Codeigniter


Hello Readers,

I hope you all are doing well!

This blog is about to create custom helper in Codeigniter(CI).
Before we start, we need to understand the definition of Helper and Library in CI.

Helper: Helper contains setup of functions which you can directly access in Model, View, Controllers without creating an object/instance.
E.g URL, Text, Cookie etc. are helpers. More: https://codeigniter.com/user_guide/general/helpers.html

Library: Library is a class and we need to create an object before using it.
E.g. Session, Email etc. are libraries.  More: https://codeigniter.com/user_guide/general/libraries.html

How to create custom helper in CI?
  • Create PHP file in %Project Directory%/application/helpers/  (You can also create helper file in system directory but it’s not the right way.)
  • You can give an identical name to that file. E.g. auth_helper.php (You have to add _helper as a suffix.)
  • Add following code to that file
  • <?php
    defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

    function isLogin() {

    $CI = & get_instance();
    if ($CI->session->userdata(‘admin_id’)) {
    return true;
    }
    return false;
    }
  • If you want to access CI library/model in custom helper then you have to access via instance.
    E.g. &get_instance();
  • In above example, I created login check function. This will check whether user is logged in or not.
How to use in CI controllers, view, model etc.?
  • First, you need to load helper. You can load in multiple ways.
    • Autoload: Please add helper’s name in autoload.php in the config directory of the project.
      Please review this link http://prntscr.com/irhgro
    • Manually : You can add helper using $this->load->helper(‘name_of_helper’);
  • You can now simply access all the functions of helpers. Just need to write down the name of a function.
    E.g.
    if (isLogin()) {
    echo “Logged in”;
    }else{
    echo “Not Log in”;
    }
  • In above exmaple, isLogin() is the name of function from auth helper.
  • You can add function anywhere you want to use.

I hope you understand this tutorial. If you have any questions or confusion then please comment it.

Advertisements
 
Leave a comment

Posted by on March 15, 2018 in Codeigniter, PHP

 

Tags: , , , ,

Magento 2 Remove Phone Number field from checkout page


In Magento 2.2 admin panel, there is a facility to disable Phone number field. But what about in Magento 2.0?

You can do via code.

Please follow the steps.

  1. Please take a back up of  checkout_index_index.xml (vendor/magento/module-checkout/view/frontend/layout/)
  2. Copy checkout_index_index.xml to app/design/frontend/<vendor-name>/<theme-name>/Magento_Checkout/layout/
  3. Add/replace following code in checkout_index_index.xml

<item name=”telephone” xsi:type=”array”>
         <item name=”visible” xsi:type=”boolean”>false</item>
         <item name=”config” xsi:type=”array”>
                   <item name=”validation” xsi:type=”array”>
                            <item name=”componentDisabled” xsi:type=”boolean”>true</item>
                            <item name=”required-entry” xsi:type=”string”>false</item>
                   </item>
         </item>
</item>

Save this file and clear Magento cache.

You can also disable other fields from checkout page with same steps.

I hope you understand this tutorial. If you have any questions or confusion then please comment it.

 

 

 

 
Leave a comment

Posted by on February 22, 2018 in E-commerce, Magento 2, PHP

 

Tags: , ,

Auto save form using PHP, jQuery


This blog is regarding how to auto save form using PHP, jQuery. There are many tutorial available but it is very simple method.

HTML Form :

<form id=”auto-save-form”>
Name : <input name=”name” type=”text” />
Email : <input name=”email” type=”text”>
<input name=”submit” type=”submit”/>
</form>

JQuery Code:

/*Form Auto Submit Logic*/
var changeFlag = false;
var focusFlag = false;
jQuery(“#auto-save-form”).change(function () {
changeFlag = true;
focusFlag = false;
});

setInterval(function () {
/*
* changeFlag for change any element of current form
* focusFlag for tell us that focus out or in
*/
if (changeFlag == true && focusFlag == false) {
jQuery(“#auto-save-form”).trigger(“submit”);
changeFlag = false;
}

}, 10000);

jQuery(“#auto-save-form input”).focus(function () {
focusFlag = true;
});
jQuery(“#auto-save-form input”).focusout(function () {
focusFlag = false;
});

/*Form Auto Submit Logic End*/
$(“#auto-save-form”).submit(function() {
//Ajax Logic
alert(“submitted”);
});

Download Demo

I hope you understand this tutorial. If you have any questions or confusion then please comment it..

 
3 Comments

Posted by on November 29, 2017 in AJAX, Javascript & Jquery, PHP

 

Tags: , , ,

PHP v/s .net


This blog I write today that about to PHP vs .NET. I’m ex. Microsoft Student Partner and Microsoft Certified Developers even if I’m oppose to .NET. Why? Find my answers below.

PHP is a free open source. So, we should not need to pay any fees for PHP. PHP is runs on various operating system. Such like Windows, Linux, Mac etc. PHP is also run on many servers like Apache, IIS, *gix etc. Windows, Mac are not free. So, you should always use with Linux, Apache.

Many MNC companies are used Java, PHP. Because no need to pay for open source. For, open source there are many open community, projects available. Now a day many websites build in PHP. Facebook is also build with PHP, MySQL.
There are many software packages available for PHP on various operating system. There are many versions for PHP. But every new version, no need to upgrade your software such like OS, editor etc. You should have to upgrade only PHP, Servers and Database and its cost-free. But in .NET every year Microsoft upgrade the software version such like OS, visual studio, .NET framework etc… For e.g. Before 3 years ago. I was developed windows 8 apps using visual studio 2012 with win 8. But if you want to develop win 10 app then you must have to install visual studio 2015 with windows 10 OS. So, you have to pay fees for OS, visual studio etc. You have to invest every time while any upgrades available for .NET technologies. In, PHP there are many open community available for free help as well as forums. You should have modified the code as per your risk. But you never changed the code in .NET technologies because of close source.

There are many free editors available for PHP, MySQL. Like NetBeans and MySQL WorkBench.

PHP is an interpreter. So, you don’t need to compile unlike .NET technologies. Nowadays php supports pure OOPS. In.NET technologies, you want high configuration system. But in PHP no need of any high configuration system in Linux only.

So, as per my view and experience, PHP is a better than .NET in terms of Costs, Scalability and Performance.

 
 

Desktop v/s Web Application


Hello Folks! I hope you are all enjoying my blog. Today, I am sharing views on Desktop v/s Web Application. There are many advantages and disadvantages of both the applications. Both the applications are good and have their own great characteristics.

What is Desktop Application?

A Desktop application is an application that runs on various computer and you can access the same from where it is installed. The only drawback of such an application is that you can access it from a specific location only, which doesn’t allow you to use the same from any other location. In order to be able to use it on any other machine you need to install the same on other machine. Even with this drawback, Desktop Application is an amazing solution to opt for because it is a trustworthy and reliable solution. You can overcome this drawback by installing the application on a portable device such as laptop so that your access to the system is not constraint by the physical boundaries.

What is Web Application?

Unlike Desktop Application, Web application runs on various web browsers and various machines. You can access the application anywhere, anytime and on any device. Hence, you can say that it definitely overcome the drawback of Desktop Application and that the same is not constraint of physical boundaries or constraint of devices.

Let’s go to the core and find true dissimilarities between Desktop & Web application. This helps us understand the basic characteristics of both the Desktop & Web application respectively.

  1. Installation 

    You need to install the desktop application which you need to access, along with application drivers on a particular machine which may sometime depend on the operating as well. Nowadays, most people are using smart phones and crave to get each application on the phone as well, in such a case we need to create a different application which can be sued on phone and hence, you can say that it is not cost-effective.When it comes to web development all you need to do is install a web browser which enables you to access the same from anywhere & at any time. Today, you may find numerous web applications made with numerous technologies which all operates smoothly on any machine such as mobiles or tablets.

  2. Platform independent 

    Desktop applications are platform dependent which proves to be one of its major drawback.
    Web applications however are not platform dependent and hence they overcome the drawback of Desktop Application.

  3. Deploying 

    We need to deploy desktop applications in each individual machines. Whereas, you only need to deploy web application to the server and not in individual machines.

  4.  Piracy 

    It doesn’t matter if your desktop application is paid or licensed. It always has great chances of piracy.
    Whereas, in web application there is a threat of piracy and all your script’s remains secure.

  5. Accessibility 

    Desktop application can’t be accessed from anywhere or from any devices whereas Web applications can be accessed anywhere, anytime and from any devices.

  6. Security

    Web applications are exposed to more security risks than Desktop applications.

  7. Cost factor

    In most cases Desktop applications are purchased once and then there are no recurring charges associated with it. However, when it comes to Web applications, their development and maintenance may cost more depending on the requirements and may need changes from time to time.

  8. Ease of use 

    Desktop applications are confined to a physical location and hence have usability constraint. Web applications on the other hand makes it convenient for the users to access the application from any location using the Internet.

This is a huge subject matter and hence it is not possible to pen down each point. As a result of which I have shared a few points which I find important. You may understand that both the Desktop and Web applications are good options and have their own pro’s and con’s. In the end Web applications are easy to use and Desktop applications are Secure. You can select the either ones based on your precise requirement.

 
8 Comments

Posted by on December 24, 2016 in Computers and Internet, difference

 

Tags: , ,

printing webpage without opening new window using jQuery


Print is a create a big problem to mostly developers (I’m one of them). The problem like “print without opening new window”.
Today, in this post I write a solutions of same problem.

The solutions is :

Use Jquery/Javascript with iframe elements. Small code for solution is below.

var theUrl="print.html"; 
$("<iframe>")                            // create a new iframe element
        .hide()                               // make it invisible
        .attr("src", theUrl) // point the iframe to the page you want to print
        .appendTo("body"); 
and Use window.print() method in the print file which you want to print.

If you have any confusion then comment it.

 
Leave a comment

Posted by on January 21, 2016 in Javascript & Jquery, PHP

 

Tags: , , , ,

 
%d bloggers like this: