PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: June 2015

Fibonacci series of given number in PHP

This blog describes about how to find Fibonacci series of given number in PHP. Fibonacci series is a sequence of number generated by adding previous two numbers. If the given number is 'n', then Factorial of 'n' is defined as 'Fn'. In mathematical terms, Fibonacci series for 'n' number looks like this:

Fn = Fn-1 + Fn-2;

Where,
F0 = 0;
F1 = 1;

Fibonacci series for 'n' number is calculated recursively by above formula.

Simple PHP program Fibonacci series:

This is the simple PHP program for find Fibonacci series.

function fibonacci_series(\$n) {
\$f1 = -1;
\$f2 = 1;

for (\$i = 1; \$i <= \$n; \$i++) {
\$f = \$f1 + \$f2;
\$f1 = \$f2;
\$f2 = \$f;
echo "\$f<br />";
}
}

echo fibonacci_series(5);

The above PHP program returns 0, 1, 1, 2, 3. As i said, Fibonacci series is a sequence of number generated by adding previous two numbers. In above program, we have generated Fibonacci series for 5.
We have assumed \$f1 = -1; and \$f2 = 1; We need to run for loop until 5.As per above program,

If \$i = 1:
\$f = -1 + 1 = 0;
\$f1 = 1;
\$f2 = 0;

If \$i = 2:
\$f = 1 + 0 = 1;
\$f1 = 0;
\$f2 = 1;

If \$i = 3:
\$f = 0 + 1 = 1;
\$f1 = 1;
\$f2 = 1;

If \$i = 4:
\$f = 1 + 1 = 2;
\$f1 = 1;
\$f2 = 2;

If \$i = 5:
\$f = 1 + 2 = 3;
\$f1 = 2;
\$f2 = 3;

Fibonacci series for 'n' number in PHP:

The below PHP program is used to find Fibonacci series of given number.

<html>
<style type="text/css">
body {
color:white;
font-size:14px;
}
.contact {
text-align:center;
background: none repeat scroll 0% 0% #8FBF73;
width:510px;
}
#number {
width: 250px;
margin-bottom: 15px;
background: none repeat scroll 0% 0% #AFCF9C;
border: 1px solid #91B57C;
height: 30px;
color: #808080;
}
#submit
{
background:none repeat scroll 0% 0% #8FCB73;
display: inline-block;
line-height: 1.05em;
border: 1px solid #8FCB73;
text-decoration: none;
opacity: 0.9;
cursor: pointer;
color:white;
}
#er {
color: #F00;
text-align: center;
margin: 10px 0px;
font-size: 17px;
}
</style>
<body>
<div class="contact">
<h1>Fibonacci series in PHP</h1>
<form action="#" method="POST">
Enter number : <input type="text" name="number" id="number" /></br>
<input  type="submit" name="submit" id="submit" value="Submit"></input>
</form>

<?php
if(isset(\$_POST['submit'])) {
\$n = \$_POST['number'];

function fibonacci_series(\$a) {
\$f1 = -1;
\$f2 = 1;

for (\$i = 1; \$i <= \$a; \$i++) {
\$f = \$f1 + \$f2;
\$f1 = \$f2;
\$f2 = \$f;
echo "\$f<br />";
}
}

echo "Fibonacci series of \$n is:<br />";
echo fibonacci_series(\$n);

}

?>
</div>
</body>
</html>

When you open above PHP file in browser, it looks like below image:

If you type number 6 in textbox, it returns 0, 1, 1, 2, 3, 5. It looks like below image:

Now you can find Fibonacci series for any number in PHP. I've hope, you got an idea for generate Fibonacci series of given number.

Factorial of given number in PHP

This blog describes about how to find factorial of given number in PHP. The factorial is one of the maths concept. It is multiplication of numbers from 1 to given number. If the given number defined as 'n', then 'n' factorial represented as 'n!'. The factorial formula for 'n' number looks like:

n! = 1 * 2 * 3 ... n;

For example, the factorial of 5 looks like this:

5! = 1 * 2 * 3 * 4* 5  = 120;

Simple PHP program for Factorial:

Consider the below example which is the sample PHP program for find factorial of number:

function factorial(\$a) {
\$fact = 1;
for(\$i = 1; \$i <= \$a; \$i++) {
\$fact = \$fact * \$i;
}
return \$fact;
}
\$f = factorial(4);
echo \$f;

Where,
'n' is 4.
n! = 4! = 1 * 2 * 3 * 4 = 24.

Factorial for 'n' number in PHP:

Consider the below example which is the PHP program for find factorial of given number. For example, if you enter 4, it will return factorial of 4. Like if you enter 'n' number, it will return factorial of 'n'.

<html>
<style type="text/css">
body {
color:white;
font-size:14px;
}
.contact {
text-align:center;
background: none repeat scroll 0% 0% #8FBF73;
width:520px;
}
#number {
width: 250px;
margin-bottom: 15px;
background: none repeat scroll 0% 0% #AFCF9C;
border: 1px solid #91B57C;
height: 30px;
color: #808080;
}
#submit
{
background:none repeat scroll 0% 0% #8FCB73;
display: inline-block;
line-height: 1.05em;
border: 1px solid #8FCB73;
text-decoration: none;
opacity: 0.9;
cursor: pointer;
color:white;
}
#er {
color: #F00;
text-align: center;
margin: 10px 0px;
font-size: 17px;
}
</style>
<body>
<div class="contact">
<h1>Factorial in PHP</h1>
<form action="#" method="POST">
Enter number : <input type="text" name="number" id="number" /></br>
<input  type="submit" name="submit" id="submit" value="Submit"></input>
</form>

<?php
if(isset(\$_POST['submit'])) {
\$n = \$_POST['number'];

function factorial(\$a) {
\$fact = 1;
for(\$i = 1; \$i <= \$a; \$i++) {
\$fact = \$fact * \$i;
}
return \$fact;
}
\$f = factorial(\$n);
echo "<div class=''>Factorial of \$n is \$f</div>";
}
?>
</div>
</body>
</html>

When you open above program in browser, it looks like this:

Whatever you enter number in the textbox, it returns factorial of given number.

Ajax upload file or image in PHP using jQuery

You can upload files and images using PHP. Basically it can be uploaded by form submitting. For more details visit how to upload file or image using PHP. When i tried to upload images using Ajax with jQuery, i can't get it work. All of you may be face this problem. This blog describes how to upload file or image in PHP using jQuery with Ajax.

Consider the following example for upload files using Ajax and PHP.

To upload file or image, your html form should be like below form:

<form name="multiform" id="multiform" action="#" method="POST" enctype="multipart/form-data">
Image : <input type="file" name="file" id="file" /></br>
</form>

where,
enctype="multpart/form-data  -  must be present in form. Otherwise file does not upload.
type="file" in <input>  tag is used to take input as a file.

Ajax and jQuery for upload image:

Before add Ajax code to your html file, you need to add jquery-1.7.2.min.js library file. To upload image, you have to write Ajax code like below:

\$("#multiform").submit(function(e) {
var formObj = \$(this);

if(window.FormData !== undefined) {
var formData = new FormData(this);
\$.ajax({
url: 'upload.php', //url to process the sent data
type: 'POST',     //form type
data:  formData, //form data to send
mimeType:"multipart/form-data",  //type of form data
contentType: false,  //content type of form
cache: false,  //disable cache the request page
processData: false,  //to send DOMDcoument or non processed data file
success: function(data, textStatus, jqXHR) {
\$("#er").html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
\$("#er").html('textStatus=' + textStatus + ', errorThrown=' + errorThrown);
}
});

//reset file field after form submitted
\$('#file').val('')
//prevent the page is refreshed
e.preventDefault();
//e.unbind();
}
});

\$("#multi-post").click(function() {
//submit the form and send data
\$("#multiform").submit();
});

});

When user click the upload button, the form is submitted, ajax is called and send form data as well.

Html file with Ajax for upload image:

We need to combine html form and ajax code to make a full html file for upload file or image. You may add some styles to html file in order to give attractive presence to users. After combined above all things, your html code should be like below one:

<html>
<style type="text/css">
body {
color:white;
font-size:14px;
}
.contact {
text-align:center;
background: none repeat scroll 0% 0% #8FBF73;
width:520px;
}
#file {
width: 250px;
margin-bottom: 15px;
background: none repeat scroll 0% 0% #AFCF9C;
border: 1px solid #91B57C;
height: 30px;
color: #808080;
}
#multi-post
{
background:none repeat scroll 0% 0% #8FCB73;
display: inline-block;
line-height: 1.05em;
border: 1px solid #8FCB73;
text-decoration: none;
opacity: 0.9;
cursor: pointer;
color:white;
}
#er {
color: #F00;
text-align: center;
margin: 10px 0px;
font-size: 17px;
}
</style>
<body>
<script type="text/javascript" src="../test/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
\$("#multiform").submit(function(e) {
var formObj = \$(this);

if(window.FormData !== undefined) {
var formData = new FormData(this);
\$.ajax({
type: 'POST',
data:  formData,
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
success: function(data, textStatus, jqXHR) {
\$("#er").html('<pre><code>'+data+'</code></pre>');
},
error: function(jqXHR, textStatus, errorThrown) {
\$("#er").html('textStatus=' + textStatus + ', errorThrown=' + errorThrown);
}
});

\$('#file').val('')
e.preventDefault();
//e.unbind();
}
});

\$("#multi-post").click(function() {
//sending form from here
\$("#multiform").submit();
console.log();
});

});

</script>
<div class="contact">
<h1>Upload files using Ajax and PHP</h1>
<div id="er"></div>
<form name="multiform" id="multiform" action="#" method="POST" enctype="multipart/form-data">
Image : <input type="file" name="file" id="file" /></br>
</form>
</div>
</body>
</html>

When you view the upload-ajax.html in browser, it should be like below image:

When ajax is called, url which is in ajax call received form data. Then the file or image is to be uploaded using following PHP code.

<?php
\$file=\$_FILES['file']['name'];
if(\$file!='')
{
//check whether file is exists or not in a folder
}
else {
}
}

Where,
file_exists() - used to check whether the file is already exists in a folder or not after upload the file
\$_SERVER['DOCUMENT_ROOT'] - used to represent the directory

Now you can add either file or image using PHP and jQuery with Ajax.

1 Jun 2015

You can upload single file and image easily in PHP. Now we are going to know how to upload multiple files and images using PHP. You can upload more than one images or files at the time using PHP. It can done using by array concepts.

First you need to make form for upload more images. Your form should be like following:

<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" id="file" multiple="multiple">
<input type="file" name="file[]" id="file" multiple="multiple">
<input type="submit" id="submit" name="submit" value="submit">
</form>

You should add enctype="multipart/form-data" for upload files. You can see above where the name of file tag should be array like name="file[]" for store multiple values.

Similarly upload single image, you will get image values by \$_FILES['file']['name'] in PHP while submitting a form. You need temporary name of image to pass one of the parameter in move_uploaded_file() in order to upload files. If you've a single image, then you can upload easily by move_uploaded_file(\$_FILES['file']['tmp_name'],\$name). But you need to upload multiple images. So you've to get values as an array. Then upload each image using loop. Consider the following example. Here it describes how to get temporary name of each image one by one using loop in PHP.

<?php
if(isset(\$_POST['submit']))
{
\$name=\$_FILES['file']['name'];
foreach(\$name as \$val => \$name1)
{
\$file_name=\$name1;
\$tmp=\$_FILES['file']['tmp_name'][\$val];
}
}

?>
<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<table>
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr>
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr>
<tr><td><input type="submit" id="submit" name="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>

Where,