# Code Snippets

**URL:** https://www.phphelp.com/c/code-snippets/11.md

[Latest](https://www.phphelp.com/latest.md) · [Categories](https://www.phphelp.com/categories.md)

---

## [About the Code Snippets category](https://www.phphelp.com/t/about-the-code-snippets-category/8566)

<div class="topic-metadata">

**Author:** [@system](https://www.phphelp.com/u/system)\
**Replies:** 0\
**Last updated:** [July 16, 2018, 5:46am UTC](https://www.phphelp.com/t/about-the-code-snippets-category/8566 "2018-07-16T05:46:14Z")

</div>

Feel free to post and discuss any helpful Working PHP code snippets.

---

## [Vanilla JavaScript Gallery](https://www.phphelp.com/t/vanilla-javascript-gallery/35784)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 0\
**Last updated:** [October 28, 2023, 3:17am UTC](https://www.phphelp.com/t/vanilla-javascript-gallery/35784 "2023-10-28T03:17:24Z")

</div>

I wrote Vanilla JavaScript gallery using a class Gallery using manageable methods (functions) for the most part. Here is the JavaScript code - 'use strict'; /\* \* Photo Gallery 2.0 Created by John Pepp \* on October 27,…

---

## [I wan to retrieve the department name and role assigned](https://www.phphelp.com/t/i-wan-to-retrieve-the-department-name-and-role-assigned/35739)

<div class="topic-metadata">

**Author:** [@Usama](https://www.phphelp.com/u/Usama)\
**Replies:** 1\
**Last updated:** [September 30, 2023, 5:49pm UTC](https://www.phphelp.com/t/i-wan-to-retrieve-the-department-name-and-role-assigned/35739 "2023-09-30T17:49:01Z")

</div>

protected static function SendEmailForActivation( \\Admin $admin, string $activationLink, string $email) { // Below variables are used in email template. $adminName = $admin-\>displayName(); $activationUrl …

---

## [Doing a Search using JavaScript and PHP](https://www.phphelp.com/t/doing-a-search-using-javascript-and-php/35464)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 0\
**Last updated:** [May 26, 2023, 2:58pm UTC](https://www.phphelp.com/t/doing-a-search-using-javascript-and-php/35464 "2023-05-26T14:58:43Z")

</div>

Here is one way to do a search on a database table 'use strict'; //edit\_blog.js (function () { document.addEventListener("DOMContentLoaded", function () { const searchForm = document.getElementById("searchFo…

---

## [A simple login to keep user login](https://www.phphelp.com/t/a-simple-login-to-keep-user-login/35422)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 1\
**Last updated:** [May 6, 2023, 9:22am UTC](https://www.phphelp.com/t/a-simple-login-to-keep-user-login/35422 "2023-05-06T09:22:45Z")

</div>

A login page that keeps a user login in for a set period of time. Note it will only do it for one browser at a time as it uses a database table, for something fancier you will have to use multiple tokens in a database ta…

---

## [Set RGB Values for filter labels](https://www.phphelp.com/t/set-rgb-values-for-filter-labels/35124)

<div class="topic-metadata">

**Author:** [@dsizz0527](https://www.phphelp.com/u/dsizz0527)\
**Replies:** 0\
**Last updated:** [November 23, 2022, 7:38am UTC](https://www.phphelp.com/t/set-rgb-values-for-filter-labels/35124 "2022-11-23T07:38:00Z")

</div>

Hello! I have a plugin on my WordPress website used to filter and categorize the products in my store. The problem is that the labels to filter the color only give me the option to display the color name rather than an a…

---

## [I'm lost in how to compare values](https://www.phphelp.com/t/im-lost-in-how-to-compare-values/35005)

<div class="topic-metadata">

**Author:** [@jPaul](https://www.phphelp.com/u/jPaul)\
**Replies:** 1\
**Last updated:** [September 16, 2022, 11:11pm UTC](https://www.phphelp.com/t/im-lost-in-how-to-compare-values/35005 "2022-09-16T23:11:30Z")

</div>

Hi Everybody, I have a form, and to be fancy I wanted to include a simple security snippet: \<?php $firstnumb = rand(1,9); $secondnumb = rand(1,99); $entry = $firstnumb + $secondnumb; print $firstnumb + $secondnumb ?\> T…

---

## [PHP upgrade help for each() replacement](https://www.phphelp.com/t/php-upgrade-help-for-each-replacement/34957)

<div class="topic-metadata">

**Author:** [@smckinnis](https://www.phphelp.com/u/smckinnis)\
**Replies:** 2\
**Last updated:** [August 28, 2022, 8:17pm UTC](https://www.phphelp.com/t/php-upgrade-help-for-each-replacement/34957 "2022-08-28T20:17:54Z")

</div>

I am trying to upgrade php 7.4 to php 8.0. One popular code in my script is the each($array) line, now depreciated. I have most taken care of most but one is throwing me, should be simple. Most are like: while(list($…

---

## [Canvert Date to Mysql Format](https://www.phphelp.com/t/canvert-date-to-mysql-format/34964)

<div class="topic-metadata">

**Author:** [@conexor](https://www.phphelp.com/u/conexor)\
**Replies:** 1\
**Last updated:** [August 22, 2022, 11:01pm UTC](https://www.phphelp.com/t/canvert-date-to-mysql-format/34964 "2022-08-22T23:01:23Z")

</div>

Hi guys, Now I do confess to being a bit lazy here. I have a database where dates are stored in a varchar fields (not sure why). They are created using $date = date(“F j, Y, g:i a”); // May 31, 2006, 5:16 pm and the…

---

## [Smart redirect in .htaccess](https://www.phphelp.com/t/smart-redirect-in-htaccess/34538)

<div class="topic-metadata">

**Author:** [@RandalOulton](https://www.phphelp.com/u/RandalOulton)\
**Replies:** 3\
**Last updated:** [May 31, 2022, 1:14pm UTC](https://www.phphelp.com/t/smart-redirect-in-htaccess/34538 "2022-05-31T13:14:46Z")

</div>

Wondering if this is something that can be done in an .htaccess file – to code a “smart redirect” based on current month. THE BUSINESS NEED: At the start of each month, I have to manually go into Yoast plugin and set …

---

## [Create an array of holidays for any given year!](https://www.phphelp.com/t/create-an-array-of-holidays-for-any-given-year/24228)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 3\
**Last updated:** [November 24, 2021, 8:09pm UTC](https://www.phphelp.com/t/create-an-array-of-holidays-for-any-given-year/24228 "2021-11-24T20:09:21Z")

</div>

I wrote a small Class that gives the major holidays in an array using OOP obviously, for I am developing an online calendar and daily appointment booking. So I thought I share this little script to save people some time,…

---

## [PHP7 random number](https://www.phphelp.com/t/php7-random-number/34012)

<div class="topic-metadata">

**Author:** [@stowman](https://www.phphelp.com/u/stowman)\
**Replies:** 10\
**Last updated:** [September 30, 2021, 3:24pm UTC](https://www.phphelp.com/t/php7-random-number/34012 "2021-09-30T15:24:40Z")

</div>

I have the following snippet, using PHP7’s random\_bytes function: \<?php $bytes = bin2hex(random\_bytes(11)) ?\> \<?= $bytes ?\> It works and generates a 22-char string, changing to another string every time I refresh the …

---

## [intval](https://www.phphelp.com/t/intval/33923)

<div class="topic-metadata">

**Author:** [@mmnaderi](https://www.phphelp.com/u/mmnaderi)\
**Replies:** 1\
**Last updated:** [September 8, 2021, 1:25pm UTC](https://www.phphelp.com/t/intval/33923 "2021-09-08T13:25:00Z")

</div>

I have a variable($jdate\_post), and I want convert it’s value to some variables(Ex: $year, $month, $day…) by explode function. but when I want to convert these variables(Ex: $year) to integer, it returns 0. Help me, plea…

---

## [Update Functions](https://www.phphelp.com/t/update-functions/33863)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 0\
**Last updated:** [August 30, 2021, 3:17am UTC](https://www.phphelp.com/t/update-functions/33863 "2021-08-30T03:17:49Z")

</div>

The following procedural function might be useful for people who want to insert data by an array. /\* \* As long as you have the correct field names as the key and \* the correct values in the corresponding keys the foll…

---

## [Can someone tell me what this line does](https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822)

<div class="topic-metadata">

**Author:** [@AnselTk1](https://www.phphelp.com/u/AnselTk1)\
**Replies:** 3\
**Last updated:** [August 15, 2021, 7:29pm UTC](https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822 "2021-08-15T19:29:17Z")

</div>

Hi - working on moving some existing code in a site. Please forgive the PHP newbie question. I have this function: function cc\_remove\_cpt\_slug( $post\_link, $post, $leavename ) { if(in\_array($post-\>post\_type, array('to…

---

## [Need help getting random lines from file into array then display them from array](https://www.phphelp.com/t/need-help-getting-random-lines-from-file-into-array-then-display-them-from-array/33816)

<div class="topic-metadata">

**Author:** [@wiser3](https://www.phphelp.com/u/wiser3)\
**Replies:** 7\
**Last updated:** [August 13, 2021, 5:01am UTC](https://www.phphelp.com/t/need-help-getting-random-lines-from-file-into-array-then-display-them-from-array/33816 "2021-08-13T05:01:28Z")

</div>

Topic says what i’m trying to do. Didn’t think anything here was beyond my abilities, but i’ve been stuck on this for far too long. Thanks for your time and help. \<?php $gamesToPick = 5; $f\_contents = file("-games\_li…

---

## [Active Record Design Pattern](https://www.phphelp.com/t/active-record-design-pattern/32972)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 0\
**Last updated:** [December 28, 2020, 4:55am UTC](https://www.phphelp.com/t/active-record-design-pattern/32972 "2020-12-28T04:55:29Z")

</div>

I am finally wrapping my head around on how to utilize PHP Object-Oriented Programming better by using the Active Record Design Pattern: I know I have a ways to go and I promise not show you all the code when I am done.…

---

## [Need help, cannot figure this out](https://www.phphelp.com/t/need-help-cannot-figure-this-out/33022)

<div class="topic-metadata">

**Author:** [@Species8472](https://www.phphelp.com/u/Species8472)\
**Replies:** 1\
**Last updated:** [January 9, 2021, 11:14pm UTC](https://www.phphelp.com/t/need-help-cannot-figure-this-out/33022 "2021-01-09T23:14:04Z")

</div>

I just put together a server with nginx and rtmp so I could stream live from my own server. I, for the life of me, cannot figure out how to create a web page that shows the old videos and then be able to click on them an…

---

## [Display only upcoming events in Wordpress widget](https://www.phphelp.com/t/display-only-upcoming-events-in-wordpress-widget/32769)

<div class="topic-metadata">

**Author:** [@B3000](https://www.phphelp.com/u/B3000)\
**Replies:** 0\
**Last updated:** [November 7, 2020, 4:11pm UTC](https://www.phphelp.com/t/display-only-upcoming-events-in-wordpress-widget/32769 "2020-11-07T16:11:37Z")

</div>

Hi. I’d like to modify the below code to only display upcoming (younger than the current date) events from my “Custom Post Type Events” in a Wordpress widget. At the moment it shows one of all event entries randomly. Th…

---

## [Membership ID generation](https://www.phphelp.com/t/membership-id-generation/32300)

<div class="topic-metadata">

**Author:** [@dougdavis22](https://www.phphelp.com/u/dougdavis22)\
**Replies:** 3\
**Last updated:** [September 19, 2020, 4:46pm UTC](https://www.phphelp.com/t/membership-id-generation/32300 "2020-09-19T16:46:35Z")

</div>

Hi all. I have some code that was provided to me my a Wordpress plugin developer that generate membership IDs for members in a database. However, I need to change the format of the membership ID as I don’t want it to j…

---

## [How to mask EIN and SSN for display](https://www.phphelp.com/t/how-to-mask-ein-and-ssn-for-display/32524)

<div class="topic-metadata">

**Author:** [@rnrstar](https://www.phphelp.com/u/rnrstar)\
**Replies:** 2\
**Last updated:** [September 15, 2020, 9:13pm UTC](https://www.phphelp.com/t/how-to-mask-ein-and-ssn-for-display/32524 "2020-09-15T21:13:30Z")

</div>

I have a field that can contain an EIN or SSN but I want to mask it when I display them in the view. Here’s the code I used to do that. Basically I just count the number of dashes. If there is only one it’s an EIN and if…

---

## [Rename files by taking names from text files](https://www.phphelp.com/t/rename-files-by-taking-names-from-text-files/32205)

<div class="topic-metadata">

**Author:** [@Gis](https://www.phphelp.com/u/Gis)\
**Replies:** 2\
**Last updated:** [July 8, 2020, 8:26pm UTC](https://www.phphelp.com/t/rename-files-by-taking-names-from-text-files/32205 "2020-07-08T20:26:52Z")

</div>

Hi, I would like to change the names of the files in a directory with a script. example: I have a directory that contains 20 files: ‘City (1) .doc’ which must become ‘city - London.doc’ ‘City (2) .doc’ which must bec…

---

## [Can someone help me make 2 changes to my code?](https://www.phphelp.com/t/can-someone-help-me-make-2-changes-to-my-code/32178)

<div class="topic-metadata">

**Author:** [@stowman](https://www.phphelp.com/u/stowman)\
**Replies:** 1\
**Last updated:** [July 7, 2020, 4:28pm UTC](https://www.phphelp.com/t/can-someone-help-me-make-2-changes-to-my-code/32178 "2020-07-07T16:28:45Z")

</div>

I upload here a PHP file called singhloop.php which works, but I need 2 changes to it: (1) there is a variable $dir. Currently the “\<a href=” takes the user to a folder which includes the $dir in its path, I need that…

---

## [Unique result from two fields not working](https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104)

<div class="topic-metadata">

**Author:** [@Sailingmike49](https://www.phphelp.com/u/Sailingmike49)\
**Replies:** 3\
**Last updated:** [July 3, 2020, 5:42pm UTC](https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104 "2020-07-03T17:42:22Z")

</div>

I have a location field \[539\] and a date field \[663\] - latter receives combined result of two date fields and I need a unique result of the location field and combined date field. I’m using the code below but it doesnt a…

---

## [Fetch, JSON, Vanilla-JS and PHP](https://www.phphelp.com/t/fetch-json-vanilla-js-and-php/31879)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 5\
**Last updated:** [May 18, 2020, 1:29am UTC](https://www.phphelp.com/t/fetch-json-vanilla-js-and-php/31879 "2020-05-18T01:29:31Z")

</div>

I’m setting up a registration/login in system for my online trivia game and have come up with a dynamic way to check for username duplicates in a MySQL Database Table in real time. First the Vanilla-JS script: 'use str…

---

## [Reading a CSV text-based database into HTML](https://www.phphelp.com/t/reading-a-csv-text-based-database-into-html/31856)

<div class="topic-metadata">

**Author:** [@cokie69](https://www.phphelp.com/u/cokie69)\
**Replies:** 5\
**Last updated:** [May 14, 2020, 2:18pm UTC](https://www.phphelp.com/t/reading-a-csv-text-based-database-into-html/31856 "2020-05-14T14:18:20Z")

</div>

Hi, newbie here, Trying to reuse a free HTML template but I need to include a bit of source code and I have no clue how to do so. Can anyone help me? Got an external CSV file (data.txt) as a database: “img\_01.jpg”, “…

---

## [Button Click Counter](https://www.phphelp.com/t/button-click-counter/31852)

<div class="topic-metadata">

**Author:** [@20miles](https://www.phphelp.com/u/20miles)\
**Replies:** 2\
**Last updated:** [May 13, 2020, 6:17pm UTC](https://www.phphelp.com/t/button-click-counter/31852 "2020-05-13T18:17:39Z")

</div>

Hi anyone who can help. I’m new at using PHP. This is the very first thing I’m trying to do with PHP: creating a button that behaves like a facebook “like”, counting and displaying how many times the button has been cli…

---

## [Csrf token](https://www.phphelp.com/t/csrf-token/28945)

<div class="topic-metadata">

**Author:** [@johnphpnewb](https://www.phphelp.com/u/johnphpnewb)\
**Replies:** 5\
**Last updated:** [May 26, 2019, 11:54am UTC](https://www.phphelp.com/t/csrf-token/28945 "2019-05-26T11:54:54Z")

</div>

Hello, i often notice that members are asking for help with commerce apps in the beginners category. I find it strange to be creating shopping carts but offering no protection for the customers. I want to post a csrf tok…

---

## [A Class Snippet](https://www.phphelp.com/t/a-class-snippet/28776)

<div class="topic-metadata">

**Author:** [@Strider64](https://www.phphelp.com/u/Strider64)\
**Replies:** 0\
**Last updated:** [April 15, 2019, 9:38pm UTC](https://www.phphelp.com/t/a-class-snippet/28776 "2019-04-15T21:38:33Z")

</div>

The following is actually a couple of Class Snippets and a little CSS: namespace Library\\Calendar; use DateTime; use Library\\Database\\Database; use Library\\Holiday\\Holiday; class Calendar { protected $date = \\NUL…

---

## [Domain name Avaliability checker](https://www.phphelp.com/t/domain-name-avaliability-checker/27190)

<div class="topic-metadata">

**Author:** [@Amiya](https://www.phphelp.com/u/Amiya)\
**Replies:** 0\
**Last updated:** [September 19, 2018, 7:18am UTC](https://www.phphelp.com/t/domain-name-avaliability-checker/27190 "2018-09-19T07:18:59Z")

</div>

i want this type of snippet code to check is there domain is available or not? can anyone help me with this. image description: https://ibb.co/daGgre if i put any domain name it will show the domain is available or not…

[Next page](https://www.phphelp.com/c/code-snippets/11.md?page=1)
