-
Posts
24,603 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
newbee to PHP, help with setting up my php to send to a txt file
Barand replied to CarriesBack's topic in PHP Coding Help
Please use code tags, or use the <> button in the toolbar, for your code. -
newbee to PHP, help with setting up my php to send to a txt file
Barand replied to CarriesBack's topic in PHP Coding Help
try if ( is_numeric($BowlerAverage) && $BowlerAverage > 0 && $BowlerAverage <= 300 ) -
newbee to PHP, help with setting up my php to send to a txt file
Barand replied to CarriesBack's topic in PHP Coding Help
No, you are still checking if a boolean expression is numeric. -
Quite true. If I used the original unsortable date formats ... $data = array( array('percentage' => '25', 'beforedate' => '10-12-2016' ), array('percentage' => '15', 'beforedate' => '31-03-2017' ), array('percentage' => '10', 'beforedate' => '30-04-2017' ), array('percentage' => '20', 'beforedate' => '31-01-2017' ), ); ...the result is 10% as the result of sorting those dates is 10-12-2016 30-04-2017 31-01-2017 31-03-2017
-
newbee to PHP, help with setting up my php to send to a txt file
Barand replied to CarriesBack's topic in PHP Coding Help
No matter what value have for the bowler average, your test will always fail. The expression ($bowleraverage >= 0) is Boolean and therefore not numeric. EG $bowleraverage = -1; echo (is_numeric($bowleraverage >= 0)) ? 'OK' : 'Error' . '<br>'; $bowleraverage = 0; echo (is_numeric($bowleraverage >= 0)) ? 'OK' : 'Error' . '<br>'; $bowleraverage = 50; echo (is_numeric($bowleraverage >= 0)) ? 'OK' : 'Error' . '<br>'; That code outputs Error Error Error You need to separate the is_numeric() check from the value checks as ginerjm said. -
Alternative solution mysql> SELECT * FROM bookingdates; +-----------------+------------+------------+ | bookingdates_id | beforedate | percentage | +-----------------+------------+------------+ | 1 | 2017-12-10 | 25 | | 2 | 2017-01-31 | 20 | | 3 | 2017-03-31 | 15 | | 4 | 2017-04-30 | 10 | +-----------------+------------+------------+ SELECT percentage FROM bookingdates WHERE beforedate = ( SELECT MIN(beforedate) FROM bookingdates WHERE beforedate >= CURDATE() ); +------------+ | percentage | +------------+ | 15 | +------------+
-
You need to sort the array into ascending date order. Then loop through the array until you come to a date that is >= today. This is the percent that you want so you can now break out of the loop. Given the data you posted (but correcting the date format) you would have $data = array( array('percentage' => '25', 'beforedate' => '2016-12-10' ), array('percentage' => '15', 'beforedate' => '2017-03-31' ), array('percentage' => '10', 'beforedate' => '2017-04-30' ), array('percentage' => '20', 'beforedate' => '2017-01-31' ) ); // SORT BY DATE usort ($data, function($a, $b) { return strcmp($a['beforedate'], $b['beforedate']); }); // VIEW RESULTS $today = new DateTime(); $today->setTime(0,0,0); // don't want the time portion foreach ($data as $rec) { if ($today <= new DateTime($rec['beforedate'])) { $discount = $rec['percentage']; break; } } echo "Applicable discount today is {$discount}%"; //--> 15%
-
We don't know what your input values are. We don't know what you are expecting from those values. We don't know what "not working" means. You might as well try "Alexa! What is the page value?"
-
Firstly, you need to change those dates - they are not sortable. You need yyyy-mm-dd
-
see http://uk1.php.net/manual/en/function.natsort.php
-
I tried helping you with advice on writing a custom sort function and how it should return values, but you chose to totally ignore it. Therefore I am not inclined to waste any more of my time.
-
Post your current code and I'll try to run it.
-
Is there a more efficient method to compress these queries?
Barand replied to imgrooot's topic in PHP Coding Help
see https://forums.phpfreaks.com/topic/303305-pdo-looping/?p=1543471&do=findComment&comment=1543471 -
Any ideas why code is inserting just one character of each value?
Barand replied to sonnieboy's topic in PHP Coding Help
Then why are they output within a foreach loop? -
The times when you might want similar to that is when you want to specify the array indexes For example if ($stmt->execute()) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $products[ $row['user_id'] ] = $row; } } or to group results if ($stmt->execute()) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $products[ $row['cat_id'] ][] = $row; } }
-
Any ideas why code is inserting just one character of each value?
Barand replied to sonnieboy's topic in PHP Coding Help
If, say, $_POST['sourcename'] is a string and not an array then $_POST['sourcename'][$i] with be the $ith character of the string and not the $ith element of an array. -
Is there a more efficient method to compress these queries?
Barand replied to imgrooot's topic in PHP Coding Help
One query, get all users and store in an indexed array. Not separate numbered variables. -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
In what way is that "outside the function"? $db = DB(); // connection OUTSIDE the function $vehicle_data = UserDetails($db, $user_id); // pass the $db connection as a parameter plus you need to redefine the function to accept the extra parameter function UserDetails ($db, $user_id) { // function code } -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
It was better as it was. Just add a WHERE clause to tell it which user. $query = $db->prepare("SELECT users.user_id , users.registration , users.email , vehicle.reg_id , vehicle.vehicle , vehicle.registration , vehicle.chassis FROM users JOIN vehicle ON vehicle.registration = users.registration WHERE users.user_id = :user_id"); $query->bindParam("user_id", $user_id, PDO::PARAM_STR); It looks like the line $db = DB(); is connecting to your database. Move that code out of the function and pass $db as a parameter to your function with $user_id. Connecting is a relatively slow process and you don't want to do every for every query. -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
Add those columns' names to the list of columns in your SELECT clause. EDIT: You now have a bound parameter but there is nowhere to bind the parameter to. You need a WHERE clause in the query -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
Then that is what you should use for the join with your current tables. SELECT .... FROM user u JOIN vehicle v ON u.registration = v.registration However, a more flexible model would be ... +-------------+ | user | +-------------+ | user_id |--------+ | email | | +--------------+ | etc | | | vehicle | +-------------+ | +--------------+ | | vehicle_id | +--------<| user_id | | registration | | vehicle | | chassis_no | +--------------+ .. which easily allows you to add a second (and third...) car for a user just by adding another row with the user's user_id. In this model the join would be on the user_id columns. -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
Your column names are ambiguous and confusing. Can you show us your table structures and some sample data? -
$data = []; is equivalent to $data = array();
-
A sort function should return -ve, zero or +ve values depending on the comparison result. Yours will return a boolean result. This would achieve that return filemtime($x) - filemtime($y);
-
You may find it better to store the date and contents in the array (with filename as the key). In which case a simple asort() will not work, you will need a custom sort function. For example $files['file1.txt'] = [ 'date' => '2017-01-03', 'content' => 'aaa' ]; $files['file2.txt'] = [ 'date' => '2017-01-01', 'content' => 'bbb' ]; $files['file3.txt'] = [ 'date' => '2017-01-02', 'content' => 'ccc' ]; $files['file4.txt'] = [ 'date' => '2017-01-04', 'content' => 'ddd' ]; uasort($files, function ($a, $b) { return strcmp ($a['date'], $b['date']); }); foreach ($files as $filename => $fdata) { // process the output }