Social Icons

Friday, November 22, 2013

Add Contact Form in Blogger

Contact form can be an essential requirement to be there in the blog. Here, I am going to tell you how to add a contact form in blogger on a specific page or in sidebar.

You can add the contact form either on sidebar or on a specific page. Its preferred to add it in a specific different page. Anyway I am going to tell you for both things. Follow the below steps: 

  • Go to Layout of your blog.
  • Click on "Add Gadget" link there and find out for "Contact Form" page element. It can be either there or in "More Gadgets". If you can to edit the title from "Contact Form" to something else, then you can do that too by editing the text box there.
  • Now, save the arrangement and you can see a contact form in your blog.
But what if you want the contact form (you created above) in a specific page. For this, follow the below steps: 
  • Let it be same as you added a gadget of  "Contact Form" in above steps.
  • Now, you would like to remove this gadget from the sidebar as you want it in a specific page. So, hide it first. Go to "Template" and click on "Edit HTML" button. Then paste the below code just above ]]></b:skin>

    #ContactForm1
    {
    display:none ! important;
    }
     


    Here, what we are doing is that hiding the contact form from sidebar with help of css.
  • Now, we have to add the contact form in a specific page. Go to pages, click on "New Page" with blank page template.
  • Switch to html mode,.
  • Paste the below html code there:
    <form name="contact-form">
    <p></p>
    Name: <br />
    <input class="contact-form-name" id="ContactForm1_contact-form-name" name="name" size="30" value="" type="text" />
    <p></p>Email: 
    <span style="font-weight: bolder;color:red;">*</span><br />
    <input class="contact-form-email" id="ContactForm1_contact-form-email" name="email" size="30" value="" type="text" />
    <p></p>
    Message: <span style="font-weight: bolder;color:red;">*</span><br />
    <textarea class="contact-form-email-message"  id="ContactForm1_contact-form-email-message" name="email-message" cols="25" rows="5"></textarea>
    <p></p>
    <input class="contact-form-button contact-form-button-submit" id="ContactForm1_contact-form-submit" value="Send" type="button" />
    <p></p>
    <div style="text-align: center; max-width: 222px; width: 100%">
    <p class="contact-form-error-message" id="ContactForm1_contact-form-error-message"></p>
    <p class="contact-form-success-message" id="ContactForm1_contact-form-success-message"></p>
    </div>
    </form>
  • This will create a form there with the fields of Name, Email, Message and a Send button. User will enter his/her name, email address and message and click on send button which will be delivered to your email address, you registered to create the blog. Thats it.

Troubleshoot if your contact form is not working for your blog

If on doing the above steps, still your contact from doesn't work properly, then follow the below steps to troubleshoot it: 
  • Go to your "Template" page and click on "Edit HTML" button.
  • There will be a "Jump to Widget" drop down button. Click on that and it will give you a list. Select "ContactForm1" id. Accordingly, some code will be displayed there. Just find out the below code:

    <form name='contact-form'>
            <p/>
            <data:contactFormNameMsg/>
            <br/>
            <input class='contact-form-name' expr:id='data:widget.instanceId + &quot;_contact-form-name&quot;' name='name' size='30' type='text' value=''/>
            <p/>
            <data:contactFormEmailMsg/> <span style='font-weight: bolder;'>*</span>
            <br/>
            <input class='contact-form-email' expr:id='data:widget.instanceId + &quot;_contact-form-email&quot;' name='email' size='30' type='text' value=''/>
            <p/>
            <data:contactFormMessageMsg/> <span style='font-weight: bolder;'>*</span>
            <br/>
            <textarea class='contact-form-email-message' cols='25' expr:id='data:widget.instanceId + &quot;_contact-form-email-message&quot;' name='email-message' rows='5'/>
            <p/>
            <input class='contact-form-button contact-form-button-submit' expr:id='data:widget.instanceId + &quot;_contact-form-submit&quot;' expr:value='data:contactFormSendMsg' type='button'/>
            <p/>
            <div style='text-align: center; max-width: 222px; width: 100%'>
              <p class='contact-form-error-message' expr:id='data:widget.instanceId + &quot;_contact-form-error-message&quot;'/>
              <p class='contact-form-success-message' expr:id='data:widget.instanceId + &quot;_contact-form-success-message&quot;'/>
            </div>
          </form>
  • Just remove the above code and you are done. Now check your blog again. The contact form should be working fine now.

Sunday, October 20, 2013

Call to a member function set_userdata() on a non-object: Codeigniter

Once I was using sessions in codeigniter and got the above error "Call to a member function set_userdata() on a non-object. Later I fixed it and providing the same solution for it. 

This error comes in codeigniter if you haven't loaded session library in your file or project. You can do it in two ways. You can either load the session library in the file itself or autoload session library in autoload.php

Load the session library in the file as follows: 
$this->load->library('session');

Or you can autoload session library as follows: 
$autoload['libraries'] = array('database', 'session');

This should fix your error.

In order to use the Session class you are required to set an encryption key in your config file

This error code belongs to codeigniter (PHP MVC framework). Once I upgraded my codeigniter and while using session in codeigniter, I got the error as : "In order to use the session class you are required to set an encryption key in your config file". Here, I am providing the solution for this error. I fixed it as follows:

The reason they ask for an encryption key is security. Even if you are not using encrypted sessions, you must set an encryption key in your config file which is used to aid in preventing session data manipulation. So, you have to change in config.php as:

$config['encryption_key'] = 'your_encryption_key_here';

This should fix your error.

Tuesday, October 8, 2013

Maximum execution time of 60 seconds exceeded: Magento

Once I was in the stage of installing magento on my machine and got this error: maximum execution time of 60 seconds exceeded. And then later I fixed it. I am just sharing the thing that I do and is required to fix the above error.

Open your php.ini configuration file (It will be inside php folder). Search for max_execution_time there in the file. Assign it the value of 1800. So, it will be now max_execution_time = 1800. Similarly, just below that there will be max_input_time. Assign it also the same value as 1800. Save the file and restart your wamp or xampp server whatever you are using. That should fix the error.

Wednesday, September 18, 2013

Unsupported template dependency: Upgrade your android eclipse plugin

Once I was working on creating an android application and while configuring, I got this error as "Unsupported template dependency: Upgrade your android eclipse plugin". Below it was written as :
Required version: (empty)
Installed Version: 18

Here, I got the fix for this issue.
Suppose if you selected for Blank Activity then you have to make one change as follows:
Go to sdk/tools/templates/activities/BlankActivity/template.xml
and comment out the dependency tag as follows:
<!--<dependency name="appcompat" version="v7" />-->

Thats it. You are done. Restart eclipse and try to create again. This fixed my issue.

Similarly, if you selected for MasterDetailFlow then
Go to sdk/tools/templates/activities/MasterDetailFlow/template.xml and comment the line
<!--<dependency name="support" version="v4" />
<dependency name="android-support-v4" revision="8"/>-->

Thats it. Above steps should fix your issue of unsupported template dependency.

Tuesday, August 27, 2013

Difference between two dates in php

Here, I am going to tell you the solution for if you have to find out the difference between two dates in php. Suppose, you have two dates and you want the interval difference between them in terms of year, month, day, hours, minutes and seconds then you can do it as follows:

<?php
$old_date = '2012-08-26';
$cur_date = date("Y-m-d H:i:s"); //This will give you current date and time

$old_date = new DateTime($old_date);
$cur_date = new DateTime($cur_date);

$interval = $cur_date->diff($old_date);
$year     = $interval->y; //Outputs year difference
$month  = $interval->m; //Outputs month difference
$day      = $interval->d; //Outputs days difference
?>

From the above code, you can easily get the date difference between two dates.

Wednesday, July 17, 2013

How to make a free website

A very common question, how to make a website. Here, I am going to explain this in detail.

Most of the people want to create their own website. Specially they want it for free completely means they want to create a website for completely free of cost. So, how to make a free website???. Well, a website is of two types: Static websites and dynamic websites. A static website is that which content is fixed for all users while dynamic website's content differ from user to user for example: your home page content of facebook is different from your friend.

A website can be designed in two ways: either using website builder (or something like cms) or with programming. In this part, I will explain how to create a free website without having knowledge of programming. In the later part, I will discuss to create website with programming and publish it. So, if you don't know programming and want to create a website for free, then this part is for you. Here we go.

You can create website with site builders. There are so many site builders available on internet. They are: weebly (www.weebly.com), wix (www.wix.com), google sites (sites.google.com) etc. They all have features like having a lot of templates, drag and drop features for paragraph, title, images etc. With the free edition, they may provide you lesser facilities. By upgrading your account on some payment, you will get more features there. That you can decide after checking out the features there and as per your needs. Also, you will get one click publish there which will publish your website on internet and anyone can see it.

Do registration in any of the above mentioned websites and there you will get option to create a free website. You will type name for your website like what you want to keep name of your website (for example www.prembaranwal.com) But you will get an option to have your website name as a sub-domain name only. For example, if you are creating your website through weebly, then your website will be like this www.yourwebsite.weebly.com like that. These site builders have some pattern for your website link(url) generation. If you want to make it with your domain name only (like www.yourwebsite.com) then you have to pay for it and upgrade your account. In that case, they will set up your account with your desired domain name and your website will open with www.yourwebsite.com A website always looks more professional with your own website domain name. But if you don't want to pay anything then also its ok. You can publish your website with subdomain as I mentioned above and anyone in the world can see it.

In the next tutorial, I will explain: If you know programming and have made the website in your local and want to publish on the web (internet) for free or at a minimal charge

Monday, June 24, 2013

Check mysql engine for a specific table

In MySql, there are many storage engines like MyISAM, InnoDB etc. What if you want to check the type of storage engine for a specific table as what storage engine it is using. You can check it as follows:

Run the below query:

SHOW TABLE STATUS WHERE Name='your_table_name'

The above query will give you the storage engine name with some more additional data, your table uses.

Similarly, if you want to check storage engines for all tables in your mysql database at once, then simply run this query:

SHOW TABLE STATUS

It will give you details about the storage engines for all the tables in your mysql database.

Wednesday, June 19, 2013

PHP file upload

In a website, it is a common requirement to upload a file or photo (image). Here, I am providing a solution as how can do it in PHP. It is not advisable to store the file in database directly. Instead, you should store the file in your project folder and in your database, you should store only the path of the file. So, we'll follow the same approach only.

First we design a form containing a file field. HTML code for the form containing a file field is :

<html>
<head>
<title>File Upload Form</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
File: <input type="file" name="myfile" id="myfile"/>
<br/>
<input type="submit" name="submit" id="submit" value="Upload File"/>
</form>
</body>
</html>

Above is just the html code for displaying a file field in the web page. Now, on clicking  of submit button, it will redirect you to upload.php as it is defined in form action. So, we'll write the code for upload.php now.

upload.php

<?php

if ( $_FILES['myfile']['error'] > 0)
{
  echo "Error: ".$_FILES['myfile']['error'];
}
else
{
  if (file_exists("upload/" . $_FILES["myfile"]["name"]))          //upload/file is the path where file                                                                                                                 gets stored
  {
      echo $_FILES["myfile"]["name"] . " already exists. ";
  }
  else
  {
      move_uploaded_file($_FILES["myfile"]["tmp_name"],
      "upload/" . $_FILES["myfile"]["name"]);
      echo "File Stored in: " . "upload/" . $_FILES["myfile"]["name"];
   }}
?>


Thats it. Now, your file upload script is ready. In the above code, "upload/" . $_FILES["myfile"]["name"] is the path where file gets uploaded or stored.  Make sure, you have 'upload' directory in your project folder path. Now as your file gets stored, if you want to save its path in database, then you can run a sql query in your upload.php script as its path after uploading the file. In that case, anytime you can echo the file with the help of the path.

Thursday, June 6, 2013

Find 2nd highest or nth highest salary of employee in sql

It is one of the common questions for developers and also asked in interviews frequently to find 2nd highest salary or nth highest salary of an employee from the employee table. Here, I am providing solution for both.

To find 2nd highest (or maximum) salary of employee, you can write your query like this in MySql:

Select max(salary) as salary 
From employees 
Where salary<(Select max(salary) From employees);

The above sql query will give you the 2nd highest salary of employee. Here, 'employees' is the name of the table in the database.

But, what if you have to find 3rd highest or 4th highest or nth highest salary of employee. If you will follow the above approach then you will have to write the subquery for n times. That doesn't seem good. Here, I am providing a generalized solution to find nth highest salary.  Look at the below sql query:


SELECT DISTINCT(salary) 
FROM employees 
ORDER BY salary DESC LIMIT (n-1) , 1


Here, n is the highest term for which you have to find out highest salary. The above query will give you nth highest salary.

Tuesday, June 4, 2013

Difference between single quote and double quote in PHP

In PHP, if yu have to display or echo something (say a string), then you can do it with the help of single quote and double quote both. But there is a little difference between them as :

A single quoted string doesn't have variables within it interpreted but a double quoted string does. It can be better understood with the following example:

<?php
$a = 'prem';
echo 'My name is $a';
echo "My name is $a";
?>

For the first echo statement having single quote, output will be
My name is $a

For the second echo statement having double quoted string, output will be
My name is prem

So, if you have to define a constant string then better use with single quote and if you have variable too in the string then go with double quotes.

Thursday, May 30, 2013

Installing and Setup Yii framework - PHP

Yii framework is one of the popular PHP MVC framework these days. Here, I am providing a guide to install yii framework. Just make sure your web server is capable of supporting PHP 5.1 orr higher. I assume that you have already installed web server and PHP or softwares like wamp, xampp or lamp. So, here we go.

First, download the yii framework from http://www.yiiframework.com/download/ . Unpack the downloaded file to a web accessible folder. For example, if you have xampp installed then place it inside xampp/htdocs/ , if wamp then you can place it in wamp/www/ . Now, you can check if your server satisfies all requirements for using yii and the installation was a success. I assume you have installed xampp server and put the downloaded yii folder inside xampp/htdocs/ . Now, open the url in your browser as http://localhost/yii/requirements/index.php You will see the results there. Few fields may show warnings like for PDOSQLite extension or memcache extension. But that is ok. You can proceed. Now, lets talk about creating a new application in yii.

Creating a new application in yii

We will use a command line tool 'yiic' that comes packaged with he yii framework. It is not mandatory to use this toll but it saves time and helps in creating a new yii application easily. Now, open your command terminal and change to your webroot folder i.e, till xampp/htdocs.

cd xampp/htdocs

If you have installed wamp then you have to reach to wamp/www. Here, I assumed you have xampp installed.

Now, as you put yii downloaded folder inside htdocs as xampp/htdocs/yii, run the command like this...

yii/framework/yiic webapp demo

Here, 'demo' is your project folder name.

Note: If you get the error here as : php.exe is not recognized as an internal or external command then look here for the solution: http://awesomesirjee.blogspot.in/2013/05/phpexe-is-not-recognized-as-internal-or.html

On running the above command, it will give you message as :

Create a Web application under '/xampp/htdocs/demo'? [Yes|No]
Yes

It will create you application successfully. You can check a new folder named 'demo' is there in xampp/htdocs/ . You see how easily you created a new brand yii application with just one command. Now, got your browser and type in url: http://localhost/demo/index.php

You will find your new application running with a welcome page. Thats it.

php.exe is not recognized as an internal or external command - yiic - yii framework

This is a common error that you can come across while creating a new yii application using 'yiic' command tool. If you get this error, follow the below steps and your error will be fixed.

You just need to add complete path of your php.exe in yii/framework/yiic.bat
yiic.bat is the MS-DOS batch file.
For example in my case, it will be

if "%PHP_COMMAND%" == "" set PHP_COMMAND=C:\xampp\php\php.exe

This solution will work only for yii.

If you want to make it for all i.e make it possible to run php.exe in any command line instance, then you will have to add path of php.exe in your PATH environment variables.

Monday, May 27, 2013

PHP extension "curl" must be loaded - Magento

While installing magento, you can get this error on configuration part.

But you can resolve it easily. Magento installation wizard now checks for curl on setup. Just make sure that curl extension is enabled in your php.ini

You can uncomment the line referring to php_curl.dll and restart your web server. It should fix the error

Friday, May 24, 2013

Include all PHP files from a directory


Suppose you have a directory (or say folder) consisting of some PHP files and you want to include all of them in your code. You need not to write include for each and every file. Then, you can do it in one line as follows:

<?php
foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}
?>

Thursday, May 23, 2013

Export HTML table to excel xlsx in php

Many a times, it has been a requirement in PHP, you have to generate a report and down the html table report to excel. Here, I am providing the solution as how can you do it in PHP.

You can achieve this using PHPExcel library. First download PHPExcel library and put in your project folder. Then, apply the below code to generate excel report. Here is the code.

<?php
$arr['name'] = array('Prem', 'Shailesh', 'Rahul');
$arr['age'] = array('23', '21', '25');


$filename ="Excel_Report.xls";
$contents = "Name \t Age \n";


include('PHPExcel.php');
for($i = 0; $i < count($arr['name']; $i++)
{
   $contents.= $arr['name'][$i]. " \t";
   $contents.= $arr['age'][$i]. " \n";
}


header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
exit;

?>

yii chtml dropdown onchange

In yii framework, you can make a dropdownlist (or select) as follows:


<?php
echo CHtml::dropDownList('select_id','',
 $data,

 array(
'prompt'=>'Select',
)
)

?>

where data is the array in the form of key=>value.

But what if you have to put here a 'onchange' event or styling too with chtml dropdown list. It can be done as follows:


<?php
echo CHtml::dropDownList('select_id','',
 $data,

 array(
'prompt'=>'Select a Industry',
'style'=>'width:250px',
'onchange'=> 'myFunc()',
)
)

?>

Now, on change of the dropdown list, it will call a javascript function myFunc().

illegal mix of collations (latin1_swedish_ci implicit) and (latin1_general_ci implicit)

It is one of the common errors in MySql.

This error comes when you are fetching data from two or more than two tables via sql query and collation of those tables is not same. You have to make sure that the tables from which you are fetching data, have the same collation. After that also check the collation type of each table field that you have used in operation.
If collations are different, then you'll get the above error as illegal mix of collations.

To resolve this, you can change the collation of a table with the below query:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET latin1 
COLLATE 'latin1_swedish_ci';

This will be the fix for the error.

Total Pageviews