Questions tagged [php]

PHP is a widely-used, general-purpose scripting language that is especially suited for web development.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
22 views

Eager loading using microservices

I am not quite sure if this is the best approach when fetching relationships, I have this: LinkController.php ...
0 votes
1 answer
23 views

Configuring SQLite3 for best efficiency as a persistent cache

I'm using SQLite3, via php's extension module, to create a persistent object cache for WordPress. It's intended for use on modestly sized sites that don't have access to redis or memcached. Why use ...
  • 301
-1 votes
0 answers
37 views

Two functions for protecting access to a page using data in cookies and session [closed]

I have a file named dashboard.php, and I want to allow only authorized users to access it, and I have used session and cookie. I have written this code: ...
2 votes
1 answer
145 views

C/C++ hash looking in flat database compared to PHP

I have a hash table which I would like to query as fast as possible. The hash table is stored this way : When I query a hash, I extract first 4 characters then use it to find the file. For instance a ...
  • 41
2 votes
4 answers
436 views

Creating storage path for file from first characters of a hash

I made the following function to create the storage path for files from the the first 5 characters of the file's hash: ...
5 votes
2 answers
883 views

Formatting Credit card names from API response

I have an API that returns the brand of a credit card. I want to make them look nice for my user (e.g. correct case and spacing). The possible options are amex, <...
  • 297
3 votes
2 answers
77 views

First attempt at a distance object

In our code we deal with distances a lot, a distance being a number and unit eg 15km, or 1.234mi The current approach is to use 2 functions, convert_distance(), and ...
  • 2,086
2 votes
2 answers
58 views

delete multidimentional array value in php

I made a program that converts JSON to a PHP array and checks every value of the multidimensional array with a loop and if a value matches certain values then it deletes that key and value and ...
1 vote
2 answers
83 views

PHP function that checks if user should have access based on button click and yearly quarter

I have a function that has a few and/or operators in it. I'm sure there is a better way to write this but can't seem to figure it out. I thought I could use arrays but ran into an issue with having ...
0 votes
0 answers
58 views

Pay with PayPal through cURL and verify

As All PayPal PHP SDK deprecated, I wrote a simple PHP class to make payment with PayPal through cURL and verifying it. Note : I didn't add shipment details for payment because didn't need that. For ...
2 votes
1 answer
65 views

PHP Laravel Check if given date is a week or a regular day

I have a problem. In my project the user can copy a menu for a specific day or for a specific week. The value that will be sent to the back-end will be something like: ...
2 votes
1 answer
76 views

Extracting authors and books from XML and inserting them into PostgreSQL

There is a tree of start folder, it's subfolders, their subfolders, etc. In each folder, subfolder, etc. there are the same structured XML files stored. books.xml ...
6 votes
2 answers
202 views

PHP readfile() with 304 Not Modified and HTTP 206 Partial Content support: httpreadfile

I wanted a readfile with 304 Not Modified and 206 Partial Content support, I think i got right, but probably spent way more effort on error-checking than worthwhile.. Also I didn't develop it by ...
1 vote
0 answers
27 views

How do you decide if a function should return data directly (via return statement) or indirectly (e.g. updating a class property) or both? [closed]

I have some functions which update records in some database tables. When I run each function I want to know the number of records affected by each. I'm wondering what the most elegant way of returning ...
  • 119
1 vote
0 answers
34 views

PHP PDO prepared statement with IN [closed]

Here's my code: ...
  • 111
0 votes
1 answer
51 views

Can I use PDO transaction in this way? [closed]

So I want to use PDO transaction in PHP OOP where in the class are multiple insert/update queries. If I create a new methods in Class like beginTransaction(), commit(), rollback() and I use them ...
8 votes
1 answer
149 views

Convert integer to Roman numeral string

This is my 1st time developing PHP code and I am only 13 years old. I have experience in other programming languages like lua, python, and c++. This is a simple Number to Roman Numeral code I made for ...
3 votes
3 answers
432 views

Sort array of associative arrays by the key of the subarrays

I am trying to sort an array of associative arrays in reverse order by the key of the subarrays. I solved it with a bubble sort algorithm which worked just fine in my test. I was wondering if there is ...
  • 243
1 vote
2 answers
85 views

Laravel PostController

I'm new at Laravel and I got an assessment task from company where they want me to create a mini-blog with Users and Posts. I was using following methods: index, create, show, store, edit and destroy ...
  • 13
0 votes
1 answer
50 views

Default to array when value is null or false

I'm working with ACF in WP which provides a get_field() function. The function can return an array of values, null or false. ...
  • 158
0 votes
1 answer
110 views

Index.php automatic router for custom MVC

In house we have been building our own MVC framework. One of the things that was important in the project was to not have to add routes manually but instead have a standard and let the index ...
2 votes
2 answers
611 views

Saving form data to a text file on server

I want to save simple string data as a simple text file (.txt) on the server (together with the IP), but I cannot find any clear guideline on how to do this correctly/efficiently/safely. (Maybe I'm ...
  • 153
4 votes
3 answers
341 views

Queries to get products selected in multiple stores

Project description I'm making a website listing products. The listing goes as following (See here): Product name Shop name Price in that shop If the product appears multiple times in the table for ...
0 votes
0 answers
124 views

Drag and Drop Sortable Image Upload System

I am working on web page where I want to use a "drag and drop" image upload system, with options to remove and sort images before upload. I would like to ask your opinion whether this system ...
2 votes
1 answer
112 views

function to group data by parent branches

I wrote this function to take database output like from PDO's fetchAll(PDO::FETCH_ASSOC) and turn it into a tree. For small datasets it works well. However, when ...
  • 123
4 votes
4 answers
424 views

Functions in PHP to run basic MySQL crud

I wrote a list of functions in PHP that I want to use in my pet project as an autoloaded file for all parts of the app. The purpose of these function is to shorten the code you write for MySQL queries ...
  • 166
3 votes
1 answer
209 views

Generating responsive image html with php?

As a beginner in php, I welcome you feedback on improving or simplifying the following php code that generates this responsive image html (image sizes and formats are auto-generated using Gulp). For ...
1 vote
2 answers
211 views

Laravel 8 blogging application

I am working on a blogging application in Laravel 8. In the FrontendController controller I have: ...
1 vote
0 answers
131 views

Applying the Strategy pattern for payment methods

The strategy pattern is applied in the code below in the payment separation solution. Please evaluate if it is well done according to standards. I would be very grateful for suggestions. ...
0 votes
0 answers
46 views

file MIME check on update form

I'm a very beginner. I developed this today and it's working (YAY) but I would like to make sure ALL of my checkFile($file) method's conditions are fully used and ...
1 vote
0 answers
56 views

Error handling for Laravel REST API client and server using bearer token authentication

I would like to query a REST API (which uses Bearer token authentication), handling several exceptions that might occur. Could you tell me if the following code (which I explain later in this post) is ...
4 votes
3 answers
818 views

Filter an array, but remove the filtered elements

I needed a PHP function that would filter values from one array into a new one, while also removing those values from the original array. After looking through the docs, I didn't see any such function,...
0 votes
1 answer
70 views

How to optimize a search between dates in a WP Query?

In the posts I have a date field and a start and end date range. I need to query if it matches date or if it matches between the dates in the range. This Query works, but how can I optimize? ...
1 vote
1 answer
141 views

Wrap INSERT statements to PDO transaction (PHP OOP)

so Im trying to wrap SQL INSERT statements to PDO transaction in PHP OOP. I don't know if I'm doing it right and this is the best/easiest way to do it. So what is important for me is: There can be ...
-1 votes
2 answers
158 views

Product list from database to web tables/boxes

I am a self-taught front-end developer, trying to land a job, the PHP and MySQL are quite new to me, since I only watched and did some code through Traversy Media courses. This is a test project, ...
  • 11
1 vote
2 answers
74 views

Check if a new movie session can be inserted

I have this function in my code, this way I validate if a new movie session can be inserted, it works, but I wanted to find a better way to do it. ...
  • 11
1 vote
1 answer
108 views

Is this PDO query okay to use in PHP OOP?

Do you think this code is good if I want to do a simple SQL query? Is it safe to use? Or am I doing something wrong? ...
0 votes
1 answer
50 views

Simple websocket chat app, security and efficiency concerns

I'm coding a simple websocket chat app. I was wondering if my code is secure and efficient. I also have a question about the way how I'm sending objects and then checking the type, is there anything ...
  • 21
5 votes
2 answers
633 views

Count each word within a string

I am looking for a solution with fewer lines of code. This is code that is working perfectly, but I think I typed too much for this algorithm. If I did something wrong, feel free to tell me the error. ...
7 votes
2 answers
398 views

OOP PHP form builder with validation

Yes, another PHP form builder... I wrote this solely to demonstrate OOP principles. My questions are: Could this be written better? OOP-wise. How could I better implement error handling? Index.php <...
  • 73
0 votes
1 answer
68 views

PHP assign values within in_array

What is a more efficient way of writing this sample code? Its purpose is to create a variable for days of the week based on a numerical value. This data is obtained from user input checkboxes. If the ...
  • 113
5 votes
3 answers
128 views

DB Class extending PDO w/ other useful functions

I have this DB class that extends PDO, that also has useful functions for certain tasks; I was wondering how well it's coded, and if so, what I could maybe improve on. I tried commenting in ...
  • 153
1 vote
1 answer
38 views

first try packagist: Detection of execution by command (Terminal) or web environment

I'm here again; I'm learning a bit about github and posting things to packagist, this is my first try. I have created a library that validates or verifies in what type of environment php is being ...
user avatar
-3 votes
2 answers
131 views

Insert employee data into a database

I've constructed code using prepared statements to try and avoid chances of SQL injections. This code is meant to insert the employees information. It does what I need it to do but I want to know if ...
2 votes
0 answers
56 views

Magic Class Invoker for procedural script

I have inherited a project in bad conditions that contains the following class and I need your help to improve it, the truth is I don't know if it is correctly optimized or if it has security problems ...
user avatar
-1 votes
1 answer
31 views

Validation rules for payment cards in a data set

I have the following part of code. I dislike this, because if I need to add additional validation rule, I have to make changes in loop and create a varaible to store boolean result. For example check ...
  • 1
0 votes
1 answer
127 views

Laravel 9 - Metadata creation using middleware?

I wrote a middleware for laravel that grabs the current metadata for the specific URL you visit. How it works: we use the $request and compare the current URL: <...
0 votes
0 answers
32 views

Execute a sequence of instructions

I wrote a quick POC application to execute a sequence of defined instructions and I would like some feedback if possible. This is purely a proof of concept and I wanted to implement a concept similar ...
  • 143
0 votes
1 answer
86 views

Sudoku puzzle solver for Leetcode

I just solved a problem from leetcode that is faster than 28.42% of the PHP codes, so the main review target is not the performance or alternative ways to solve this problem. I wrote the code thinking ...
  • 3
-2 votes
1 answer
104 views

Insert row into database using static or non-static method [closed]

I would like to know if there are any benefits of one of the following 2 methods which insert an object into a database The first function calls the Model method statically, creating a new instance of ...

1
2 3 4 5
87