Tuesday 9 January 2018

How to upload multiple image in php

Step : (1) Create index.phpfile paste the code .

<!DOCTYPE html>
<html>
<head>
<title>Upload Multiple Images Using jquery and PHP</title>
<!-------Including jQuery from Google ------>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="script.js"></script>
<!------- Including CSS File ------>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<div id="maindiv">
<div id="formdiv">
<h2>Multiple Image Upload Form</h2>
<form enctype="multipart/form-data" action="" method="post">
First Field is Compulsory. Only JPEG,PNG,JPG Type Image Uploaded. Image Size Should Be Less Than 100KB.
<div id="filediv"><input name="file[]" type="file" id="file"/></div>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>
<!------- Including PHP Script here ------>
<?php include "manjeet.php"; ?>
</div>
</div>
</body>

</html>


Step : (2) Create manjeet.js file paste the code .

var abc = 0;     
$(document).ready(function() {

$('#add_more').click(function() {
$(this).before($("<div/>", {
id: 'filediv'
}).fadeIn('slow').append($("<input/>", {
name: 'file[]',
type: 'file',
id: 'file'
}), $("<br/><br/>")));
});

$('body').on('change', '#file', function() {
if (this.files && this.files[0]) {
abc += 1; // Incrementing global variable by 1.
var z = abc - 1;
var x = $(this).parent().find('#previewimg' + z).remove();
$(this).before("<div id='abcd" + abc + "' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
$(this).hide();
$("#abcd" + abc).append($("<img/>", {
id: 'img',
src: 'x.png',
alt: 'delete'
}).click(function() {
$(this).parent().parent().remove();
}));
}
});

function imageIsLoaded(e) {
$('#previewimg' + abc).attr('src', e.target.result);
};
$('#upload').click(function(e) {
var name = $(":file").val();
if (!name) {
alert("First Image Must Be Selected");
e.preventDefault();
}
});

});

Step : (3) Create manjeet.css file paste the code .

form{
background-color:#fff
}
#maindiv{
width:960px;
margin:10px auto;
padding:10px;
font-family:'Droid Sans',sans-serif
}
#formdiv{
width:500px;
float:left;
text-align:center
}
form{
padding:40px 20px;
box-shadow:0 0 10px;
border-radius:2px
}
h2{
margin-left:30px
}
.upload{
background-color:red;
border:1px solid red;
color:#fff;
border-radius:5px;
padding:10px;
text-shadow:1px 1px 0 green;
box-shadow:2px 2px 15px rgba(0,0,0,.75)
}
.upload:hover{
cursor:pointer;
background:#c20b0b;
border:1px solid #c20b0b;
box-shadow:0 0 5px rgba(0,0,0,.75)
}
#file{
color:green;
padding:5px;
border:1px dashed #123456;
background-color:#f9ffe5
}
#upload{
margin-left:45px
}
#noerror{
color:green;
text-align:left
}
#error{
color:red;
text-align:left
}
#img{
width:17px;
border:none;
height:17px;
margin-left:-20px;
margin-bottom:91px
}
.abcd{
text-align:center
}
.abcd img{
height:100px;
width:100px;
padding:5px;
border:1px solid #e8debd
}
b{
color:red

}


Step : (4) Create manjeet.php file paste the code .

<?php
if (isset($_POST['submit'])) {
$j = 0;     // Variable for indexing uploaded image.
$target_path = "/sumit/";     // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i]));   // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
$j = $j + 1;      // Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 100000)     // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
// If file moved to uploads folder.
echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
} else {     //  If File Was Not Moved.
echo $j. ').<span id="error">please try again!.</span><br/><br/>';
}
} else {     //   If File Size And File Type Was Incorrect.
echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}

?>

Sunday 17 December 2017

Disable Admin Bar in wordpress.

Paste this code in your theme’s functions.php file


add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(false);
}
}

If you want to disable it for all users, then simply put use this code in your theme’s functions.php file



  show_admin_bar(false);

Sunday 30 July 2017

Use htmlentities with the correct characterset option

1
$value = htmlentities($this->value , ENT_QUOTES , 'UTF-8');

Difference Between Comparison Operators in php.

If you use strpos() to determine whether a substring exists within a string (it returns FALSE

<?php
 
$authors = 'Chris & Sean';
 
if (strpos($authors, 'Chris')) {
    echo 'Chris is an author.';
} else {
    echo 'Chris is not an author.';
}
 
?>

Friday 28 July 2017

Logical Operators Example

   <?php
         $a = 42;
         $b = 0;
         
         if( $a && $b ) {
            echo "TEST1 : Both a and b are true<br/>";
         }else{
            echo "TEST1 : Either a or b is false<br/>";
         }
         
         if( $a and $b ) {
            echo "TEST2 : Both a and b are true<br/>";
         }else{
            echo "TEST2 : Either a or b is false<br/>";
         }
         
         if( $a || $b ) {
            echo "TEST3 : Either a or b is true<br/>";
         }else{
            echo "TEST3 : Both a and b are false<br/>";
         }
         
         if( $a or $b ) {
            echo "TEST4 : Either a or b is true<br/>";
         }else {
            echo "TEST4 : Both a and b are false<br/>";
         }
         
         $a = 10;
         $b = 20;
         
         if( $a ) {
            echo "TEST5 : a is true <br/>";
         }else {
            echo "TEST5 : a  is false<br/>";
         }
         
         if( $b ) {
            echo "TEST6 : b is true <br/>";
         }else {
            echo "TEST6 : b  is false<br/>";
         }
         
         if( !$a ) {
            echo "TEST7 : a is true <br/>";
         }else {
            echo "TEST7 : a  is false<br/>";
         }
         
         if( !$b ) {
            echo "TEST8 : b is true <br/>";
         }else {
            echo "TEST8 : b  is false<br/>";
         }
      ?>
 
This will produce the following result −


TEST1 : Either a or b is false
TEST2 : Either a or b is false
TEST3 : Either a or b is true
TEST4 : Either a or b is true
TEST5 : a is true
TEST6 : b is true
TEST7 : a is false
TEST8 : b is false
 

How to print following number table:

How to print following number table:




<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">  
   <!-- cell 270px wide (8 columns x 60px) -->  
    <?php  
        //$a = 1;
      for($row=1;$row<=10;$row++)  
      {  
          echo "<tr>";
         
          for($col=1;$col<=10;$col++)  
          {  
              echo "<td>" .($col * $row). "</td>";
          }
          //$a++;
          echo "</tr>";
      }
      ?>
 </table>

Write a program to make a chess:

Write a program to make a chess:












  <table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
   <!-- cell 270px wide (8 columns x 60px) -->  
      <?php  
      for($row=1;$row<=8;$row++)  
      {  
          echo "<tr>";  
          for($col=1;$col<=8;$col++)  
          {  
          $total=$row+$col;  
          if($total%2==0)  
          {  
          echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";  
          }  
          else  
          {  
          echo "<td height=30px width=30px bgcolor=#000000></td>";  
          }  
          }  
          echo "</tr>";  
    }  
          ?>  
  </table>  

Program to print below format...

(1) Program to print below format
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *



<?php

   $rows=5; // number of row
      for($i=1;$i<=$rows;$i++)
        {
 
           for($j=1;$j<=$i;$j++)
              {
                echo "*";
              }
            echo "<br />";
        }




?> 





(2 ) Write a program to print the below format :
1 5 9
2 6 10
3 7 11
4 8 12



<?php

for($i=1;$i<=4;$i++)
{
 $i1=$i+4;
 $i2=$i+8;
echo $i." ".$i1." ".$i2;
echo "<br />";
}


?>
 
 
(3) Write a program for this Pattern:
 
*****
*      *
*      *
*      *
*****
 
<?php
for($i = 1; $i<=5; $i++){
            for($j = 1; $j<=5; $j++){
               if($i == 1 || $i == 5){
                   echo "*";
               }
               else if($j == 1 || $j == 5){
                   echo "*";
               }
               else {
                   echo "&nbsp;&nbsp;";
               }
               
            }
            echo "<br/>";
}
?>
 

 
 
 
 
 
 


Program to print below format..

Program to print below format.
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1





<?php
   $rows=5;  // number of row
   for($i=$rows;$i>=1;--$i)
     {
         for($j=1;$j<=$i;++$j)
         {
            echo $j;
         }
     echo "<br />";
     }
 ?>
 

Program to print the below format.

Program to print the below format
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *



<?php


   $rows=5;  // number of row enter.
   for($i=$rows;$i>=1;--$i)
     {
         for($space=0;$space<$rows-$i;++$space)
            echo "  ";
         for($j=$i;$j<=2*$i-1;++$j)
           echo "* ";
         for($j=0;$j<$i-1;++$j)
             echo "* ";
         echo "<br />";
     }



?>