Monday, May 28, 2012

How to delete DUPLICATE records from MYSQL table

Some times we need to delete duplicate rows from a table. Because there is no need for keeping duplicate records in same table. Deleting these duplicate records is very easy. It can be don using some simple SQL queries.

Here’s two easy ways to clean out that table quickly.

1) Use ALTER IGNORE on MySQL 5.1+
MySQL will allow you to create a unique index on a table with duplicate records with its IGNORE SQL extension:

 ALTER IGNORE TABLE 'SHIPMENTS' ADD UNIQUE INDEX (CART_ID, TRACKING_NUMBER)  

Duplicates will be deleted.

2) Recreate the table with GROUP BY

 execute 'CREATE TABLE shipments_deduped like shipments;'  
 execute 'INSERT shipments_deduped SELECT * FROM shipments GROUP BY cart_id, tracking_number;'  
 execute 'RENAME TABLE shipments TO shipments_with_dupes;'  
 execute 'RENAME TABLE shipments_deduped TO shipments;'  
 add_index :shipments, [:cart_id, :tracking_number], :unique => true  
 execute 'DROP TABLE shipments_with_dupes;'  

Recreating the table is much, much faster than trying to delete the records in the existing table and doesn’t lock the existing table, making your application downtime minimal.

Insurance

Friday, May 25, 2012

How to download and unzip a .ZIP file using php

Today I was working on a website which will be an affiliate member of another big company with already running a very good website ( i don't want to mention the name of that affiliate program because with this my idea is some how disclosed to the public and i don't want to do so :) ). In that affiliate program I have to download the feeds in .zip form and then importing that in my database. So I needed a script to automatically download, unzip and then import that data in my database. By using my experience and skills and also with the help of Google, I have successfully written a script for this purpose. In this post I am going to share the same,

First part of my script checks that php zip extension is loaded or not.
 if (!extension_loaded('zip')) {  
                dl('zip.so');  
           }  
After that, mention the path of download file and destination folder and rest of the script is as follows,
 $target_url = "http://www.example.com/test.zip";  
 $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';  
 $file_zip = "downloads/new.zip";  
 $file_txt = "downloads/".md5(time())."new.txt";  
 echo "<br>Starting<br>Target_url: $target_url";  
 echo "<br>Headers stripped out";  
 // make the cURL request to $target_url  
 $ch = curl_init();  
 $fp = fopen("$file_zip", "w");  
 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);  
 curl_setopt($ch, CURLOPT_URL,$target_url);  
 curl_setopt($ch, CURLOPT_FAILONERROR, true);  
 curl_setopt($ch, CURLOPT_HEADER,0);  
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
 curl_setopt($ch, CURLOPT_AUTOREFERER, true);  
 curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);  
 curl_setopt($ch, CURLOPT_TIMEOUT, 10);  
 curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);   
 curl_setopt($ch, CURLOPT_FILE, $fp);  
 $page = curl_exec($ch);  
 if (!$page) {  
   echo "<br />cURL error number:" .curl_errno($ch);  
   echo "<br />cURL error:" . curl_error($ch);  
   exit;  
 }  
 curl_close($ch);  
 echo "<br>Downloaded file: $target_url";  
 echo "<br>Saved as file: $file_zip";  
 echo "<br>About to unzip ...";  
 // Un zip the file  
 $zip = new ZipArchive;  
   if (! $zip) {  
     echo "<br>Could not make ZipArchive object.";  
     exit;  
   }  
   if($zip->open("$file_zip") != "true") {  
       echo "<br>Could not open $file_zip";  
         }  
   $zip->extractTo("$file_txt");  
   $zip->close();  
 echo "<br>Unzipped file to: $file_txt<br><br>";  
If you like this script, so please post your comments and feedback. Your good feedback is the main desire of me to continue writing useful posts for my blog reader. Thanks

Tuesday, May 22, 2012

Password protection using .htaccess

Some times, some portions of website need to be password protected in order to prevent it from UN-autorized access. There are many ways to do this. One way is achieved by server side programming and other way to achieve this is client side programming like java-script. In this post I will discuss password protection using .htaccess file. This method have following steps to follow,

1. First decide what portion or directory you want to make password protected. I am considering root directory in this post.

2. After this go to this URL http://www.tools.dynamicdrive.com/password/ and generate code for your .htaccess and .htpasswd files.

3. After this you need to save these codes in .htaccess and .htpasswd files respectively. I you are unable to create .htaccess file, you can read this http://our-knowledge-base.blogspot.com/2012/05/how-to-create-htaccess-file-simple.html article to learn how to create this. Creating .htpasswd file is exactly same like creating .htaccess file.

4. Now upload the both file in the corresponding directory. Your website is password protected now.

feel free to post your feedback

Thanks


How to create .htaccess file - Simple steps

I this simple step by step tutorial I will tell you how to create .htaccess file using a notepad editor.
Here are the steps to do this,
 
1. Open your Windows notepad editor.


2. Make sure that the word wrap function is off.


3. Select File, then select New
 

 4. You should have a screen that doesn’t contain any text.

5. Next, select Save As

6. Type the following for your file name, .htaccess

Browse your folders and select which folder you want to save the .htaccess file in, then click Save
The next step is to verify that the file was named .htaccess by the editor. You simply open the Windows file manager, locate the file, right-click the file icon and selecting Properties.) If the file was named with the .txt suffix, you can rename the file to .htaccess and your done.

Thanks

Friday, May 18, 2012

Balanced Diet Plane

Hey guys, if you want a perfect diet plane chart, then this post is just for you.

Cereals (per 100 grams) Calories
Breads (per slice) Calories
Vegetables (per 100 grams) Calories
Fruits (per 100 grams) Calories
Desserts (per 100 grams) Calories
Milk Products (per cup) Calories
Others Calories
Bajra 360

Maize flour 355

Rice 325

Wheat flour 341
Chapati (wheat bread) (medium) 119

White bread 60

Paratha (not stuffed) 280
Cabbage 45

Carrot 48

Cauliflower 30

Corn (baked) 84

Cucumber 12

Eggplant (Fresh) 24

Eggplant (Cooked) 69

Fenugreek (methi) 49

French Beans 26

Lettuce 21

Peas 93

Potato 97

Potato (fried- 1 cup) 450

Potato (baked- 1 cup) 100

Potato (mashed- 1 cup) 245

Potato (boiled- 1 cup) 83

Pumpkin (cooked) 33

Spinach 26

Tomato (fresh) 21

Tomato (stuffed and baked) 58

Tomato (baked) 39
Apple 56

Banana 153

Black Grapes 45

Chickoo (Sapota) 94

Cherries 70

Dates 281

Guava 66

Litchies 61

Mango 70

Muskmelon 74

Orange 53

Papaya 32

Peach 50

Pears 51

Pineapple 46

Plums 56

Pomegranate 77

Watermelon 16
Biscuits 399

Boondi ladoo 150

Cake (with icing) 302

Cake (without icing) 218

Cookies (butter cookies) 482

Custard 205

Fruit pie 236

Fruit salad 80

Gujia 501

Gulab Jamun 387

Halwa (atta) 263

Halwa (rawa) 181

Halwa (sohan halwa) 399

Jalebi 494

Jelly 65

Kheer (rice) 141

Kheer (rice-carrot) 226

Malpua 325

Sandesh 57
Buffalo milk 115

Butter (100gms.) 750

Buttermilk 19

Cheese 315

Cow milk 100

Cream (100gms) 210

Ghee (100gms) 910

Skimmed milk 45
Coconut water (100 ml) 25

Coffee 40

Honey (1 tbsp) 90

Orange juice (100 ml) 47

Sugar (1 tbsp) 48

Tea 30

Tomato juice (100ml) 22

How to change "font-family" in dompdf script

In latest web development industry document conversion from one format to other format is very important. Sometimes we need to generate spread sheet files, some times we need to generate word files and some times PDF files. In this topic I will be talking about PDF file conversion.

For generating PDF files from the given data, there are a number of scripts and modules written in php and different languages. DomPDF is one of them written in php. DomPDF is fabulous in its features. It allows direct conversion of HTML files to PDF files. But there are some limitations like some times it never generates the exact copy of that HTML. Some times font-family doesn't apply correctly as we apply in HTML file. I had the same problem last days when I was using DomPDF for my PDF generation. I goggled the web and found some solutions. And on the basis of those many solutions and posts, I wrote a summarized article focusing on this problem.

It need some simple steps to follow,

  1. Download archive from approved answer. Extract files /dompdf/lib/fonts/times*.*.
  2. Copy them in your dompdf fonts folder.
  3. Edit dompdf_font_family_cache.dist.php with snippet 1.
  4. In CSS use font-family: times;.

Snippet 1:

 /* ... */  
 'times' => array (  
   'normal' => DOMPDF_FONT_DIR . 'times',  
   'bold' => DOMPDF_FONT_DIR . 'timesbd',  
   'italic' => DOMPDF_FONT_DIR . 'timesi',  
   'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'  
 ),  
 'times-roman' => array (  
   'normal' => DOMPDF_FONT_DIR . 'times',  
   'bold' => DOMPDF_FONT_DIR . 'timesbd',  
   'italic' => DOMPDF_FONT_DIR . 'timesi',  
   'bold_italic' => DOMPDF_FONT_DIR . 'timesbi'  
 ),  
 /* ... */  

If you want to use your own TTF font (say, Arial.ttf):
  1. Run: ttf2afm -o Arial.afm Arial.ttf. (I did it in Ubuntu.)
  2. Run: ttf2ufm -a -F Arial.ttf. (you can use /dompdf/lib/ttf2ufm/bin/ttf2ufm.exe.)
  3. Copy Arial.* files in /dompdf/lib/fonts/.
  4. Add to dompdf_font_family_cache.dist.php snippet 2.
  5. In CSS use font-family: arial;.

Snippet 2:

 /* ... */  
 'arial' => array (  
   'normal' => DOMPDF_FONT_DIR . 'Arial',  
   'bold' => DOMPDF_FONT_DIR . 'Arial',  
   'italic' => DOMPDF_FONT_DIR . 'Arial',  
   'bold_italic' => DOMPDF_FONT_DIR . 'Arial'  
 )  
 /* ... */  

 Please feel free to post you comments and suggestions.

Thanks
 

Thursday, May 3, 2012

Working with arrays in pyrocms plugins

Hi guys, I am writing this post for one of my cool blog reader. He posted a comment that how we can return arrays from plugins to plugin view files and then how we can loop through and display the values of that array.

Pyrocms is very cool in handling this type of thing because it has a very strong template engine already built-in its codeignitor package. In order to explain how to use array in pyrocms plugin, i will take the same "example" plugin which I explained in this post http://our-knowledge-base.blogspot.com/2012/04/how-to-develop-custom-plugin-in-pyrocms.html.

Adding another function:

Open "example" plugin code and put the following function some where in that class.
 <?php  
 public function myposts()  
 {  
           $plugin_data = array();  
           $plugin_data[] = array(  
                                    'value'          => "Post # 1 value",  
                                    'title'          => 'Post # 1 title'  
                               );  
           $plugin_data[] = array(  
                                    'value'          => "Post # 2 value",  
                                    'title'          => 'Post # 2 title'  
                               );  
           $plugin_data[] = array(  
                                    'value'          => "Post # 3 value",  
                                    'title'          => 'Post # 3 title'  
                               );  
           return $plugin_data;  
      }  
 ?>  

so after adding this function whole plugin code will look like this,
1:  <?php defined('BASEPATH') OR exit('No direct script access allowed');  
2:  /**  
3:   * Example Plugin  
4:   *  
5:   * Quick plugin to demonstrate how things work  
6:   *  
7:   * @package          PyroCMS  
8:   * @author          PyroCMS Dev Team  
9:   * @copyright     Copyright (c) 2009 - 2010, PyroCMS  
10:   *  
11:   */  
12:  class Plugin_Example extends Plugin  
13:  {  
14:       /**  
15:        * Hello  
16:        *  
17:        * Usage:  
18:        * {{ example:hello }}  
19:        *  
20:        * @param     array  
21:        * @return     array  
22:        */  
23:       function hello()  
24:       {  
25:            $name = $this->attribute('name', 'World');  
26:            return 'Hello '.$name.'!';  
27:       }  
28:   
29:  
30:
31:   public function myposts()  
32:   {  
33:         $plugin_data = array();  
34:         $plugin_data[] = array(  
35:                                    'value'          => "Post # 1 value",  
36:                                    'title'          => 'Post # 1 title'  
37:                               );  
38:         $plugin_data[] = array(  
39:                                    'value'          => "Post # 2 value",  
40:                                    'title'          => 'Post # 2 title'  
41:                               );  
42:         $plugin_data[] = array(  
43:                                    'value'          => "Post # 3 value",  
44:                                    'title'          => 'Post # 3 title'  
45:                               );  
46:          return $plugin_data;  
47:      }  
48:  }  
49:  /* End of file example.php */

How to use this?
Using this is very simple. You just have to paste following code in view file. It will show post titles in the form of a list.
 {{ example:myposts }}  
     <strong>{{ name }}:</strong> {{ value }}<br />  
 {{ /example:myposts }}  

Although this is a very simple explanation of how to handle arrays in pyrocms, but after ready and understanding this example, you can easily modify this or even can develop your own complex plugin.

Please feel to post comments on this.

Thanks

Wednesday, May 2, 2012

Tips to Improve Self Confidence

1. Groom yourself

This seems like such an obvious one, but it’s amazing how much of a difference a shower and a shave can make in your feelings of self-confidence and for your self-image. There have been days when I turned my mood around completely with this one little thing.



2. Dress nicely

A corollary of the first item above … if you dress nicely, you’ll feel good about yourself. You’ll feel successful and presentable and ready to tackle the world. Now, dressing nicely means something different for everyone … it doesn’t necessarily mean wearing a $500 outfit, but could mean casual clothes that are nice looking and presentable.


3. Photoshop your self-image

Our self-image means so much to us, more than we often realize. We have a mental picture of ourselves, and it determines how confident we are in ourselves. But this picture isn’t fixed and immutable. You can change it. Use your mental Photoshopping skills, and work on your self-image. If it’s not a very good one, change it. Figure out why you see yourself that way, and find a way to fix it.

4. Think positive.

One of the things I learned when I started running, about two years ago, what how to replace negative thoughts (see next item) with positive ones. How I can actually change my thoughts, and by doing so make great things happened. With this tiny little skill, I was able to train for and run a marathon within a year. It sounds so trite, so Norman Vincent Peale, but my goodness this works. Seriously. Try it if you haven’t.

5. Kill negative thoughts

Goes hand-in-hand with the above item, but it’s so important that I made it a separate item. You have to learn to be aware of your self-talk, the thoughts you have about yourself and what you’re doing. When I was running, sometimes my mind would start to say, “This is too hard. I want to stop and go watch TV.” Well, I soon learned to recognize this negative self-talk, and soon I learned a trick that changed everything in my life: I would imagine that a negative thought was a bug, and I would vigilantly be on the lookout for these bugs. When I caught one, I would stomp on it (mentally of course) and squash it. Kill it dead. Then replace it with a positive one. (”C’mon, I can do this! Only one mile left!”)

6. Get to know yourself

When going into battle, the wisest general learns to know his enemy very, very well. You can’t defeat the enemy without knowing him. And when you’re trying to overcome a negative self-image and replace it with self-confidence, your enemy is yourself. Get to know yourself well. Start listening to your thoughts. Start writing a journal about yourself, and about the thoughts you have about yourself, and analyzing why you have such negative thoughts. And then think about the good things about yourself, the things you can do well, the things you like. Start thinking about your limitations, and whether they’re real limitations or just ones you’ve allowed to be placed there, artificially. Dig deep within yourself, and you’ll come out (eventually) with even greater self-confidence.

7. Act positive

More than just thinking positive, you have to put it into action. Action, actually, is the key to developing self-confidence. It’s one thing to learn to think positive, but when you start acting on it, you change yourself, one action at a time. You are what you do, and so if you change what you do, you change what you are. Act in a positive way, take action instead of telling yourself you can’t, be positive. Talk to people in a positive way, put energy into your actions. You’ll soon start to notice a difference.

8. Be kind and generous

Oh, so corny. If this is too corny for you, move on. But for the rest of you, know that being kind to others, and generous with yourself and your time and what you have, is a tremendous way to improve your self-image. You act in accordance with the Golden Rule, and you start to feel good about yourself, and to think that you are a good person. It does wonders for your self-confidence, believe me.

9. Get prepared

It’s hard to be confident in yourself if you don’t think you’ll do well at something. Beat that feeling by preparing yourself as much as possible. Think about taking an exam: if you haven’t studied, you won’t have much confidence in your abilities to do well on the exam. But if you studied your butt off, you’re prepared, and you’ll be much more confident. Now think of life as your exam, and prepare yourself.

10. Know your principles and live them

What are the principles upon which your life is built? If you don’t know, you will have trouble, because your life will feel directionless. For myself, I try to live the Golden Rule (and fail often). This is my key principle, and I try to live my life in accordance with it. I have others, but they are mostly in some way related to this rule (the major exception being to “Live my Passion”). Think about your principles … you might have them but perhaps you haven’t given them much thought. Now think about whether you actually live these principles, or if you just believe in them but don’t act on them.

11. Speak slowly

Such a simple thing, but it can have a big difference in how others perceive you. A person in authority, with authority, speaks slowly. It shows confidence. A person who feels that he isn’t worth listening to will speak quickly, because he doesn’t want to keep others waiting on something not worthy of listening to. Even if you don’t feel the confidence of someone who speaks slowly, try doing it a few times. It will make you feel more confident. Of course, don’t take it to an extreme, but just don’t sound rushed either.

12. Stand tall

I have horrible posture, so it will sound hypocritical for me to give this advice, but I know it works because I try it often. When I remind myself to stand tall and straight, I feel better about myself. I imagine that a rope is pulling the top of my head toward the sky, and the rest of my body straightens accordingly. As an aside, people who stand tall and confident are more attractive. That’s a good thing any day, in my book.

13. Increase competence

How do you feel more competent? By becoming more competent. And how do you do that? By studying and practicing. Just do small bits at a time. If you want to be a more competent writer, for example, don’t try to tackle the entire profession of writing all at once. Just begin to write more. Journal, blog, write short stories, do some freelance writing. The more you write, the better you’ll be. Set aside 30 minutes a day to write (for example), and the practice will increase your competence.

please share your thoughts about "how to improve self confidence"...keep reading :)