-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Just a bit. => is used to define key/value pairs in an array, not for assigning values to variables. Your array isn't assigned to variable so the array definition does nothing. try changing the array creation to $data = array('fname'=>$_POST['fname'], 'lname'=>$_POST['lname'], 'email'=>$_POST['email'] ); then change the execute stement to $stmt->execute($data);
-
Where have $fname, $lname and $email appeared from?
-
Obviously, $POST should be $_POST
-
when i click on buy now then open a particular url ..........javascript
Barand replied to pallavi's topic in Javascript Help
Perhaps something like this HTML <form id="abc"> <select id="foo"> <option value="">- select -</option> <option value="bar">Bar</option> <option value="cat">Cat</option> <option value="mat">Mat</option> </select> </form> <a id="ab" >buy now</a> SCRIPT <script type='text/javascript'> const urls = { bar:"www.youtube.com", cat:"www.twitter.com", mat:"www.facebook.com" } $(function() { $("#foo").change(function() { $("#ab").attr("href", urls[ $(this).val() ] ) }) }) </script> -
Here's my entry for this competition... $status=""; if($_SERVER["REQUEST_METHOD"]=="POST") { $fname=$POST['fname']; $lname=$POST['lname']; $email=$POST['email']; if(empty($fname)|| empty($lname)|| $email){ $status="All fields are compulsory."; }elseif(strlen($fname)>= 255 || !preg_match("/^[a-zA-Z-'\s+$/", $fname)){ $status="Please enter a valid name"; }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $status="Please Enter a valid email"; }else{ $sql="INSERT INTO senior_dat(fname,lname) VALUES (:fname, :lname)"; $stmt=$pdo->prepare($sql); $stmt->execute(['fname'=>$fname], ['lname'=>$lname]); $status="Your entrys have been accepted"; $fname=""; $email=""; } } BTW, what's the prize?
-
<?= $x ?> is shortcode alternative syntax for <?php echo $x; ?>
-
The first thing I'd do is convert the file data into something that is easily processed... $filedata = file('test.txt', FILE_IGNORE_NEW_LINES); # # organise text file data into a manageable structured array # $data = []; foreach ($filedata as $line) { $rec = []; $fields = explode('|', $line); foreach ($fields as $f) { list($k, $v) = explode(':', $f); $rec[trim($k)] = trim($v); } $data[] = $rec; } giving $data = Array ( [0] => Array ( [Name] => John Doe [Email] => johnd@gmail.com [Staff ID] => SS1234 [Gender] => Male [School] => FECS ) [1] => Array ( [Name] => Jane Doe [Email] => doejane@gmail.com [Staff ID] => SS3454 [Gender] => Female [School] => SFS ) [2] => Array ( [Name] => Scott Doe [Email] => does@gmail.com [Staff ID] => SS9087 [Gender] => Male [School] => FBDA ) [3] => Array ( [Name] => Kelly Doe [Email] => kellydoe@gmail.com [Staff ID] => SS2093 [Gender] => Female [School] => SFS ) ) Now it's simple to loop through the array to find the record you want for each ($data as record} if (record is required ) output record contents end if end for each
-
There is no guarantee a query will always work, even if there are no errror. However for a query to fail it has to be executed and I saw no such code in the code you posted. However the best way to check is to put tis line of code mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); immediately before the line where you call mysqli_connect(). Make sure that error_repoting is set to E_ALL and display_error is on (php.ini file settings) I know, I read your update query string. You said the feed value can be from +1 to +14. Your query always adds 1, but what if the value is already 14 - what should happen then?
-
Return the value from the function https://www.php.net/manual/en/functions.returning-values.php
-
mysqli behaves differently on virtual and physical machines
Barand replied to mgaman's topic in PHP Coding Help
Try this code in both environments <?php mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $link = mysqli_connect($dbhost, $dbusername, $dbpassword,$dbname); ?> -
PS When I run $pdo->exec("SELECT * FROM student"); $res = $pdo->fetchAll(); I get Fatal error: Uncaught Error: Call to undefined method PDO::fetchAll() Have you got error reporting switched on?
-
PDO::exec() does not return a resultset (update, insert, delete only) query() or execute() to get a resultset.
-
First step is to turn on PDO exception reporting and ensure PHP reporting is on.
-
There is a forum on this site where you can requet work to be done https://forums.phpfreaks.com/forum/77-job-offerings/ Provide a description of what you need and contact details.
-
If the name of the function you created is "nearestDate" then that is what the query should use (not "nearestDate2" which was an alternative version I tried.) But I'm sure you could have worked that out for yourself.
-
On further experimentation, a hybrid method, which only calls on the function when the conventional query fails to find a matching date, seems to be the most efficient option. In my tests, this method was 30 times faster (but that would depend on how many unmatched dates there were) SELECT a.symbol , a.price as today , CASE WHEN b.price IS NOT NULL THEN b.price ELSE ( SELECT price FROM sampletable WHERE symbol = a.symbol AND date = nearestDate2(a.symbol, CURDATE() - interval 7 day) ) END as last_wk , CASE WHEN c.price IS NOT NULL THEN c.price ELSE ( SELECT price FROM sampletable WHERE symbol = a.symbol AND date = nearestDate2(a.symbol, CURDATE() - interval 1 month) ) END as last_mth , CASE WHEN d.price IS NOT NULL THEN d.price ELSE ( SELECT price FROM sampletable WHERE symbol = a.symbol AND date = nearestDate2(a.symbol, CURDATE() - interval 1 year) ) END as last_yr FROM sampletable a LEFT JOIN sampletable b ON a.symbol = b.symbol AND b.date = a.date - INTERVAL 7 DAY LEFT JOIN sampletable c ON a.symbol = c.symbol AND c.date = a.date - INTERVAL 1 MONTH LEFT JOIN sampletable d ON a.symbol = d.symbol AND d.date = a.date - INTERVAL 1 YEAR WHERE a.date = CURDATE(); +--------+-------+---------+----------+---------+ | symbol | today | last_wk | last_mth | last_yr | +--------+-------+---------+----------+---------+ | ABC | 19.24 | 5.96 | 5.30 | 3.86 | | DEF | 19.52 | 10.15 | 7.61 | 16.59 | | XYZ | 15.00 | 2.87 | 15.11 | 4.20 | +--------+-------+---------+----------+---------+
-
If you had data for every day for symbol it would be relatively simple SELECT a.symbol , a.date as today , a.price as today_price , b.date as weekago , b.price as last_wk_price , c.date as monthago , c.price as last_mth_price , d.date as yearago , d.price as last_yr_price FROM sampletable a LEFT JOIN sampletable b ON a.symbol = b.symbol AND b.date = a.date - INTERVAL 7 DAY LEFT JOIN sampletable c ON a.symbol = c.symbol AND c.date = a.date - INTERVAL 1 MONTH LEFT JOIN sampletable d ON a.symbol = d.symbol AND d.date = a.date - INTERVAL 1 YEAR WHERE a.date = CURDATE(); which gives ++--------+------------+-------------+------------+---------------+------------+----------------+------------+---------------+ | symbol | today | today_price | weekago | last_wk_price | monthago | last_mth_price | yearago | last_yr_price | +--------+------------+-------------+------------+---------------+------------+----------------+------------+---------------+ | ABC | 2022-09-30 | 19.24 | 2022-09-23 | 5.96 | 2022-08-30 | 5.30 | 2021-09-30 | 3.86 | | DEF | 2022-09-30 | 19.52 | 2022-09-23 | 10.15 | 2022-08-30 | 7.61 | 2021-09-30 | 16.59 | | XYZ | 2022-09-30 | 15.00 | | | 2022-08-30 | 15.11 | | | +--------+------------+-------------+------------+---------------+------------+----------------+------------+---------------+ ABC and DEF have daily data whereas XYZ on ly has 1 or 2 per week so, as you see, some results are missing. The tricky part is finding the closest date in the table to the required date. My solution was to use a MySQL user defined function. CREATE FUNCTION `nearestDate`(sym varchar(20), thedate date) RETURNS date READS SQL DATA BEGIN DECLARE v_closest DATE; DECLARE v_mindiff INT; SELECT date INTO v_closest # do we have the exact date? FROM sampletable WHERE symbol = sym AND date = thedate LIMIT 1; IF v_closest IS NOT NULL THEN # if we do, return it RETURN v_closest; END IF; SELECT y.date INTO v_closest # find smallest date difference FROM ( SELECT symbol , MIN(abs(datediff(date, thedate))) as mindiff FROM sampletable WHERE symbol = sym ) x JOIN # and match to find the date ( SELECT date FROM sampletable WHERE symbol = sym ) y ON abs(datediff(y.date, thedate)) = x.mindiff; RETURN v_closest; # return found date END The query using that function becomes SELECT a.symbol , a.date as today , a.price as today_price , b.date as d7 , b.price as last_wk_price , c.date as d30 , c.price as last_mth_price , d.date as d365 , d.price as last_yr_price FROM sampletable a LEFT JOIN sampletable b ON a.symbol = b.symbol AND b.date = nearestDate(a.symbol, curdate()-interval 7 day) LEFT JOIN sampletable c ON a.symbol = c.symbol AND c.date = nearestDate(a.symbol, curdate()-interval 1 month) LEFT JOIN sampletable d ON a.symbol = d.symbol AND d.date = nearestDate(a.symbol, curdate()-interval 1 year) WHERE a.date = curdate(); now giving +--------+------------+-------------+------------+---------------+------------+----------------+------------+---------------+ | symbol | today | today_price | d7 | last_wk_price | d30 | last_mth_price | d365 | last_yr_price | +--------+------------+-------------+------------+---------------+------------+----------------+------------+---------------+ | ABC | 2022-09-30 | 19.24 | 2022-09-23 | 5.96 | 2022-08-30 | 5.30 | 2021-09-30 | 3.86 | | DEF | 2022-09-30 | 19.52 | 2022-09-23 | 10.15 | 2022-08-30 | 7.61 | 2021-09-30 | 16.59 | | XYZ | 2022-09-30 | 15.00 | 2022-09-25 | 2.87 | 2022-08-30 | 15.11 | 2021-09-29 | 4.20 | +--------+------------+-------------+------------+---------------+------------+----------------+------------+---------------+
-
A better data type for prices is decimal EG current_price DECIMAL(10,2) , Also your dates should be stored as DATE type (yyyy-mm-dd), not int. Then you get maximum utitlity from all the datetime functions. What query have you tried so far? Do you have data for every day going back 1 year or more?
-
function is_leapyear($year){ if(is_numeric($year)){ if($year%4 == 0) { // if it's divisible by 4 its probably a leapyar if($year%100 == 0) { // but check the century condition return ($year%400 == 0); // if its divisible by 400 return true otherwise return false } else return true; } } return false; // if we get to here, return false }
-
try <?php if (isset($_GET["leapyear"]) && !empty($_GET['leapyear'])) { $result = (is_leapyear($_GET["leapyear"])) ? "<span style=\"color:#008631;\">{$_GET['leapyear']} is a leap year</span>" : "<span style=\"color:#FF0000;\">{$_GET['leapyear']} is not a leap year</span>" ; } else $result = '' ; function is_leapyear($year){ if(is_numeric($year)){ if($year%4 == 0) { if($year%100 == 0) { return ($year%400 == 0); } else return true; } } return false; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Leap year form</title> </head> <body> <?= $result ?> <h1>Leap Year</h1> <form method = "get" > <label for="leapyear">Enter a year</label> <input type="text" name="leapyear" id="leapyear" autofocus/> <p><input type="submit" name="confirm" value="Check For Leap Year" /></p> </form> </body> </html>
-
Moved to JS forum