Questions tagged [php]
PHP is a widely-used, general-purpose scripting language that is especially suited for web development.
4,303
questions
1
vote
2
answers
2k
views
Formatting date difference in human-friendly terms
I've written a function to take the difference between two dates and display it in a particular format. I've used a lot of if-else statements, which seems a little ...
0
votes
0
answers
264
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 ...
3
votes
2
answers
86
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 ...
3
votes
1
answer
205
views
Diving into PHP's MVC - the application's core
I recently started programming in PHP using the MVC-pattern, which resulted in one of best choices I made so far, but I read through many articles showing "easy" examples and some of them ...
5
votes
2
answers
740
views
A PHP script used on my home server to download YouTube videos
I have a PHP page I used to download YouTube videos, hosted on my Pi 4.
The site is only going to be used on my home network (so a dot local address), but it allows me to download videos on my phone ...
4
votes
2
answers
2k
views
Full text search for multiple words using Laravel and MySQL
I'm using Laravel 5.6 and MySQL. I need to search multiple columns for a single word or multiple words (e.g., "camhandle", "cam handle").
The way I was originally doing it was just ...
1
vote
2
answers
90
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 ...
2
votes
2
answers
68
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 ...
3
votes
3
answers
641
views
Bool function in PHP to check if you run the script inside docker
I had to do a CLI script that does something, I also had to add a validation that checks if it runs inside the Docker. After some research I found a solution that people check some processes via ...
38
votes
7
answers
105k
views
Setting a default value if a variable is empty
This is the pattern I've been using for "if the variable is empty, set a default value", and for variables like $muffin, it has all seemed well and good. But in ...
2
votes
1
answer
103
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:
...
4
votes
1
answer
119
views
Enables markup to instantiate objects, call methods, and generate HTML
I created this package PXP.
PXP enables markup to instantiate objects, call methods, and generate
HTML. It works similar to a server-side templating engine, but rather
than enforcing braces it ...
3
votes
1
answer
1k
views
Add post to favorite in PDO and mysql
After lot of research and couple of questions in https://stackoverflow.com/ i managed to create Favorite System in PDO mysql as ...
2
votes
1
answer
98
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
...
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 ...
6
votes
2
answers
250
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
31
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 ...
1
vote
0
answers
41
views
PHP PDO prepared statement with IN [closed]
Here's my code:
...
0
votes
1
answer
96
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 ...
5
votes
1
answer
75
views
Create archive webpage for newsletters based on nested array
I am creating a webpage for my newsletter archives. The archives are divided by year (currently there is only 1, but there could be many), then by topic, before listing each newsletter. There will ...
3
votes
3
answers
459
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 ...
4
votes
1
answer
91
views
Request handler based solely on reflections ver. 2
This question is connected to the previous one: Request handler based solely on reflections
I have edited the code, taking tim's answer in regard, editing the Kernel class cohesion.
...
1
vote
2
answers
110
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 ...
4
votes
1
answer
250
views
Database wrapper class
I'm trying to create a single class to interact with my database (MariaDB, in this case). Is there anything I can do to improve this code? I'm fairly new to Object-oriented PHP.
...
0
votes
1
answer
60
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. ...
2
votes
2
answers
658
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 ...
2
votes
1
answer
196
views
Merge sort in PHP 5
I have this small program in PHP implementing merge sort. I used one rather simple run time optimisation via using one additional array which allows faster merge operations:
...
4
votes
3
answers
361
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 ...
1
vote
2
answers
15k
views
PHP Routing with MVC Structure
I'm trying to do a simple CMS with PHP from scratch using MVC structure.
Yesterday I posted this, which is a login system using PHP and it works but it has a handful of problems regarding the OOP ...
0
votes
0
answers
193
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 ...
4
votes
4
answers
456
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 ...
5
votes
1
answer
228
views
Testable code with ORM factory
I'm trying to improve the following code for testability.
...
2
votes
1
answer
117
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 ...
1
vote
2
answers
242
views
Laravel 8 blogging application
I am working on a blogging application in Laravel 8.
In the FrontendController controller I have:
...
0
votes
4
answers
312
views
table name column name from GET parameter
This is a function that reads a setting data from DB.
...
7
votes
4
answers
2k
views
Function for creating unique strings for urls
I wrote this function in order to create unique url's for my site. Using these urls, visitors could demo site functionality without having to create a login. The function was intended to return a ...
3
votes
1
answer
237
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 ...
2
votes
2
answers
117
views
Calculate tax based on product category
Below you can see a class and a function. The class is just an dummy entity. The function has a switch statement. That switch statement calculates the tax by to a predefined hardcoded string, and I ...
1
vote
0
answers
141
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 ...
2
votes
1
answer
179
views
Optimization of laravel pivot table relationship
I have a pivot table called invite_riskarea which is designed as follows:
This table stores the permissions that have a specific user (through an invite id) to ...
6
votes
4
answers
1k
views
Dynamically loading CSS files based on URL or URI in PHP
I have written out the following code in the head section of my document,
which should load 3 separate style sheets for each page based on the URL/URI that the user is visiting. It is working as ...
3
votes
1
answer
2k
views
prestashop: get final product category
In general, in Prestashop there are no functions to really have the final category (i.e. the one with highest level_depth) of a product based on id_product, so I extended the Product class and added ...
1
vote
0
answers
122
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 ...
39
votes
3
answers
4k
views
Displaying courses in an HTML calendar
I'm struggling for a while now with the readability of my code, I after I tried to get as much insight as possible (for my standards). On my level, I think I understand and use it all right for my ...
4
votes
4
answers
719
views
Guestbook script
I'm brand new at OOP PHP and I would like to get as many of the coding conventions right as fast as possible. I made this tiny guestbook script, and I would like to know if there's anything not done ...
6
votes
2
answers
169
views
File read/write methods
I'm using some simple files for caching and some basic user data. I was first just using file_put_contents() and ...
-3
votes
2
answers
134
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 ...
1
vote
1
answer
182
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?
...
5
votes
3
answers
141
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 ...