-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
Are you using MySql or MariaDB? -
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
-
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
It's SQL (Structured Query Language - the language that all database queries use) Neither PDO nor mysqli - it will work with either. The '@' prefix is used for user variables. In this case I use those @seq, @rank, @prevr and @prevd for the sequence count, rank, previous record's received value and previous record's donation value so that I can store the values in one record and refer to them in the next one. The := is the assignent operator for user variables in SQL. -
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
@kicken Nice. I'll have to have another go at installing v8.0 as I mentioned, that was the quick and dirty way. IMO a better way (pre v8.0) is as below. In my tests, this method was typically around 6x faster on that small dataset. Savings would be greater with larger sets. Quick n dirty : 0.0034 seconds Long n clean : 0.0005 seconds SQL SELECT name , @seq := @seq + 1 as seq , @rank := CASE WHEN total_received = @prevr THEN CASE WHEN total_donated = @prevd THEN @rank ELSE @seq END ELSE @seq END as rank , @prevr := total_received as received , @prevd := total_donated as donated FROM ( SELECT name , total_donated , total_received FROM donation WHERE total_participant = 1 ORDER BY total_received DESC, total_donated ASC -- LIMIT 18446744073709551615 --(required by mariaDB users only - bug workaraound) ) ordered JOIN ( SELECT @rank := 0, @seq := 0, @prevd := 0, @prevr := 0 ) init ; Sample results +---------+------+------+----------+----------+ | name | seq | rank | received | donated | +---------+------+------+----------+----------+ | Dasher | 1 | 1 | 500 | 250 | | Cupid | 2 | 2 | 400 | 370 | | Prancer | 3 | 2 | 400 | 370 | | Comet | 4 | 4 | 380 | 370 | | Dancer | 5 | 5 | 200 | 510 | | Vixen | 6 | 6 | 100 | 200 | | Donner | 7 | 7 | 100 | 510 | +---------+------+------+----------+----------+ -
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
Perhaps SELECT * FROM donation; +----+---------+---------------+----------------+-------------------+ | id | name | total_donated | total_received | total_participant | +----+---------+---------------+----------------+-------------------+ | 1 | Comet | 370 | 380 | 1 | | 2 | Cupid | 370 | 400 | 1 | | 3 | Donner | 510 | 100 | 1 | | 4 | Blitzen | 480 | 350 | 0 | | 5 | Dancer | 510 | 200 | 1 | | 6 | Prancer | 370 | 400 | 1 | | 7 | Dasher | 250 | 500 | 1 | | 8 | Vixen | 200 | 100 | 1 | +----+---------+---------------+----------------+-------------------+ SELECT name , total_received , total_donated , ( SELECT COUNT(*)+1 FROM donation rd2 WHERE ( rd2.total_received > rd1.total_received OR ( rd2.total_received = rd1.total_received AND rd2.total_donated < rd1.total_donated ) ) AND total_participant = 1 ) AS rank FROM donation rd1 WHERE total_participant = 1 ORDER BY total_received DESC, total_donated ASC +---------+----------------+---------------+------+ | name | total_received | total_donated | rank | +---------+----------------+---------------+------+ | Dasher | 500 | 250 | 1 | | Cupid | 400 | 370 | 2 | | Prancer | 400 | 370 | 2 | | Comet | 380 | 370 | 4 | | Dancer | 200 | 510 | 5 | | Vixen | 100 | 200 | 6 | | Donner | 100 | 510 | 7 | +---------+----------------+---------------+------+ -
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
They are table aliases. Easier to write rd2.total_received than random_donation_clash2.total_received -
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
Here's a quick and dirty solution SELECT users_name , total_donated , total_received , ( SELECT COUNT(*)+1 FROM random_donation_clash rd2 WHERE rd2.total_received > rd1.total_received ) AS rank FROM random_donation_clash rd1 WHERE total_participant = 1 ORDER BY total_received DESC Still using mysqli !? -
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
At present it's neither - just a typed example -
how to count the number of rows preceding a given row?
Barand replied to alexandre's topic in PHP Coding Help
Are you saying you want to know the ranking of each amount? EG +----------+-----------+------+ | Name | Donation | Rank | +----------+-----------+------+ | Comet | 650 | 1 | | Cupid | 510 | 2 | | Donner | 510 | 2 | | Blitzen | 480 | 4 | | Dancer | 410 | 5 | | Prancer | 370 | 6 | | Dasher | 250 | 7 | | Vixen | 200 | 8 | +----------+-----------+------+ -
See my example. Use name='amt[$i]' and not name='amt_".$i."'
-
<?= $x ?> is a shorthand version of <?php echo $x ?> $x ?? 0 uses the null coalesce operator (??) and is an alternative to ( isset($x) ) ? $x : 0; In other words, if $x exists, use it, otherwise default to "0";
-
Set the "selected" attributes of the options containing the selected values in each dropdown For example <?php if ($_SERVER['REQUEST_METHOD']=='POST') { echo "You ordered the following quantities<br>"; foreach ($_POST['qty'] as $i => $qty) { if ($qty) { echo "Item $i : $qty<br>"; } } echo "<hr>"; } function qtyOptions($current) { $opts = ''; for ($i=0; $i<11; $i++) { $sel = $i==$current ? 'selected' : ''; $opts .= "<option $sel value='$i'>$i</option>\n"; } return $opts; } ?> <!DOCTYPE html> <html lang='en'> <head> <title>Example</title> <meta charset='utf8'> </head> <body> <form method='POST'> Item 1 <select name='qty[1]'><?= qtyOptions($_POST['qty'][1] ?? 0)?></select> <br> Item 2 <select name='qty[2]'><?= qtyOptions($_POST['qty'][2] ?? 0)?></select> <br> Item 3 <select name='qty[3]'><?= qtyOptions($_POST['qty'][3] ?? 0)?></select> <br> <br> <input type='submit'> </form> </body> </html>
-
How to convert text string into associative array using delimiters?
Barand replied to Darkwoods's topic in PHP Coding Help
explode() on the "|" to get the 3 main sections explode each of those on "=" to get the key and the values explode the values using "," -
I said the superfluous one was on line 96. The curly bracket was being opened on 20 and should've been closed at the end of line 21. Because of the missing one on 21, the next one that the { on line 20 could be paired with was the one on line 75. This was completely screwing up the logic and processing flow of your code so lines that should have executed didn't - hence some variables weren't defined.
-
I copied the code you posted and got all your errors on fname etc. I then made those 2 changes and all the errors went away. This is your code with those two { } errors fixed. This doesn't mean you have more that aren't showing yet. <?php session_start(); error_reporting (E_ALL); ini_set('display_errors','1'); $errors = array(); #require "conn/connect_seniorform.php"; // MORE PHP CODE IF ANY if($_SERVER["REQUEST_METHOD"] == "POST") { // read the inputs if (isset ($_POST['fname'])){ $fname = htmlentities($_POST['fname'],ENT_QUOTES);} if (isset ($_POST['sname'])){ $sname = htmlentities($_POST['sname'],ENT_QUOTES);} if (isset ($_POST['email'])){ $email = htmlentities($_POST['email'],ENT_QUOTES);} if (isset ($_POST['addr'])){ $addr = htmlentities($_POST['addr'],ENT_QUOTES);} if (isset ($_POST['phone'])){ $phone = htmlentities($_POST['phone'],ENT_QUOTES);} if (isset ($_POST['mob'])){ $mob = htmlentities($_POST['mob'],ENT_QUOTES); } if (isset($_POST['bmfa_no'])){ $bmfa_no = htmlentities($_POST['bmfa_no'],ENT_QUOTES);} if (isset($_POST['caa_no'])){ $caa_no = htmlentities($_POST['caa_no'],ENT_QUOTES);} if (isset ($_POST['rewd'])){ $rewd = htmlentities($_POST['rewd'],ENT_QUOTES);} if (isset ($_POST['fam'])){ $fam = htmlentities($_POST['fam'],ENT_QUOTES);} if (isset($_POST['ctry'])){ $ctry = htmlentities($_POST['ctry'],ENT_QUOTES);} if (isset($_POST['ctry_bmfa'])){ $ctry_bmfa = htmlentities($_POST['ctry_bmfa'],ENT_QUOTES);} if (isset ($_POST['bdf'])){ $bdf = htmlentities($_POST['bdf'],ENT_QUOTES);} if (isset($_POST['payopt'])){ $payopt = htmlentities($_POST['payopt'],ENT_QUOTES);} if (isset ($_POST['payopt'])){ $pay = htmlentities($_POST['pay'],ENT_QUOTES);} if (isset ($_POST['date'])){ $date = htmlentities($_POST['date'],ENT_QUOTES);} if (isset ($_POST['sign'])){ $sign = htmlentities($_POST['sign'],ENT_QUOTES);} // process the data now if (count($errors) == 0) { $sql = "INSERT INTO senior_dat(fname,sname,email,dob,addr,phone,mob,bmfa_no,caa_no,rewd,fam,ctry,ctry_bmfa,bdf,payopt,pay,date,sign) VALUES (:fname, :sname, :email,:dob, :addr, :phone, :mob, :bmfa_no, :caa_no, :rewd, :fam, :ctry, :ctry_bmfa, :bdf, :payopt, :pay, :date, :sign)"; $stmt = $pdo->prepare($sql); $parms = array( 'fname'=>$fname, 'sname'=>$sname, 'email'=>$email, 'addr'=>$addr, 'phone'=>$phone, 'mob'=>$mob, 'bmfa_no'=>$bmfa_no, 'caa_no'=>$caa_no, 'rewd'=>$rewd, 'fam'=>$fam, 'ctry'=>$ctry, 'ctry_bmfa'=>$ctry_bmfa, 'bdf'=>$bdf, 'payopt'=>$payopt, 'pay'=>$pay, 'date'=>$date, 'sign'>$sign ); if (!$stmt->execute($parms)) $errors[] = "Insert query did not run"; else $errors[] = "Your entries have been accepted"; } } else // no inputs yet { $fname = ''; $sname = ''; $email = ''; $addr = ''; $phone = ''; $mob = ''; $bmfa_no = ''; $caa_no = ''; $rewd = ''; $fam = ''; $ctry = ''; $ctry_bmfa = ''; $bdf = ''; $pay_opt = ''; $pay = ''; $date = ''; $sign = ''; } // Done with the inputs - redisplay the form screen with the messages we produced. // // implode the errrors array to show the result message or the errors $errmsg = implode('<br>', $errors); echo " <!DOCTYPE html> <html lang='en'> <head> <style type='text/css'> #form_box { position:relative; float:left; margin:3% 1%; padding:5px; border:2px solid black; } .red{color:red;} </style> </head> <body> "; echo " <div id='form_box'> <center> <h1>SENIOR RENEWAL FORM</h1> </center> "; if(!empty($errmsg)) echo "<p class='red'>$errmsg</p>"; echo " <form method='POST'> <label><b>First Name: </b> <br> <input type='text' name='fname' size='20' maxlength='40' value='$fname' required> </label> <br> <label><b>Surname: </b> <br> <input type='text' name='sname' size='20' maxlength='40' value='$sname' required> </label> <br> <label><b>Email: </b> <br> <input type='text' name='email' size='20' maxlength='40' value='$email' required> </label> <br><br> <label><b>Address: </b> <br> <input type='textarea' name='addr' rows='4' cols='50 value='$addr' required> </label> <br> <label><b>Phone number:</br> <input type='tel' name='phone' 'min=12' max=16' value='$phone' required> </label> <br> <label> <b>Mobile number:</br> <input type='tel' name='mob' 'min=12' 'max=16' value'$mob' required> </label> <br> <label BMFA No: <input type='number' name='bmfa_no' value='$bmfa_no' required> </label> <br> <label CAA Operator No: <input type='text' name='caa_no' value='$caa_no' required><br> </label> <label Do you wish that the club obtains your BMFA membership, Insurance & CAA renewal: <select name='club_membership' id='club_membership'> <option value='Select Yes or No'> </option> <option value='NO'>NO</option> <option value='YES'>YES</option> </select> </label> <br> <label for='Ctry'>Are you a Country Member:</label> <select name='Ctry' id='Ctry'> <option value='blank'> </option> <option value='NO'>NO</option> <option value='YES'>YES</option> </select> <br> <label for='caa'>If you answered YES to the previous question,. have you a current BMFA and CAA membership:</label> <select name='caa' id='caa'> <option value='blank'> </option> <option value='NO'>NO</option> <option value='YES'>YES</option> <option value='na'>N/A</option> </select> <br> <label for='rewd'>Do you wish to purchase a BMFA Membership/Reward Card:</label> <select name='rewd' id='rewd'> <option value='blank'> </option> <option value='NO'>NO</option> <option value='YES'>YES</option> </select> <br> <label for='fam'>Are you a family member:</label> <select name='fam' id='fam'> <option value='blank'> </option> <option value='NO'>NO</option> <option value='YES'>YES</option> </select> <br> <label for='bdf'>Do you wish to join the British Drone flyers Association, instead of the BMFA:</label> <select name='bdf' id='bdf'> <option value='blank'> </option> <option value='NO'>NO</option> <option value='YES'>YES</option> </select> <p>Please note that if you join the BDF you can also fly fixed wing/ helicopters as well!</p> <br> <label for='pay_opt'>Ho do you wish to pay:</label> <select name='pay_opt' id='pay_opt'> <option value='blank'> </option> <option value='chq'>CHEQUE</option> <option value='csh'>CASH</option> <option value='bacs'>BACS</option> </select> <br> <p><b>Payment required: £</b><input type='text' name='pay' size='20' maxlength='40'/></p> <br> <p style='background-color:tomato;'>I apply for membership of the 1066 Model Flying Club and agree to abide by the rules. By doing so agree to allow the club to use your details in respect of the GDPR regulations for the processing of your renewal, a copy of which can be found on the club website members page. </p> <br> <label for='date'>Date:</label> <input type='text' id='date' name='date' pattern='[0-9]{2}/[0-9]{2}/[0-9]{4}' required> <br> <label for='sign'>Sign here:</label> <input type='text' id='sign' name='sign' required> <br> <input type='submit' value='Submit'> </form> </div> "; and the error-free output...
-
You have been told exactly where the problem is ...
-
trying to make a div parent of all rows pulled from database
Barand replied to alexandre's topic in PHP Coding Help
You need 3 sets of output... before the loop inside the loop after the loop -
trying to make a div parent of all rows pulled from database
Barand replied to alexandre's topic in PHP Coding Help
At the moment you appear to have something like this start loop { <div> <div> <table> <tr> headings </tr> <tr> data </tr> </table> </div> </div>} } end loop It sound like what you want is this <div> <div> <table> <tr> headings </tr> start loop { <tr> data </tr> } end loop </table> </div> </div>} -
The error is on line 21 - missing a } at the end of the line. The opening { on line 20 is currently matched with the closing } on line 75; You also have a superfluous } on line 96 (so they are all in pairs, jus not matched correctly).