Saturday, August 24, 2019

How to calculate the total number of days between two dates in PHP?

This articles is to find the difference between total number of days for two specified dates (e.g. start date and end dates).

 Examples : 

 Start date : 2019-02-07

 End date : 2019-05-12

 Here are two different function to find out the total number of days between two dates

 Example 1 : 

// Define start date and end date
 $start_date = date_create("2019-02-07");
 $end_date = date_create("2019-05-12");

// Get the difference between the total days of two dates
 $total_days = date_diff($start_date,$end_date);
 print "Total number of days between two dates: ".$total_days->format("%a");


Output :   Total number of days between two dates: 94

Example 2 : 

// Define start date and end date
 $start_date = strtotime("2019-02-07");
 $end_date = strtotime("2019-05-12");

// Get the difference between the total days of two dates
 $total_days = ($end_date - $start_date);
 print "Total number of days between two dates: " .  round($total_days / (60 * 60 * 24));

Output :   Total number of days between two dates: 94

Thats it !!!!

How to hide/disable deprected errors in PHP?

If you want to hide/disable deprecated all errors/notices completely. Please add the following line into your php.ini file or your setting.php file as per your framework.

For examples : If you use PHP then add below line to php.ini or you use osCommerce please add below line to application_top.php file.

  For php.ini file :

  ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);

  For WAMP server : 
  Please open php.ini file amd use below line

  error_reporting = E_ALL & ~E_NOTICE

  For osCommerce site :

  error_reporting(E_ALL & ~E_NOTICE);

  For wordpress site :

  error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  OR
  error_reporting(E_ALL ^ E_DEPRECATED);

  Now, All errors/notices will be hide/disable.

Tuesday, August 13, 2019

How to get multiple checkboxes with value of drop down in php?

I have a php form which you can select multiple checkboxes with values from drop down list. 

Please see below steps to select the multiple checkboxes as per drop down selection.

Step 1: create a drop down list using HTML form.

<form name="ques" action="" method="post">
    <div class="title">
Multiple Checkboxes
</div>
<div class="multi">
<select id="security_ques" name="security_ques" onchange="check_question();">
<option value="" selected>Select question</option>
<option value="1">Physical</option>
<option value="2">Chemistry</option>
<option value="3">Biology</option>
</select>
</div>

<div id="physical" class="check">
<div><input type="checkbox" name="ques" value="Question1">Question1</div>
<div><input type="checkbox" name="ques" value="Question2">Question2</div>
<div><input type="checkbox" name="ques" value="Question3">Question3</div>
</div>

<div id="chemistry" class="check">
<div><input type="checkbox" name="ques" value="Question1">Question4</div>
<div><input type="checkbox" name="ques" value="Question1">Question5</div>
<div><input type="checkbox" name="ques" value="Question1">Question6</div>
</div>

<div id="biology" class="check">
<div><input type="checkbox" name="ques" value="Question1">Question7</div>
<div><input type="checkbox" name="ques" value="Question1">Question8</div>
<div><input type="checkbox" name="ques" value="Question1">Question9</div>
</div>
</form>

Step 2: CSS code

<style>
.title {
color: blue;
font-size: 24px;
margin: 10px;
}

.multi {
margin: 10px;
}

.check {
margin: 10px;
display:none;
}
</style>

Step 3: Add Javascript code

<script type="text/javascript">
function check_question() {
    if(document.getElementById('security_ques').value == "1") {
        document.getElementById('physical').style.display = 'block';
document.getElementById('chemistry').style.display = 'none';
        document.getElementById('biology').style.display = 'none';

    } else if(document.getElementById('security_ques').value == "2") {
        document.getElementById('physical').style.display = 'none';
document.getElementById('chemistry').style.display = 'block';
        document.getElementById('biology').style.display = 'none';

    } else if(document.getElementById('security_ques').value == "3") {
        document.getElementById('physical').style.display = 'none';
document.getElementById('chemistry').style.display = 'none';
        document.getElementById('biology').style.display = 'block';
    }
}
</script>

Output :


  • Before select the questions

  • After select the question show the multiple checkboxes

Thursday, August 8, 2019

How to get all selected checkbox values in an array using PHP and jQuery?

In this blogs, I show you how to get all selected checkbox values in an array through PHP and jquery. Here you will easily get all selected checkbox values in php and jquery.

Please see below steps :

Step 1 : Define below jquery script

<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

Step 2 : Add HTML and PHP code

<p id="title">Multiple Subject  Checkboxes</p>
<p>Before click on submit button please check few subject checkbox.</p>
<input type="checkbox" name="subject" value="English" /> English
<input type="checkbox" name="subject" value="Maths" /> Maths
<input type="checkbox" name="subject" value="Science" />Science
<br>
<button name="submit">Submit</button>
<p id="multicheck"></p> 

Step 3 : Add javascript

<script type="text/javascript">     
$('button').on('click', function() {
    var array = [];
    $("input:checked").each(function() {
array.push($(this).val());
    });
    $('#multicheck').text(array);
});
</script>

Step 4 :  Add CSS

<style>         
#multicheck {
     color: Red;
     font-weight: bold;
     font-size: 20px;
}       
button {
      margin-top: 20px;
      margin-left: 60px;
}
#title {
     color: blue;
     font-size: 26px;
     font-weight: bold;
}
</style>

Now, Here is Full PHP, HTML and Jquery code


<html>
<head>
    <title>
        Get all selected checkbox values in an array | PHP | Jquery
    </title>
    <style>         
        #multicheck {
                color: Red;
font-weight: bold;
                font-size: 20px;
        }       
        button {
                margin-top: 20px;
margin-left: 60px;
        }
#title {
color: blue;
font-size: 26px;
font-weight: bold;
}
    </style>
</head>
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> 
<body>
    <p id="title">Multiple Subject  Checkboxes</p>
    <p>Before click on submit button please check few subject checkbox.</p>
    <input type="checkbox" name="subject" value="English" /> English
    <input type="checkbox" name="subject" value="Maths" /> Maths
    <input type="checkbox" name="subject" value="Science" />Science
    <br>
    <button name="submit">Submit</button>
    <p id="multicheck"></p>
    <script type="text/javascript">     
        $('button').on('click', function() {
            var array = [];
            $("input:checked").each(function() {
                array.push($(this).val());
            });
            $('#multicheck').text(array);
        });
    </script>
</body> 
</html>

Output : 
  • Before select the checkboxes
  • After select the checkboxes show the values

Thats it !!!!

I hope it can help you...


Saturday, August 3, 2019

How to export MySQL data to Excel file with send excel content to email in PHP?

Here we learn how to export the MySQL data to export file and after that send excel content to email through Phpmailer.

Also here is enter the captcha code before export the data to excel file.

It's very easy stepd to export the MySQL data to PHP excel file. Please follow below step by step and see screenshots.
  • Capcha code HTML form





  • Captcha validation







  • Excel sheet screenshot










Step 1 : Create MySQL Database Table

Firstly we create MySQL database table using below query to export data into excel file.

CREATE TABLE `users` (
  `users_id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(50) NOT NULL DEFAULT '',
  `lastname` varchar(50) NOT NULL DEFAULT '',
  `email_address` varchar(70) NOT NULL DEFAULT '',
  `city` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Step 2 : Make MySQL Server database connection

Now, first we make MySQL Server database connection from the PHP file. So, add the server name, username, password and database name.

<?php
$DB_Server = "localhost"; //mysql Server 
$DB_Username = "root"; //mysql Username   
$DB_Password = ""; //mysql Password   
$DB_DBName = "users"; //mysql Database Name

//create mysql connection
$connect = mysqli_connect($DB_Server, $DB_Username, $DB_Password, $DB_DBName);
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
} ?>


Step 3: Get MySQL Data

we make Query which will select the firstname, lastname, email_address and city from the database table. As show below:

$sql = "Select * from users";
$result = mysqli_query($Connect, $sql);


Step 4: Export the data to excel file 

We make the query and collect the database data and use file_put_contents php function to export the data in excel file

$sql = "Select * from users";
$result = mysqli_query($Connect, $sql); 

$header = "User ID" . "\t";
$header .= "Firstname" . "\t";
$header .= "Lastname" . "\t";
$header .= "City" . "\t";
$header .= "Country" . "\t";

$data = '';
while( $row = mysql_fetch_array($result)){
$row1 = array();
$row1[] = $row['users_id'];
$row1[] = $row['firstname'];
$row1[] = $row['lastname'];
$row1[] = $row['city'];
$row1[] = $row['country'];
$data .= join("\t", $row1)."\n";}

file_put_contents("excel/report1.xlsx","$header\n$data");

Step 5: Send the excel file content to email

For send excel file content to email through phpmailer class

require("phpmailer.php");
$mail = new PHPMailer();
$mail->SetFrom('manson1306@gmail.com', 'Manoj Son'); //Name is optional
$mail->Subject   = 'Test email';
$mail->Body      = "Test Message";
$mail->AddAddress( 'manson1306@gmail.com' );
$first_report = 'excel/report1.xls';
$second_report = 'excel/report2.xls';
// If checked both checkboxes to send both attachment
if(isset($_POST["reports"][0]) && isset($_POST["reports"][1])){
$mail->AddAttachment( $first_report );
$mail->AddAttachment( $second_report );
} else { // If checked single checkbox to send single attachment
$mail->AddAttachment( $first_report );
}
if($mail->Send()) {
$success_message = "Email send successfully.";
} else {
echo "\r\nMail not sent. " .  $mail->ErrorInfo;
}

Step 6: HTML code with Captcha and export button

<form name="frmExcel" method="post" action="">
<table border="0" cellpadding="10" cellspacing="1" width="100%" class="demo-table">
<?php
if(isset($error_message)) { ?>
<tr id="error-captcha" class="demo-error"><td><?php  echo $error_message; ?></td></tr>
<?php }?>
<tr class="tablerow">
<td>Captcha Code: <br/>
<input name="captcha_code" type="text" class="demo-input captcha-input">
</td>
<td><br/><input type="submit" name="submit" value="Submit" class="demo-btn"></td>
</tr>
</table>
</form>

Step 7: CSS code

<style>
body {
font-family:calibri;
width:610px;
}
.demo-error {
display:inline-block;
color:#FF0000;
background: #dcfddc;
}
.demo-input {
    width: 100%;
    border-radius: 5px;
    border: #CCC 1px solid;
    padding: 10px;
    margin-top: 5px;
}
.demo-btn {
padding: 10px;
    border-radius: 5px;
    background: #478347;
    border: #325a32 1px solid;
    color: #FFF;
    font-size: 1em;
    width: 100%;
    cursor:pointer;
}
.demo-table {
    background: #dcfddc;
    border-radius: 5px;
    padding: 10px;
}
.demo-success {
    color: #FF0000;
    border-radius: 5px;
}
.captcha-input {
background:#FFF url('captcha_code.php') repeat-y;
padding-left: 85px;
}
</style>

Step 8: Captcha code

Please create captch_code.php file and add below code

<?php
session_start();
$random_alpha = md5(rand());
$captcha_code = substr($random_alpha, 0, 6);
$_SESSION["captcha_code"] = $captcha_code;
$target_layer = imagecreatetruecolor(70,30);
$captcha_background = imagecolorallocate($target_layer, 255, 160, 119);
imagefill($target_layer,0,0,$captcha_background);
$captcha_text_color = imagecolorallocate($target_layer, 0, 0, 0);
imagestring($target_layer, 5, 5, 5, $captcha_code, $captcha_text_color);
header("Content-type: image/jpeg");
imagejpeg($target_layer);
?>

Step 9: Full PHP, HTML and CSS code

Here is database table connection, PHP, HTML, CSS with captcha validation code

<?php
$DB_Server = "localhost"; //mysql Server 
$DB_Username = "root"; //mysql Username   
$DB_Password = ""; //mysql Password   
$DB_DBName = "users"; //mysql Database Name

//create mysql connection
$connect = mysqli_connect($DB_Server, $DB_Username, $DB_Password, $DB_DBName);
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if(count($_POST)>0) {

if($_POST["captcha_code"] == $_SESSION["captcha_code"]){

$sql = "Select * from users";
        $result = mysqli_query($Connect, $sql);   

$header = "User ID" . "\t";
$header .= "Firstname" . "\t";
$header .= "Lastname" . "\t";
$header .= "City" . "\t";
$header .= "Country" . "\t";

$data = '';
while( $row = mysql_fetch_array($result)){
$row1 = array();
$row1[] = $row['users_id'];
$row1[] = $row['firstname'];
$row1[] = $row['lastname'];
$row1[] = $row['city'];
$row1[] = $row['country'];
$data .= join("\t", $row1)."\n";}

@file_put_contents("excel/report1.xlsx","$header\n$data");

/* email start here */
require("phpmailer.php");
$mail = new PHPMailer();
$mail->SetFrom('manson1306@gmail.com', 'Manoj Son'); //Name is optional
$mail->Subject   = 'Test email';
$mail->Body      = "Test Message";
$mail->AddAddress( 'manson1306@gmail.com' );
$first_report = 'excel/report1.xls';
$second_report = 'excel/report2.xls';
// If checked both checkboxes to send both attachment
if(isset($_POST["reports"][0]) && isset($_POST["reports"][1])){
$mail->AddAttachment( $first_report );
$mail->AddAttachment( $second_report );
} else { // If checked single checkbox to send single attachment
$mail->AddAttachment( $first_report );
}
if($mail->Send()) {
$success_message = "Email send successfully.";
} else {
echo "\r\nMail not sent. " .  $mail->ErrorInfo;
}

}else{
$error_message = "Please enter Captcha Code.";
}
 }
?>
<style>
body {
font-family:calibri;
width:610px;
}
.demo-error {
display:inline-block;
color:#FF0000;
background: #dcfddc;
}
.demo-input {
    width: 100%;
    border-radius: 5px;
    border: #CCC 1px solid;
    padding: 10px;
    margin-top: 5px;
}
.demo-btn {
padding: 10px;
    border-radius: 5px;
    background: #478347;
    border: #325a32 1px solid;
    color: #FFF;
    font-size: 1em;
    width: 100%;
    cursor:pointer;
}
.demo-table {
    background: #dcfddc;
    border-radius: 5px;
    padding: 10px;
}
.demo-success {
    color: #FF0000;
    border-radius: 5px;
}
.captcha-input {
background:#FFF url('captcha_code.php') repeat-y;
padding-left: 85px;
}
</style>
<form name="frmExcel" method="post" action="">
<table border="0" cellpadding="10" cellspacing="1" width="100%" class="demo-table">
<?php
if(isset($error_message)) { ?>
<tr id="error-captcha" class="demo-error"><td><?php  echo $error_message; ?></td></tr>
<?php }?>
<tr class="tablerow">
<td>Captcha Code: <br/>
<input name="captcha_code" type="text" class="demo-input captcha-input">
</td>
<td><br/><input type="submit" name="submit" value="Submit" class="demo-btn"></td>
</tr>
</table>
</form>

That's it...

Saturday, July 20, 2019

HTML Level 1 Freelancer Exam Questions and Answers

1. The _____________ tag is used to create a bulleted list.
  • <list>
  • <ol>
  • <index>
  • <ul>
  • <li>
Answer : <ul>

2. HTML stands for:
  • Homepage Text Maker Language
  • HyperText Markup Language
  • Homepage Tool Making Language
  • HyperText Machine Language
  • Hyperlinked Text Markup Language
Answer: HyperText Markup Language

3. Which of the following form tags was introduced in HTML 5?
  • label
  • legend
  • optgroup
  • output
  • textarea
Answer:  output

4. The <meta> tag should be placed in the ________ section of an HTML page.
  • <body>
  • <base>
  • <head>
  • <footer>
  • <form>
Answer: <head>

5. Which of the following is NOT an attribute of an input element introduced in HTML 5?
  • pattern
  • multiple
  • disabled
  • autocomplete
  • placeholder
Answer:  disabled

6. The top-level container for an HTML document is:
  • <p></p>
  • <footer></footer>
  • <html></html>
  • <body></body>
  • <table></table>
Answer: <html></html>

7. What is the correct tag to display text as formatted in the html document?
  • <format>
  • <print>
  • <pre>
  • <text>
  • <asis>
Answer:  <pre>

8. Which element was not removed by HTML5?
  • <center>
  • <strike>
  • <small>
  • <big>
Answer:  <small>

9. Most HTML editors use __________ to transfer files to and from web servers.
  • HTML
  • HTTP
  • FTP
  • POP
  • SMTP
Answer:  FTP

10. To set a root URL for all links on an HTML page, use the __________ tag.
  • <a>
  • <href>
  • <link>
  • <root>
  • <base>
Answer:  <base>

Friday, July 19, 2019

US English Level 1 Freelancer Exam Questions and Answers

1. Nineteen is an ____ number.
  • strange 
  • odd
  • weird 
  • round
Answer : odd

2. ____I give an order, you should follow it!
  • In case of 
  • where
  • In case 
  • if
  • Because of
Answer : If

3.  Cedric was very——–.
  • erritated
  • irritated
  • iritated
  • earitated
  • irretated
Answer: irritated

4. —– not like you.
  • i’m
  • Am
  • i’re
  • I’m
  • we
Answer: I’m

5. A valley is a low ____ area.
  •   lyeing 
  •   lying 
  •   lie-ing 
  •   laying 
  •   lieing 
Answer : laying

6. You got yourself ____ this mess; get yourself out.
  •   for
  •   at
  •   from
  •   by
  •   into
Answer : into

8. You would —-better off in the other car.
  • never
  • become
  • have been
  • been
  • have
Answer: have been

9. It _____ matter if we stay another day.
  •   don't 
  •   not 
  •   isn't 
  •   won't 
  •   ain't 
Answer : won't

10. A statement should end with a ______.
  •   comma 
  •   question mark 
  •   period 
  •   exclamation mark 
  •   colon
Answer : period

11. She—– three apples.
  • have
  • got
  • is
  • on
  • has
Answer: got

12. We will ________ you to the house.
  •   after 
  •   come at 
  •   behind 
  •   follow 
  •   flow 
Answer : follow

13. At the edge of the trail there was a _____ cliff.
  •   sheared 
  •   sheerly 
  •   shear 
  •   sheerest 
  •   sheer 
Answer : sheer 

14.  —– there any time left?
  • How
  • Were
  • Is
  • is
  • What
Answer: Is

15. You should only take this drug if ____ prescribed by a doctor.
  •   they are 
  •   it 
  •   it's 
  •   when 
  •   its
Answer : it's

16. How—– money do you want to earn?
  • many
  • do
  • few
  • much
  • more
Answer: much

17. I try __ be friendly to my neighbors.
  •   too 
  •   always 
  •   and 
  •   for 
  •   to 
Answer : to

18. Your phone is over—-.
  • there
  • they’re
  • some
  • to
  • their
Answer: there

19. Buckingham ____ is a popular tourist attraction
  •   Palace 
  •   place 
  •   open 
  •   sure 
Answer : Palace 

20. The river is ______ here than I remember.
  •   most wide 
  •   the most wide 
  •   wide 
  •   widest 
  •   wider 
Answer : wider

21. John writes very _____.
  •  best
  •  better
  •  good
  •  much good
  •  well
Answer: well

22. Don't ever do that __________!
  •  hear
  •  not again
  •  again
  •  some more
  •  their
Answer: again

23. Most wines are made _________ grapes.
  •  as
  •  at
  •  into
  •  on
  •  from
Answer: from

24. Our visitor was from ___________.
  •  England
  •  england
  •  far
  •  scotland
  •  their
Answer: England

25. A/an ___________ is most commonly used to indicate a short pause.
  •  colon
  •  comma
  •  apostrophe
  •  period
  •  slash
Answer: comma

26. You _______ drink lots of water.
  •  ever
  •  make
  •  take
  •  will be
  •  should
Answer: should

27. I don't have __________ apples.
  •  any
  •  lots
  •  much
  •  every
  •  no
Answer: any

28. It __________ matter if we stay another day.
  •  ain't
  •  don't
  •  won't
  •  isn't
  •  not
Answer: won't

29. Does anyone ___________ the answer?
  •  found 
  •  has
  •  have known
  •  got
  •  know
Answer: know

30. Be sure you get to school _______ time.
  •  at
  •  by
  •  on
  •  when
  •  all
Answer: on


Popular Posts