36 Reusable Code Snippet Topics
Remove Filter The Markdown editor that we use here at DaniWeb is called [CodeMirror](https://codemirror.net/) and we've been using it for forever. The other day, I noticed some bugs in the editor toolbar that we use, which is based on the [CodeMirror API](https://codemirror.net/docs/ref/). (The one that allows the buttons for bold, italic, etc.) … | |
DaniWeb is built on top of the Codeigniter 3.1.x PHP framework. Although I probably should have built it as a CI model, here is the database library that we are using. You can see it mainly serves as a wrapper for CodeIgniter's built-in database class. You can see we use … | |
Sometimes you need to delete duplicate rows in a database :) In the future, set UNIQUE keys when you need them :) In the example below, we have a table appropriately named `TABLE_NAME` and there are multiple rows that have the same value for the `duplicate_field` field. In this example, … | |
Here is the function that I use here at DaniWeb to manage flood control. It keeps track of how often a specific user or IP address is making a request, and increments a counter. If there have been no requests after 5 minutes, the counter resets to 0. It returns … | |
Sometimes we have a need to take a simple PHP array and represent it as an encoded string. One use case that we have for this at DaniWeb is when a new user signs up, and we ask them to click on a link that was sent to them via … | |
Sometimes we want to know if the webpage was fetched over an SSL connection (e.g. the URL begins with https:// instead of http://). This way, if an end-user is accessing an insecure version of our site, we can redirect them to the secure version. The following PHP function called `no_ssl()` … | |
For some reason, PHP class constants don't play nicely with inheritance. For example, suppose you have the following code: class Foo { const VAR = 'foo'; public function __construct() { echo self::VAR; } } class Bar extends Foo { const VAR = 'bar'; } // This will actually print out … | |
By default, PHP's clone keyword just creates a shallow copy of an object, and doesn't work as desired if the object contains properties that are also objects, because it doesn't create real copies of those objects but rather just pointers to the initial version of them. This function creates a … | |
A little while ago, I wrote a [tutorial](https://www.daniweb.com/programming/web-development/tutorials/537376/sanitize-php-user-input-strings) about how important it is to sanitize PHP user input strings. Not only is it important to sanitize user input being fed into a database query, but it's also important to sanitize user input being displayed to the end-user to generate valid … | |
If you need your PHP script to redirect to a different website, you can send an HTTP header to do that. Remember, `header()` must be called before any actual output is sent, which includes not just HTML, but blank lines, etc.. as well. | |
This is the code behind the demo at https://www.daniweb.com/connect/oauth/demo | |
You might be familiar with the dreaded blank page when your PHP script doesn't work. Here's how to spit out errors to the screen, instead of getting just a blank page, as well as logging errors to a file. | |
There are two ways to write to a file in PHP. You can either open a stream, and write to it in parts, or you can use `file_put_contents()` which is much more convenient, with the trade off being that it takes up much more memory, and is not ideal if … | |
We have a Swagger file for our API, but in our API documentation, we want to show valid responses for each endpoint. I coded up this little recursive function in PHP which takes our Swagger file and spits out a valid response. I use it as follows, for each individual … | |
| |
I recently had to mass insert a really large text file of strings into MySQL. Here's how I did it. | |
This is a program I just recently completed for a computer science course. Given a 2D grid, where every path between two gridpoints has a weight (path length) associated with it, this program computes the shortest path from (1,1) to (m,n). You are only allowed to move up and to … | |
This is a program I wrote for my x86 assembly class which is basically the Spade Invaders game in all its glory. It uses Irvine32.inc which came with the textbook. | |
I am often asked how to create a tableless two column CSS layout so I thought I would just post it here for once and for all. | |
This is a program I wrote for my x86 assembly class which generates matrices, multiplies them, and computes how long the arithmetic took. It uses Irvine32.inc which came with the textbook. | |
This is a program I wrote for my x86 assembly class which computes the sum of n numbers. It uses Irvine32.inc which came with the textbook. | |
[LEFT]This is a program I wrote for my x86 assembly class which revolves a "roulette wheel" around. Wait about a minute and it will eventually slow down to a stop. It uses Irvine32.inc which came with the textbook.[/LEFT] | |
This is a program I wrote for my x86 assembly class which bounces an ASCII ball across the screen. It uses Irvine32.inc which came with the textbook. | |
A simplistic program to compute the volume of a cylinder when its radius and length are input via MyInput.class | |
A simple demonstration of how MyInput.class can be used to input data from the keyboard. Computation is then done to compute the interest rate on a mortage. | |
A short example of passing variables between methods (functions). | |
An example of method overloading. The program calls a function to get the square root of a number. The appropriate function (integer or double version) is executed. | |
The JOptionPane class is used to give the CompMortgage.java program a front-end GUI interface. The class is divided into numerous functions. | |
A program similar to ComputeMortgage.java utilizing a mathematical formula and multiple methods to calculate a loan payment. | |
A single class consisting of multiple methods is used to demonstrate the robustness of keyboard input in Java. A try-catch block is used. The program graphically prints out a square root table by using JTextArea, JScrollPane and JOptionPane. | |
A demonstration of introductory object oriented programming. A class to define a circle object with radius, area, and perimeter parameters. | |
This program consists of two functions in one class. It is the driver program for the CircleClass.java file. | |
A program similar to CircleClass.java which defines a rectangle object. ClientProgram.java is the driver program for the Rectangle.java file. | |
Two classes which aim to demonstrate private and public members of a class. A static variable is used as an accumulator to keep track of the number of class objects which are created. | |
A program combining a switch statement with static variables in a class. Based on the switch statement, a class object is dynamically created. | |
This program consists of two functions in one class. It demonstrates a recursive function to calculate the factorial of a number. |
The End.