Social Icons

Featured Posts

Wednesday, October 14, 2015

Can't connect to mysql server on 'some_server'

I had this problem when installing a site on a CentOS server, connecting to a database on another server. It was giving the error as Can't connect to mysql server 'another_server'.

It turned out the connection to the database was being prevented by SELinux.

Issuing the following command (as root) told SELinux to allow the connection:
setsebool httpd_can_network_connect_db=1

Wednesday, February 18, 2015

How to override inline css

Generally, inline css is meant to be used to override external css. But sometimes, it needs that you cannot remove the inline css for an element and you have to overrride that using some external css. You can do this using "!important" in css

Say for example, you have a "p" element as follows:

<p id="mypara" style="color:red">
    Paragraph content
</p>

Here, you have to change the "p" background to blue color with external css i.e, you have to override the inline one. You can do this as follows:

#para {
    color: blue; !important
}

jquery remove a style property

There are many a times it happen that you need to remove a particular css style only for an element (say div). You can remove that style property using jquery.

Say for example, you have a div like this:

<div id="mydiv" style="overflow:auto;float:left">
       div content
</div>

Now, if u want to remove the "overflow" style only from the above div, then you can do it in jquery as follows:

$(#div).css("overflow", "");

The above code will remove the "overflow" style from the div.

Tuesday, April 8, 2014

Calculate difference between two dates in php

Suppose you have two dates as some start date and end date. You have to find the difference between those dates in years, months and days. It can be easily done by converting the date into timestamp value using strtotime function.

Say, you have two dates as follows:

$date1 = "2012-04-24";
$date2 = "2013-05-28";

Convert these dates into timestamp and subtract these.

$difference = abs(strtotime($date2) - strtotime($date1));

Now, you can calculate difference in years, month and days as follows:-

$years = floor($difference / (365*60*60*24));
$months = floor(($difference - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($difference - $years * 365*60*60*24 - $months * 30*60*60*24) / (60*60*24));

Another way to do it using OOP approach as follows:-

$date1 = new DateTime("2012-04-24");
$date2 = new DateTime("2013-05-28");

$interval = $date1->diff($date2);
echo $interval->y ." years ".$interval->m." months ".$interval->d." days";

Thursday, March 20, 2014

Why you shouldn't use mysql_* functions in php?

To use php with mysql database, there are many php functions for it. For example: mysql_connect(), mysql_select_db(), mysql_query(), mysql_real_escape_string() etc. Now you need to move away from it and stop using these functions. 

Very straight forward answer to stop using these functions is that they are outdated. These functions ae likely to be deprecated in higher versions of php. Some important reasons to avoid these can be concluded as below: 
  • They are likely to be deprecated.
  • All Mysql 5.1 functionalities are not supported.
  • They don't support stored procedures.
  • They don't support multiple statements etc.

You can start using mysqli or PDO extensions APIs to do the database sort of things.

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.

Total Pageviews