-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Position looks fine - after accumulating the data into the array but before outputting the data from the array. If you are getting repeated items it could be the data. Can you post the output from echo '<pre>',print_r($data, true),'</pre>';
-
That looks very much like MS SQL Server to me.
-
Are you formatting the price fields with commas in the query? If so 1,850.00 is treated as 1 by PHP (up to the first non-numeric character)
-
$grandTotal = 0; // initialise to 0 while (odbc_fetch_row($result)) { $scanner=odbc_result($result,"tuser"); $Item=odbc_result($result,"item"); $Qty=odbc_result($result,"qty"); $Price=odbc_result($result,"TotalPrice"); $grandTotal += $price; // accumulate price echo "<tr><td>$scanner</td>"; echo "<td> ◃ </td>"; echo "<td>$Item</td>"; echo "<td> ▹ </td>"; echo "<td>$Qty</td>"; echo "<td align='right'>$Price</td></tr>"; } echo "Grand Total: $grandTotal"; // output grand total
-
You have only defined the function to validate input, you haven't called it
-
Test data +------------------+-------+---------+----------+---------+ | submittedform_id | empid | empname | formname | scoring | +------------------+-------+---------+----------+---------+ | 1 | 1 | James | Form A | 4 | | 2 | 1 | James | Form B | 9 | | 3 | 1 | James | Form C | 8 | | 4 | 2 | Foo | Form A | 12 | | 5 | 2 | Foo | Form D | 10 | | 6 | 3 | bar | Form C | 30 | | 7 | 3 | bar | Form A | 5 | | 8 | 4 | Peter | Form B | 10 | | 9 | 4 | Peter | Form C | 4 | | 10 | 5 | Paul | Form A | 5 | | 11 | 5 | Paul | Form D | 12 | | 12 | 5 | Paul | Form B | 8 | | 13 | 6 | Mary | Form A | 9 | | 14 | 6 | Mary | Form B | 11 | | 15 | 7 | Jenny | Form B | 15 | | 16 | 7 | Jenny | Form C | 8 | | 17 | 8 | Steve | Form B | 9 | | 18 | 8 | Steve | Form D | 15 | | 19 | 9 | Tom | Form A | 12 | | 20 | 9 | Tom | Form C | 8 | | 21 | 10 | Zak | Form C | 9 | | 22 | 10 | Zak | Form D | 8 | +------------------+-------+---------+----------+---------+ 10 employees therefore top 20% are the two with highest scores SELECT sf.empid , sf.empname , sf.formname , sf.scoring , total , rank , pos , numemps FROM submittedform sf INNER JOIN ( SELECT empid , total , @pos := @pos + 1 as pos , @rank := IF(total=@prevtot, @rank, @pos) as rank , @prevtot := total FROM ( SELECT empid , SUM(scoring) as total FROM submittedform INNER JOIN (SELECT @pos:=0, @rank:=1,@prevtot:=0) as init GROUP BY empid ORDER BY SUM(scoring) DESC ) tot ) posn ON sf.empid = posn.empid CROSS JOIN ( SELECT COUNT(DISTINCT empid) as numemps FROM submittedform ) as ct WHERE CEIL(rank*100/numemps) <= 20 ORDER BY total DESC; +-------+---------+----------+---------+-------+------+------+---------+ | empid | empname | formname | scoring | total | rank | pos | numemps | +-------+---------+----------+---------+-------+------+------+---------+ | 3 | bar | Form C | 30 | 35 | 1 | 1 | 10 | | 3 | bar | Form A | 5 | 35 | 1 | 1 | 10 | | 5 | Paul | Form A | 5 | 25 | 2 | 2 | 10 | | 5 | Paul | Form D | 12 | 25 | 2 | 2 | 10 | | 5 | Paul | Form B | 8 | 25 | 2 | 2 | 10 | +-------+---------+----------+---------+-------+------+------+---------+
-
The top 20% are not those that got 20% (or more) of the total. In your example, the top 66% got 40% (or more)
-
Assuming the data looks something like this +--------+----------+-------------+ | userid | username | childcareid | +--------+----------+-------------+ | 1 | Albert | 1 | | 2 | Betty | 2 | | 3 | Charlie | 3 | | 4 | Diane | 1 | | 5 | Edward | 2 | | 6 | Fiona | 3 | | 7 | Graham | 1 | | 8 | Helen | 2 | | 9 | Ian | 3 | | 10 | Jane | 1 | +--------+----------+-------------+ then the query you want is (if logged in user has id=1) SELECT u2.username , u2.childcareid FROM users u1 INNER JOIN users u2 USING (childcareid) WHERE u1.userid = 1 -- logged user +----------+-------------+ | username | childcareid | +----------+-------------+ | Albert | 1 | | Diane | 1 | | Graham | 1 | | Jane | 1 | +----------+-------------+
-
NOTE: usort() above should be uasort() to preserve the empid values. This will give the top 20 % uasort($data, function($a,$b){return $b['total']-$a['total'];}); $pcent20 = ceil(count($data)/5); $data = array_slice($data, 0, $pcent20, true);
-
Sorting the $data array prior to output will give the descending totals usort($data, function($a,$b){return $b['total']-$a['total'];}); Where did that suddenly spring from?
-
You have coded it to give an error only when the name contains a single space if($_POST["fname"] == " ") { $errors['fname']="<font color ='red'> Please enter your first name </font>"; }
-
Strictly speaking, it ain't broken but a simple display_form(); would do exactly the same as if(count($errors) !=0) { display_form(); } else display_form(); The question is whether or not you want to call the function at all, as you don't appear to have a definition of that function. Should there be a display_form() function? Only you know that - we do not know what you intend to do.
-
The structure is ok, it's the logic using it that is questionable.
-
Do you think that may be because you haven't defined a function called display_form()? And what is purpose of the if() statement here when you do exactly the same regardless of the outcome? if(count($errors) !=0) { display_form(); } else display_form();
-
You really should normalize your data. Relational databases are not for storing spreadsheets. That said, with the data you currently have, your first query would be SELECT DISTINCT country FROM extras ORDER BY country When you know the country, the next query would be SELECT DISTINCT destination FROM extras WHERE country = ? ORDER BY destination ... and so on
-
The most common cause of that error is an opening "{" without a corresponding closing "}" [edit] ... or, in your case, two
-
Unwanted parenthesis else(display_form();
-
What is the best method to write long if - else conditions?
Barand replied to thara's topic in PHP Coding Help
As you are testing for a range of values (with <= ) then either is fine, although my preference would be "ifelse" in this instance. If you were using "=" then I would definitely go for an array, as then you could just use $cssClass = $mapping[$emission]; That's my 0.02 worth. -
You use LIKE with wildcards when you are searching for something that contains the search string. If you are looking for an exact match use "=". In this case it could be that perhaps the title in the db has extra space character at the end. Try this with your current query $LIKE ="%rammstein%"; If that fails it could be you are using a case-sensitive collation.
-
Then you only need one query $db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE); $sql = "SELECT empid , empname , formname , scoring FROM submittedform ORDER BY empid, formname"; $data = []; $res = $db->query($sql); // // process query, store in array by employee // while (list($eid, $ename, $fname, $score) = $res->fetch_row()) { if (!isset($data[$eid])) { // initialize array for employee $data[$eid] = ['emp' => $ename, 'total' => 0, 'items' => [] ]; } // accumulate item data $data[$eid]['items'][] = ['form' => $fname, 'score' => $score]; $data[$eid]['total'] += $score; } // // Now output the array // echo <<<TABLE <table border="1"> <tr> <th>Employee ID</th> <th>Employee Name</th> <th>Grand Total</th> <th>Forms</th> <th>Form Scores</th> </tr> TABLE; foreach ($data as $eid => $edata) { // how many items $numItems = count($edata['items']); echo "<tr> <td rowspan='$numItems'>$eid</td> <td rowspan='$numItems'>{$edata['emp']}</td> <td rowspan='$numItems'>{$edata['total']}</td>"; foreach ($edata['items'] as $k => $idata) { if ($k > 0) { echo "<tr>"; } echo "<td>{$idata['form']}</td><td>{$idata['score']}</td></tr>"; } } echo "</table>";
-
Don't run queries inside loops, it's a performance killer. And in this case, why would you want to? You would use an ajax request to get the details you want.
-
Here's one to start with http://forums.phpfreaks.com/topic/293179-reseting-of-passwords/?do=findComment&comment=1500065
-
Where are you getting $user_info? You were originally using $context['user']['mentions'] when you called the function, why have you changed it?
-
Yes, you are correct. It does return a count of 1. So changing the query to see what it is counting gives SELECT * FROM `ebspma_paad_ebspma`.`req_material_reserva` WHERE `ebspma_paad_ebspma`.`req_material_reserva`.`idsala` = 61 AND (((`ebspma_paad_ebspma`.`req_material_reserva`.`idtempoInicio` BETWEEN 3 AND 3) AND (`ebspma_paad_ebspma`.`req_material_reserva`.`idTempoFim` BETWEEN 3 AND 3)) OR (`ebspma_paad_ebspma`.`req_material_reserva`.`idtempoInicio` <= 3 AND `ebspma_paad_ebspma`.`req_material_reserva`.`idTempoFim` >= 3)) AND `ebspma_paad_ebspma`.`req_material_reserva`.`data` = "2015-10-23" +-----------+--------------+--------+---------------+------------+---------------+------------+ | idreserva | idutilizador | idsala | idtempoInicio | idtempoFim | idequipamento | data | +-----------+--------------+--------+---------------+------------+---------------+------------+ | 3 | 719 | 61 | 3 | 3 | NULL | 2015-10-23 | +-----------+--------------+--------+---------------+------------+---------------+------------+ which. I believe, explains the count of 1