Posts

Showing posts with the label php

Preg_split on 2 characters

Preg_split on 2 characters Im trying to split a string on 2 variables (T & +) which when using on "2018-06-25T10:32:00+01:00" will split it to:- [0] => 2018-06-25 [1] => 10:32:00 [2] => 01:00 So I thought I could use "preg_split("/ (T|+) /", $ent)", but it itsnt working at all. My code I have (in PHP) is:- $ent = "2018-06-25T10:32:00+01:00"; print_r(preg_split("/ (T|+) /", $ent)); Any ideas what Im doing wrong? Remove the spaces – Philipp Jul 2 at 9:43 That worked, thank you :-) Sorry, Im a newbie at this – Tony Cross Jul 2 at 9:57 Why do you parse dates using regex ? PHP provides the DateTime classes t...

session_start(): Cannot send session cache limiter - headers already sent even with session_start() at the top [closed]

session_start(): Cannot send session cache limiter - headers already sent even with session_start() at the top [closed] I'm trying to set up an admin login page but I keep getting this error. It works on a local server but not on an online server. I know this is a duplicate but all the other answer suggest putting session_start() at the top which I'm already doing. Here's my code index.php if ( !isset($_SESSION['user']) ){ header('Location: login.php'); } else { ?> //html content <?php } login.php (just html with a form tag) <form method="POST" action="loginVerify.php" autocomplete="off"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="submit"> </form> login verify <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; $admin...

Merging Eloquent result set

Merging Eloquent result set I have two eloquent outputs allowance Amount A 100 B 80 C 120 D 150 AND Deductions Amount X 50 Y 60 Z 70 and json appears as $allowance = [{"allname":"A","allamount":"100"},{"allname":"B","allamount":"80"},{"allname":"C","allamount":"120"},{"allname":"D","allamount":"150"}] $Deductions = [{"dedname":"X","dedamount":"50"},{"dedname":"Y","dedamount":"60"},{"dedname":"Z","dedamount":"70"}]. I want new json to be like $new = [{"allname":"A","allamount":"100","dedname":"X","dedamount":"50"},{"allname":"B","allamou...

Backup MySQL tables in Laravel without third-party libraries

Backup MySQL tables in Laravel without third-party libraries I new to Laravel. I've been trying to create a controller that backups tables in the form of backup_date_.sql format without using any third-party library at all, but I'm getting frustrated. I've searched and I found some code examples. I've tried to use them within my BackupsController, but things are getting more and more difficult. Any help is really appreciated. This is my code, Thanks in advance. <?php public function query($data, $mode = PDO::FETCH_ASSOC) { $pdo = DB::connection()->getPdo(); $stmt = $pdo->query($data); $results = $stmt->fetchAll($mode); // $results = $stmt->fetch($mode); return $results; } public function backup(Request $request) { if ($request->all()) { $output = ''; foreach (request('table') as $table) { $show_table_query = $this->query("SHOW CREATE TABLE " . stripslashes($table) . "...

PHP register check for user in database [closed]

PHP register check for user in database [closed] I created a function that checks for user in database but when I type the email that exists, it returns the message, and then, on second try can't proceed to register function, can't get rid of error, here is my try: if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $email_error = "Invalid email format"; } else if($user->doesUserExist($email) > 0) { $email_error = "Email is already taken"; } else { $email_error = ""; } if(empty($error) && empty($email_error) && empty($confirm_password_error)) { $user->register($username, $name, $email, $password); } It's not problem with register function. Here are the class functions: public function doesUserExist($email) { $query = "SELECT count(*) FROM users WHERE email = :email"; $stmt = $this->connection->prepare($query); $stmt...

Standard JSON format for PHP

Standard JSON format for PHP I am storing data in JSON files with array of JSON objects. Right Now I am doing like [{"key1":"value1","key2":"value2"}] Is this universally accepted. ? If so which Tech Lead company supports this format . JSON is universal data format. So it is supported. In your case, however, json is incorrect as it uses single quotes. Correct json uses double quotes. More info on json.org. – u_mulder Jul 2 at 10:09 Okay ... let me correct that .. And thanks for opening the eyes of beginner – Manjunath Jidagi Jul 2 at 10:12 1 Answer 1 ...

Mail validation in laravel

Mail validation in laravel I am trying to use laravel form validation. My validation code is: $this->validate(request(), [ 'name' => 'required|alpha_num|max:255', 'email' => 'required|email|max:255|unique:users', 'register_no' => 'required|regex:/(1)[0-9]{3}-[0-9]{3}-[0-9]{3}/', 'password' => 'required|min:6' ]); name takes everything whereas I want only letters & hyphen(-). email takes gt@hj.fhdfygt@hj.fhdfy whereas I want like something@anything.com . register_no says pattern doesn't match I wrote to return like 111-111-001 . name email gt@hj.fhdfygt@hj.fhdfy something@anything.com register_no 111-111-001 There already is an email validator in Laravel – kerbholz Jul 2 at 10:10 2 Answers ...

How to add a class to a WooCommerce product within the product loop

How to add a class to a WooCommerce product within the product loop I would like to add multiple bootstrap col classes to this line of code inside the product loop but I have no idea. <li><?php wc_product_class(); ?></li> Can I somehow push my classed into this bit of php and still keep the product applied? 2 Answers 2 try this <li <?php wc_product_class("col-md-4 col-sm-6 custom-class m-4"); ?>></li> <li <?php wc_product_class("col-md-4 col-sm-6 custom-class m-4"); ?>></li> For this wc_product_class(); uses two arguments one is class name and other one is product id so just pass your class name as first argument and product id in second one. wc_product_class(); you could see the wc_product_class() in /woocommerce/includes/wc-template-functions.php For example your class name is test and product id is 143 then just pass ...

Nginx PHP Failing with Large File Uploads (Over 6 GB)

Image
Nginx PHP Failing with Large File Uploads (Over 6 GB) I am having a very weird issue uploading larges files over 6GB. My process works like this: My PHP(HHVM) and NGINX configuration both have their configuration set to allow up to 16GB of file, my test file is only 8GB. Here is the weird part, the ajax will ALWAYS time out. But the file is successfully uploaded, its gets copied to the tmp location, the location stored in the db, s3, etc. But the AJAX runs for an hour even AFTER all the execution is finished(which takes 10-15 minutes) and only ends when timing out. What can be causing the server not send a response for only large files? Also error logs on server side are empty. You need to post the code if you want some help figuring out what might be wrong with it. – Barmar Jun 5 '17 at 15:04 Do you make r...

Session content not being deleted with either session_destroy(); or unset

Session content not being deleted with either session_destroy(); or unset I've had this issue for a couple of days and I can't seem to fix it whatever I try. There is a loginform that either redirects you to /dashboard or /admin depending on the data that is in the started session from my loginscript. /dashboard /admin This is my structure: index.php (contains header with connection.php and the html login form) dashboard.php (contains header with connection.php and some irrelevant html) admin.php (same as above) header.php (at the top of all pages, has connection.php included at the first line). connection.php (my connection script, with at the top line session_start(); login.php (my loginscript, this script is called with ajax so I have to add my entire connection class in here with session_start(); again at the top. logout.php (my logout script, it kills the session and redirects to login page this is linked to in my header.php) In my loginscript I have the following part: ...

Load Random Images from Directory

Load Random Images from Directory I'd like to randomly load images from a directory and have a button somewhere that refreshes the entire page. Here's the current code I have now: <?php $a = array(); $dir = '../public/wp-content/uploads/2012/01'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (preg_match("/.png$/", $file)) $a = $file; elseif (preg_match("/.jpg$/", $file)) $a = $file; elseif (preg_match("/.jpeg$/", $file)) $a = $file; } closedir($handle); } foreach ($a as $i) { echo "<img src='" . $dir . '/' . $i . "' />"; } ?> The problem is it loads all 400,000 images at once. I only want 30 to load. 30 random images from the directory. I tried looking up some code such as modifying the above to this: <?php $a = array(); $dir = '../public/wp-content/uploads/2012/01'; if ($handle = opendir($dir)) { while (false !== ($file = re...

Php Slim with SolrClient

Php Slim with SolrClient I am trying query on my solr server from php slim code but i have problem to use SolrClient. I download solr.so in my php.ini. When i run normal php code, it works correct, However when I run slim code with SolrClient class, it says "Class 'blablaSolrClient' not found". I know solr.so is module for pure php but I want to know if it is possible to run it with slim? Thx in advance 1 Answer 1 You have two options to use the global namespace for the SolrClient class. $client = new SolrClient($options); use SolrClient; $client = new SolrClient($options); thanks man first solution works. You saved my day – erdemhayir Jul 4 at 12:22 Then can you please accept my answer? ...

XML from URL into historical data graph

XML from URL into historical data graph What I'm after is a service or script that pulls XML from a URL once every x minutes, saves the data to a mysql database and then users are able to search for an item and it displays historical price data for that item. This is what the XML looks like for 1 item: <?xml version="1.0" encoding="UTF-8"?> <Success> <Result> <AccId>2075</AccId> <Type>2793</Type> <Price>100</Price> <SaleId>211557</SaleId> <Time>636662891054273275</Time> <Hours>47</Hours> </Result> <Result> <AccId>7911</AccId> <Type>2793</Type> <Price>100</Price> <SaleId>198528</SaleId> <Time>636662291658957640</Time> <Hours>31</Hours> </Result> <Result> <AccId>8839</AccId> ...

Set Outgoing ip for remote php mysql connection

Set Outgoing ip for remote php mysql connection I have php application with separate host for both application and database, Lets say my application is hosted on Ip1, and db is hosted on Ip2, both apllication and Db is in separate network, now application server is behind the firewall, my firewall has two ips fw1,fw2 for failover purpose, for both firewall ips, rule has been set to allow connection from ip1 to ip2. Now the problem is one of the firewall ip fw1 is down, and my application is still going to connect db hosted on ip2 through fw1, resulting in fatal error PDO CONNECTION ERROR: SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. Is there any way in php,mysql, i can define Outgoing ip for remote mysql connection, so i can direct application to make connection through fw2 instead of fw1. I can do this in case of curl...

how to select 3 fruit from 4 fruit in sequence and alternately?

how to select 3 fruit from 4 fruit in sequence and alternately? for example i have this sequence : orange, banana, apple and raspberry. i want to show 3 data in sequence and alternately like this : etc note : the amount of data i want to display changes i try using array_push and array_pop but i stuck when i want to random it. here my code : array_push array_pop <?php $stack = array("orange", "banana"); array_push($stack, "apple", "raspberry"); array_pop($stack); print_r($stack); ?> can anyone fix my code? php.net/manual/en/function.shuffle.php – jeroen Jul 2 at 8:06 @jeroen would not using shuffle would just random rather than sequentially? – Daniel Alfianto Jul 2 at 8:09 ...