DeveloperBreeze

Checking Internet Connection Status in JavaScript

function checkInternetConnection() {
    const online = navigator.onLine;
    return online;
}
console.log('Internet Connection Status:', checkInternetConnection());

Continue Reading

Discover more amazing content handpicked just for you

Code

How to Create a New MySQL User with Full Privileges

No preview available for this content.

May 01, 2025
Read More
Code
python

Configuring SQLAlchemy with PostgreSQL on Heroku: A Quick Guide

Instead of manually replacing the URI, you can use libraries like dj-database-url to parse and adapt the DATABASE_URL for different frameworks or drivers. However, the manual approach is straightforward and works well for most cases.

Nov 08, 2024
Read More
Code
php

How to Delete All WordPress Posts and Their Uploads Using a Custom PHP Script

You can loop through all posts, delete each post, and then remove any associated media files (attachments). Here's a complete script to achieve this:

<?php
// Load WordPress environment
require_once('/path/to/your/wp-load.php');

// Get all posts (any post type)
$args = array(
    'post_type'      => 'any', // 'any' retrieves all post types (posts, pages, custom post types)
    'post_status'    => 'any', // 'any' retrieves all post statuses
    'posts_per_page' => -1,    // Retrieve all posts
);

$all_posts = get_posts($args);

foreach ($all_posts as $post) {
    // Get the post ID
    $post_id = $post->ID;

    // Check if the post has attachments (media files)
    $attachments = get_attached_media('', $post_id);

    // Delete each attachment associated with the post
    foreach ($attachments as $attachment) {
        $attachment_id = $attachment->ID;
        // This deletes the file from the uploads directory and the database record
        wp_delete_attachment($attachment_id, true);
    }

    // Delete the post itself
    wp_delete_post($post_id, true); // true = force delete, bypass trash
}

echo 'All posts and their uploads have been deleted.';

Oct 25, 2024
Read More
Code
php

How To enable all error debugging in PHP

No preview available for this content.

Oct 25, 2024
Read More
Code
bash

How to Paste in an SSH Terminal Without a Mouse

No preview available for this content.

Oct 20, 2024
Read More
Note
javascript nodejs

Vite vs Webpack

  • Vite: Vite has a growing ecosystem with plugins, and it's designed to be framework-agnostic, although it has strong support for Vue and React.
  • Webpack: Webpack has a mature ecosystem with a vast array of plugins and integrations, making it suitable for complex and large-scale projects.

In summary, while both Vite and Webpack serve similar purposes in managing and bundling assets for web applications, Vite is often preferred for its speed and simplicity, particularly in development, whereas Webpack offers more customization and flexibility, making it a strong choice for complex builds.

Aug 14, 2024
Read More
Code
nodejs graphql

GraphQL API Server with Node.js and Apollo Server

Run the server using Node.js:

   node index.js

Aug 12, 2024
Read More
Code
javascript

React Custom Hook for API Requests

  • Reusability: The useFetch hook can be used across different components and applications, reducing code duplication and simplifying API interaction.
  • Loading and Error States: Automatically manages loading and error states, providing a consistent way to handle asynchronous operations.
  • Cleanup Handling: Prevents state updates on unmounted components, reducing potential memory leaks and ensuring stability.
  • Custom Headers: Extend the hook to accept custom headers or authentication tokens in the options parameter.
  • Polling: Implement a polling mechanism by setting up a setInterval within the useEffect for periodically fetching data.
  • Data Transformation: Add a callback function to transform the fetched data before setting it in state.

Aug 12, 2024
Read More
Code
csharp

Unity Inventory System using Scriptable Objects

No preview available for this content.

Aug 12, 2024
Read More
Code
csharp

Unity Player Controller Blueprint

using UnityEngine;

// Basic player controller for Unity
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    [Header("Player Settings")]
    public float moveSpeed = 5f;
    public float jumpHeight = 2f;
    public float gravity = -9.81f;
    public Transform cameraTransform;

    private CharacterController characterController;
    private Vector3 velocity;
    private bool isGrounded;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        MovePlayer();
        RotateCamera();
    }

    void MovePlayer()
    {
        // Check if the player is on the ground
        isGrounded = characterController.isGrounded;
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f; // A small negative value to keep the player grounded
        }

        // Get input for movement
        float moveX = Input.GetAxis("Horizontal");
        float moveZ = Input.GetAxis("Vertical");

        // Calculate movement direction
        Vector3 move = transform.right * moveX + transform.forward * moveZ;
        characterController.Move(move * moveSpeed * Time.deltaTime);

        // Jumping logic
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        // Apply gravity
        velocity.y += gravity * Time.deltaTime;
        characterController.Move(velocity * Time.deltaTime);
    }

    void RotateCamera()
    {
        // Get input for mouse movement
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");

        // Rotate player along the y-axis
        transform.Rotate(Vector3.up * mouseX);

        // Rotate camera along the x-axis
        cameraTransform.Rotate(Vector3.left * mouseY);
    }
}
  • Animation Integration: Add animator components and trigger animations based on movement and jump states.
  • Advanced Physics: Integrate more complex physics interactions, such as slopes or surface friction.
  • Networking: Adapt the controller for multiplayer environments using Unity’s networking solutions.

Aug 12, 2024
Read More
Code
bash

Bash: How to Loop Over Files in a Directory

#!/bin/bash

for file in /path/to/directory/*; do
    echo "Processing $file"
    # perform some operation on $file
done

Aug 12, 2024
Read More
Code
csharp

C#: How to Read a File

No preview available for this content.

Aug 12, 2024
Read More
Code
html

HTML: Basic Template Structure

No preview available for this content.

Aug 12, 2024
Read More
Code
php

PHP: How to Connect to a MySQL Database

No preview available for this content.

Aug 12, 2024
Read More
Code
css

CSS: How to Center a Div Horizontally and Vertically

No preview available for this content.

Aug 12, 2024
Read More
Code
java

Java: How to Sort a List

No preview available for this content.

Aug 12, 2024
Read More
Code
javascript nodejs

Node.js: How to Create an HTTP Server

No preview available for this content.

Aug 12, 2024
Read More
Code
sql

SQL: How to Select Top N Records

No preview available for this content.

Aug 12, 2024
Read More
Code
python

Python: How to Reverse a String

No preview available for this content.

Aug 12, 2024
Read More
Code
javascript json

How to Deep Clone a JavaScript Object

No preview available for this content.

Aug 12, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!