Articles on Trending Technologies

Technical articles with clear explanations and examples

Download file through an AJAX call in PHP

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 2K+ Views

Using Ajax to download files is not considered to be a good idea. Instead, window.location = or document.location should be used.The 'window.location' has the following characteristics −JavaScript enabling is requiredIt doesn't need PHP.It helps show the content of the site, and redirects the user after a few seconds.Redirect can be dependent on any conditions such as −$success = 1 if ($success) {    window.location.href = 'http://example.com'; }A variable named ‘success’ is assigned with the value of 1. When this condition is satisfied, the window.location is used.On running, the user is redirected to the website 'http://example.com'

Read More

Validate Date in MySQL using a custom function

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 1K+ Views

Let us create a custom function to validate date in MySQL −mysql> set global log_bin_trust_function_creators=1; Query OK, 0 rows affected (0.03 sec) mysql> delimiter // mysql> create function isValidDate(actualDate varchar(255)) returns int    -> begin    -> declare flag int;    -> if (select length(date(actualDate)) IS NOT NULL ) then    -> set flag = 1;    -> else    -> set flag = 0;    -> end if;    -> return flag;    -> end    -> // Query OK, 0 rows affected (0.11 sec) mysql> delimiter ;Case 1 −When parameter is null value i.e. the date to be ...

Read More

Parse HTML with PHP's HTML DOMDocument

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 559 Views

The text inside a tag inside class="text" inside with class="main" can be obtained with the following code −Example$html = nodeValue)); }OutputThis will produce the following output −string ‘This is text 1’ (length=14) string ‘This is text 2' (length=14)

Read More

Display TRUE FALSE records as 0 1 in MySQL

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 566 Views

Set the column as BOOLEAN to display 0 and 1 values. Let us create a table −mysql> create table DemoTable2035    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> isMarried boolean,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2035(Name, isMarried) values('Chris', true); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2035(Name, isMarried) values('David', false); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable2035(Name, isMarried) values('Bob', true); Query OK, 1 ...

Read More

How can I get PHP to display the headers it received from a browser?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 209 Views

The below line of code can be used to display the header that the PHP code received via a browser −orExample$headers = getallheaders(); foreach($headers as $key=>$val){    echo $key . ': ' . $val . ''; }OutputThis will produce the following output −Host: www.websitename.com Content-Length: 180 Cache-Control: max-age=0 Origin: http://www.websitename.com Upgrade-Insecure-Requests: 1 Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, image/apng, */*;q=0.8, application/signed-exchange;v=b3;q=0.9 Referer: http://www.writephponline.com/ Accept-Encoding: gzip, deflate Accept-Language: en-GB, en-US;q=0.9, en;q=0.8 Cookie: _ga=GA1.2.118418268.1575911248; _gid=GA1.2.1794052867.1576852059

Read More

Implement Dynamic SQL query inside a MySQL stored procedure?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 4K+ Views

For dynamic SQL query in a stored procedure, use the concept of PREPARE STATEMENT. Let us first create a table −mysql> create table DemoTable2033    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2033(Name) values('Chris'); Query OK, 1 row affected (0.85 sec) mysql> insert into DemoTable2033(Name) values('Bob'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2033(Name) values('David'); Query OK, 1 row affected (0.24 sec) mysql> insert into ...

Read More

Get all the images from a folder in PHP

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 5K+ Views

The glob function can be used to get the images from a specific folder. Below is a sample code for the same −The path to the image folder is specified and all the files that have a .png extension are extracted. They are displayed with the help of the foreach loop −$dir_name = "path/to/image/folder/"; $images = glob($dir_name."*.png"); foreach($images as $image) {    echo ''; }Based on the folder that contains all images, the path of every image present inside the folder is returned.

Read More

PHP: Unlink All Files Within A Directory, and then Deleting That Directory

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 469 Views

Use glob to find all files matching a pattern.function recursive_directory_removal($directory) {    foreach(glob("{$directory}/*") as $file) {       if(is_dir($file)) {          recursive_directory_removal($file);       } else {          unlink($file);       }    }    rmdir($directory); }On PHP version 5.3 and above, the following code can be used −$dir = ... array_walk(glob($dir . '/*'), function ($fn) {    if (is_file($fn))    unlink($fn); }); unlink($dir);

Read More

What are the differences in die() and exit() in PHP?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 4K+ Views

There is no difference between die and exit, they are the same.The PHP Manual for exit states −"This language construct is equivalent to die()."The PHP Manual for die states −"This language construct is equivalent to exit()."However, there is a small difference, i.e the amount of time it takes for the parser to return the token.

Read More

Search for partial value match in an Array in PHP

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 1K+ Views

The array_filter function can be used to match a partial value in an array. A callback can be provided, that helps in deciding which elements would remain in the array and which would be removed.When the callback returns false, it means the given element needs to be removed. Below is a code example demonstrating the same −$arr = array(0 => 'abc', 1 => 'def', 2 => 'ghijk', 3 => 'lmnxyz'); $results = array(); foreach ($arr as $value) {    if (strpos($value, 'xyz') !== false) { $results[] = $value; } } if( empty($results) ) { echo 'No matches found.'; } else ...

Read More
Showing 51001–51010 of 61,248 articles
Advertisements