Social Icons

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.

Monday, December 24, 2012

For loop in C

for loop is also a kind of loop and used to execute a block of code for a specified number of times. It is the most widely used loop.


Syntax of 'for' loop


           for(initialization counter;loop condition;increment counter)
           {
                --------------------------------
                Some code to execute
                --------------------------------
           }


 Explanation of the syntax:


 There are 3 parts in the for loop written in a single line itself. They are:
  •  initialization counter - It is used to initialize the counter before execution of the loop starts.
  • loop condition - As the loop condition will be true, the execution of the loop will be continued. Its execution will be stopped when the loop condition is false.
  • increment counter - It is used to increase or decrease the value of counter each time after the end of each loop iteration. In other words, it will increase(or decrease) every time after the f block of code inside for loop will execute once.


 Now, lets see an example of for loop with a program.


 WAP to print numbers between 1 to 100 using for loop.


      #include<stdio.h>
      void main()
      {
           int i=1;
           for(i=1;i<100;i++)
           {
                 printf("%d\t",i);
           }
       }


   Explanation for the program:


           Here, we have initialized a variable i and set its value equal to 1. Now, it will come in for loop statement. Firstly, the initialization counter is there i.e, i=1. Then, it will check for the loop condition as i<100 i.e, currently 1<100 which is true. Then, it will execute the block of code written inside for loop i.e, printf statement. Now, it will print the value of i. Then, it will go to for loop statement again and will increase the value of i by 1 as i++ is there. So, now i=2. Again, it will check 2<=100 i.e, true. And value of i will be printed. Similarly, it will print from 1 to 100. \t is provided in the print statement as provide a tab space between each number.

Wednesday, December 19, 2012

do while loop in C

In the previous chapter, we read about while loop and its working. Herein this chapter, we shall read about the next loop in C i.e, do while loop. This loop is similar to while loop. But, the only difference is that do while loop at least execute the block of code once irrespective of the condition either true or false. In do while loop, firstly the block of code gets executed then the evaluation of condition takes place. If the condition is true then the block of code will again execute.


Syntax of do while Loop


            do
            {
                   --------------------------
                   Some Statements
                   --------------------------
            }
            while(condition);
             
Above is the syntax for do while loop. Now lets understand it with an example program.


 WAP to print 1 to 10 using 'do while' loop


          #include<stdio.h>
          void main()
          {
                 int no = 10, i=0;
                 do
                 {
                        i++;
                        printf("%d \n",i);
                 }
                 while(i < no);
          }


 Output :  1
                2
                3
                4
                5
                6
                7
                8
                9
                10


 Explanation for the above program:
 
                Here, we have taken two integer variables as no(//number) and i. Variable no is initialized to 10 and variable i is initialized to 0 as you can see in the program.  Then, it will come inside do and perform i++ i.e, value of i will be incremented by 1.  In the next line, with the printf statement, it will print the value of i i.e, 1 and goes to next line as \n is there. Now, it will come to while and check the condition if i<no. Till here, i = 1 and no = 10. So, it will check as while(1<10) means the condition is true and it will again go to the do statement and again execute the block of code inside do. So, value of i will be 2 now and it will be printed. Similarly, it will print upto 10 and then check for while(10 < 10) i.e false and come out of the loop.

Thursday, December 13, 2012

While Loop in C

While loop is one of the type of loops in C and used to repeat a block of code.

Syntax of while loop

          while (some condition)
          {
                 ---------------------
                 Some code to execute
                 ---------------------
                 increment/decrement
          }

     This loop will execute as long as the condition written in parentheses after while is correct. When the condition gets wrong, it will come out of the loop. If the condition is false in the very first then the block of code of while will not execute. Now, lets see an example of while loop using a program.

 WAP to print numbers between 1 to 100

             #include<stdio.h>
             void main()
             {
                   int  i=1;
                   while(i <= 100)
                   {
                          printf("%d \t", i);
                          i++;
                    }
               }

    Explanation:- Here, we have taken an integer variable 'i' initialized by 1. Now, it will come in while loop and will check its condition for the first time as i <= 100 means 1 <= 100. As the condition is true, it will go inside the loop and will print the value of i as 1. Again, there is an increment in i (i++). It will give one plus to the value of i. Now, i = 2. Again, it will check the condition of while as i <= 100. This time, it will check as 2 <= 100. Again, the condition is true and 2 will be printed. Similarly, it will be printed as  1 to 100. When value of i will be 101. It will check the condition as 101 <= 100. As the condition is false and it will come out of the loop.

Monday, December 10, 2012

Loops in C

Before discussing various loops in 'C', it is necessary to understand about loop, what exactly it is. We use loop when we have to repeat a block of code. For many times, it is required to execute the same code a number of times for some cases. Then we take the help of loop. 
                         There are three types of loop:- 
                          1.  while
                          2.  dowhile
                          3.   for

                 There are two keywords which are important in loop. So, before studying loops in deep, lets see these two keywords. They are:-
 1. break
 2. continue

 1. Break
               We use break to terminate the loop. In other words, we can say that it is useful to get exit from a loop under some circumstances. This will be more clear in using loops in next chapters.

2. Continue
                  Continue is a keyword that controls the flow of loops. Sippose we are executing a loop and hit a continue statement, the loop will stop its current iteration, update itself (in case of for loops) and begin to execute again from the top.

Total Pageviews