New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home Blog Page 4

How to edit/update a txt file with php

0

Welcome back to shortlearner.com, in this post we will see how to update a txt file with the help of PHP.
So in this post we learn how to fetch data from the file and show in the text area and when user changes in it and hit on the update button file will update.

update txt file php


there is a myfile.txt file in my directory and in the below code i am fetching data from myfile.txt with the help of PHP and showing the data into text area.

Method 1:

in this method we fetch data from PHP file with the help of loop.

<?php
$file = file("myfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
} 
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
?>

Method 2: file_get_contents

In this method we used PHP predefined function file_get_contents.
this function is use for get the contents of a file and store the content into a variable.
if we need to store content into a file than we also use file_put_contents which is works just opposite to file_get_contents funcion.

<?php
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
echo $text = file_get_contents('myfile.txt');
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
?>

after showing the file data into text area we will update the data into file. so when user click on the update button the files will get open again and with the help of write method we writes the data into file and close the file.
here is the complete code.

<?php
if($_POST['Submit']){
$open = fopen("myfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />"; 
echo "File:<br />";
$file = file("myfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
} 
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

If these article helps you please share it with your developers buddy.
and if you are a web designer and looking for amazing snippets thank click on the below button.

Amazing Snippets

Keep Learning, Keep Coding

how to store data in cache file in php

0

welcome back to shortlearner.com, in this post we will see how to store data in cache file with the help of PHP.
before start this tutorial we should take an overview about cache.
so the very first question in our mind is what is cache , why we store our data in cache file, and what are the benefits to store data in this file.

data caching in php

so basically data caching refers to storing the result of an operation into a file so that future request return faster or we can say that caching is a perfect way to showing repeated data with minimum execution time.

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

in the below code we are creating a cache file with the help of php script and store data into that file and match the execution time of both actions.

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<?php
$start = microtime(true);
$con  = mysqli_connect("localhost","root","rootroot","testdb");
$cache_file = "cache/index.cache.php";
if(file_exists($cache_file))
{
echo "data fetch from cache file";
	include($cache_file);
}
else
{
	echo "data not file";
	$city = '';
	$find = $con->query("SELECT * FROM cities");
	while($fetch= $find->fetch_assoc())
	{
	 $city .= $fetch['city'];
	}
	$handle =fopen($cache_file, 'w');
	{
          fwrite($handle,$city);
	  fclose($handle);
	}
}
$end = microtime(true);
echo round($end-$start,4);
 
?>
</body>
</html>

so i have a table in MySQL database with the name of cities and this table have thousands of record in it. so whenever i use select query to fetch all records from the database it takes some time to execution which is more than 5 seconds. i want to reduce this execution time from 5 seconds to 0.005 seconds.

here i am using microtime() which is store the time before the script is start for execution. than we declare a variable of $cache_file and check that the file is exist or not.

so in the very first time this is not exist so we fetch all records from database and store in our $cache_file by using predefined file functions.
now when we perform the same task again it will fetch records from our $cache_file file which will exist this time.

and after the end we store execution time again in our $end variable and than check the difference between start and end time of execution so we got the actual execution time of this script.

how to get data from api using curl in php

0

Welcome back to shortlearner.com, In this post we will see how to fetch record from a website by using cURL API. so before start this tutorial we should take an small overview of it, what is it , how does it works and its benefites.

PHP cURL tutorial

What is cURL

so basically cURL means client URL which allows us to connect with other URLs and use their responses in our code, and it is a library that allows to make HTTP requests in PHP.
in order to use PHP’s cURL functions we need to install the libcurl package in our server.
PHP also requires that we use libcurl 7.02-beta and higher and it is works from PHP version 4.3.0 or higher.
in easy way cURL is a way to hit an URL from our code to get a html response from it.

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

Now we understand the uses of cURL with a real life example. suppose you are a owner of 5 luxuries hotel and all are registered in some kind of OTA (online traveling agent) like make my trip, goibibo and travel guru.
you want to manage all the inventory and rates on their platforms according to season.

in this case you have perform this task in 2 ways. first and difficult one is you go to each and every OTA’s dashboard and manage inventory or rates or you can try to develop a platform to manage all the hotels by using API.

so if you have a knowledge of cURL funcationlity you will manage it easily by hitting client’s URL.

In the below code we fetch the record from jsonplaceholder.typicode.com/users by using cURL functionality and print it on our webpage.

<?php 
$ch = curl_init( );
curl_setopt($ch, CURLOPT_URL, "http://jsonplaceholder.typicode.com/users");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
            curl_close($ch); 
$response = json_decode($result);
//echo "<pre>";print_r($response);echo"</pre>";
foreach ($response as $key => $row) {
	echo $row->name."<br>";
}
?>

first of all we use curl_init() function, this function is use for initializes a new session.
curl_setopt() functions is ues for set URL.
CURLOPT_RETURNTRANSFER that will return the transfer as a string of the return value of curl_exec().
and curl_close() that is using for end the session.

how to create social media share buttons

0

In this post we learn how to create social media share buttons with the help of font awesome icons , bootstrap and CSS and learn how to deploy it for any website.


so before start this tutorial we should take an small overview about fontawesome and learn the implementation process of icons.

we are living in the era of digital. we have a lots of digital platform like facebook, twitter, instagram, linkedin, whatsapp, google plus and many more.

Today most of the business and companies are holding their social account. so they can reach to their customers or client base via sharing some important and relatable contents according to their niche, and sharing all the offers and information over there.

most of the developers are uses small images of social media platform or uses favicons of also .fontawesome and glyphicons are the two third party icon providers which have a good library of favicons and easy to implement.

If we want to share this article with different type of social media platforms so this article with reach more readers than i use this .

In the below code we will learn how to add share button to website. so just copy the code and paste it to your website.

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.css" rel="stylesheet">
<!-- Include the above in your HEAD tag -->
<div class="container-fluid" id="cont">
	<div class="row">
		<div class="col-sm-12">
		    <div class="middle">
                <a class="btn2" href="http://www.facebook.com/sharer.php?u=<?=$site_url?>">
                    <i class="fab fa-facebook-f"></i>
                </a>

                <a class="btn2" href="https://twitter.com/share?url=<?=$site_url?>&amp;text=Simple%20Share%20Buttons&amp;hashtags=shortlearner">
                    <i class="fab fa-twitter"></i>
                </a>
<a class="btn2" href="https://plus.google.com/share?url=<?=$site_url?>">
                    <i class="fab fa-google"></i>
                </a>

<a href="whatsapp://send?text=The text to share!" data-action="share/whatsapp/share"> <i class="fab fa-whatsapp"></i></a>

  </div>
		</div>
	</div>
</div>

now we are adding some CSS to creating our button responsive and attractive. just copy the below CSS part and paste it in the head section of your html code.

#cont {
    background-color: #002147;
    padding: 100px;
}

.middle {
    text-align: center;
}
.btn2 {
	display: inline-block;
	width: 50px;
	height: 50px;
	background: #f1f1f1;
	margin: 10px;
	border-radius: 30%;
	box-shadow: 0 5px 15px -5px #ecf0f1;
	color: #3498db;
	overflow: hidden;
	position: relative;
}

a.btn2:hover {
	transform: scale(1.3);
	color: #f1f1f1;
}

.btn2 i {
	line-height: 50px;
	font-size: 20px;
	transition: 0.2s linear;
}

.btn2:hover i {
	transform: scale(1.3);
	color: #f1f1f1;
}

.fa-facebook-f {
    color: #3c5a99;
}

.fa-twitter {
    color: #1da1f2;
}
.fa-google {
    color: #dd4b39;
}


.btn2::before {
	content: "";
	position: absolute;
	width: 120%;
	height: 120%;
	background: #3498db;
	transform: rotate(45deg);
	left: -110%;
	top: 90%;
}

.btn2:hover::before {
	animation: aaa 0.7s 1;
	top: -10%;
	left: -10%;
}

@keyframes aaa {
	0% {
		left: -110%;
		top: 90%;
	}
	50% {
		left: 10%;
		top: -30%;
	}
	100% {
		left: -10%;
		top: -10%;
	}
}

this is the customization of social network sharing buttons. if you are trying to add on WordPress website than there are some best plugins that will help you.

WordPress to Buffer
Simple Social Icons
Social Icons Widget by WPZoom
WordPress Social Login
Revive Old Posts
AddtoAny

hope this article will helps you. please share this article with all your developer buddies and help them.

Preview of image before upload using jQuery

0

today in this post we will learn how to Preview of image immediately after file is selected in file upload using jQuery. In our previous post we learn how to convert a number into words.
Most of the developer use this feature in their panel and it is a good practice. A good developer always thinks about his user and he provide a user friendly UI to his clients.

image preview using jquery

To begin with this tutorial we will take an short overview of jQuery and its useful features.

Why is Used in jQuery.

jQuery is a open source and light weight library of Java script, with the help of jQuery we can develop our website or software more interactive and attractive.
we can also handle such difficult task like event handling, AJAX and animation easily with the help of jQuery.

Add Minified Jquery file

so first of all we are add jquerymin.js and jquery-ui.min.js in the head section of html code.
after adding the jQuery files we are adding and input type file which will takes the file from our local system.

after selecting the file we will call an onchange function which will takes the file and read the file and preview the image file with the help of id.
just copy the below code and run it.

<script type="text/javascript">
		function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#viewfile')
                        .attr('src', e.target.result)
                        .width(150)
                        .height(200);
                };

                reader.readAsDataURL(input.files[0]);
            }
        }
	</script>

PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

show image Complete code

<!DOCTYPE html>
<html>
<head>
<script  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script  src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>Shortlearner Jquery Tutorial</title>
</head>
<body>
	<script type="text/javascript">
		function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#viewfile')
                        .attr('src', e.target.result)
                        .width(150)
                        .height(200);
                };

                reader.readAsDataURL(input.files[0]);
            }
        }
	</script>
  <input type='file' onchange="readURL(this);" />
    <img id="viewfile" src="#"  />
</body>
</html>

after run the above code we will see the Preview of our image before upload with the help of jQuery.

if this tutorial helps you please share it with your developer buddies, and if you want to add something new method than contact us we will updated your methods too.
keep learning ,keep coding