Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Sort the array first. Assuming you start with ... $options = [ [ 'type' => 'Visual disability', 'name' => 'Audio-described cut-scenes' ], [ 'type' => 'Visual disability', 'name' => 'Highlighted path to follow' ], [ 'type' => 'Physical disability', 'name' => 'Sensitivity settings for all the controls' ], [ 'type' => 'Visual disability', 'name' => 'Screen readers on menus' ], [ 'type' => 'Visual disability', 'name' => 'Slow down the game speed' ], ]; then ... # # Sort the array by type (descending) # usort($options, function($a, $b) { return $b['type'] <=> $a['type']; }); # # process the array # $prev_type = ''; // store previous type echo "<ul>\n"; foreach ($options as $opt) { if ($opt['type'] != $prev_type) { // is this a new type? if ($prev_type != '') { // was there a previous one? echo "</ul>\n</li>\n"; // if so, close it } echo "<li>{$opt['type']}\n<ul>\n"; $prev_type = $opt['type']; // store as previous value } echo "<li>{$opt['name']}</li>\n"; } // close last group echo "</ul>\n</li>\n"; // close whole list echo "</ul>\n"; giving ... <ul> <li>Visual disability <ul> <li>Audio-described cut-scenes</li> <li>Highlighted path to follow</li> <li>Screen readers on menus</li> <li>Slow down the game speed</li> </ul> </li> <li>Physical disability <ul> <li>Sensitivity settings for all the controls</li> </ul> </li> </ul> An alternative approach is to reorganise the array using subarrays for each type... $options = [ 'Visual disability' => [ 'Audio-described cut-scenes', 'Highlighted path to follow', 'Screen readers on menus', 'Slow down the game speed' ], 'Physical disability' => [ 'Sensitivity settings for all the controls' ] ]; then use two nested foreach() loops.
  2. If that is "fixed" then your testing is a little lax. Have you checked if the record is actually being updated (by magic, no doubt, as no query is executed)?
  3. You can store images in a database but it's not advised. Best way is store the image file in the file system and store the image location and metadata in the database.
  4. $this->pdo now contains your pdo connection. so $db = new DB; $stmt = $db->pdo->prepare(...) You should extra code to your class to set the connection's charset (UTF-8) and to set PDO::ATTR_EMULATE_PREPARES to false. It's also useful to set the PDO::ATTR_DEFAULT_FETCH_MODE attribute also.
  5. Change to $id = $_GET['id'] ?? DEFAULT_ID;
  6. Probably because the inner loop's result object is overwriting the outer result object as you store them both in $query. But don't run queries inside loops like that . Use a single query with a join to get all the data in a single query. It's far more efficient. SELECT faqc_name , faq_id , faq_question FROM faq_cats c JOIN faqs f ON f.faq_cat = c.faqc_id WHERE faqc_site = :site ORDER BY faqc_id , faq_id
  7. PHP has array_product(). EG echo array_product([1, 2, 3, 8]); // 48 If you want to be able to supply separate arguments to a function instead of an array EG echo mul(1, 2, 3, 2, 4); then you can define the function as function mul(...$a) { return array_product($a); } echo mul(3,8,2); //48 echo mul(1,2,3,2,2,2) //48
  8. https://lmgtfy.app/?q=Epidemiology+Databases
  9. All you need is $insert_array = [ 'site_name' => 'asdfasdf', 'site_address' => 'Asdffads', 'description' => 'Asdfs', 'contact_phone' => 'Asdfsdaf' ]; $stmt = $conn->prepare("INSERT INTO Sites (site_name, site_address, description, contact_phone) VALUES (:site_name, :site_address, :description, :contact_phone) "); $stmt->execute($insert_array);
  10. One way would be to rename your input fields to something like name = 'jour[$jour][workday]' name = 'jour[$jour][startTime]' name = 'jour[$jour][endTime]' your posted data will then be nicely grouped Array ( [jour] => Array ( [1] => Array ( [workday] => 01/12/2020 [startTime] => 08:00 [endTime] => 06:00 ) [2] => Array ( [workday] => 02/12/2020 [startTime] => 08:30 [endTime] => 06:30 ) [3] => Array ( [workday] => 03/12/2020 [startTime] => 09:00 [endTime] => 04:30 ) . . . )
  11. You are calculating the rank out of the whole population, not just the class. You need to include the class (armslevelid) in the groupings giving EDIT -- use code tags (<> button)
  12. Using your new data I get this (below) for student 1398... How are you getting the "sub-position"?
  13. 9208 has 4 students with a higher grade and therefore must be rank"5" and not "4". (If 4 people beat you in a race, you are fifth, even if the 4 winners have the same times) As the rank is either equal to the previous rank or it is the sequence number, there is no way a rank will be more than the number of students.
  14. I give those with same total the same rank, otherwise they are ranked by their position in the sorted (DESC) results This the bit of the SQL that does it , @seq := CASE WHEN yss <> @prev THEN 1 ELSE @seq + 1 END as seq , @rank := CASE WHEN total = @prevtot THEN @rank ELSE @seq END as rank , @prevtot := total as total , @prev := yss as yss
  15. Here's my attempt function TVWeek($d = null) { $dt = new DateTime($d); $y = $dt->format('Y'); $mon1 = new DateTime("$y-01-01"); // last week of year condition ++$y; $nextd1 = new DateTime("$y-01-01"); if ($dt->format('W') == $nextd1->format('W')) return 1; if ($mon1->format('w') != 1) { $mon1->modify('last monday'); } return intdiv( $mon1->diff($dt)->days, 7 ) + 1; } echo '<br>' . TVWeek(); // 1 echo '<br>' . date('W'); // 53 echo '<br>' . TVWeek('2020-06-01'); // 23 echo '<br>' . date('W', strtotime('2020-06-01')); // 23 echo '<br>' . TVWeek('2016-06-01'); // 23 echo '<br>' . date('W', strtotime('2016-06-01')); // 22
  16. In fact, remove reference to semester completely from the query. As you are no longer grouping by it, any values will be arbitrary and meaningless.
  17. then remove the semester from the groupings, so you get totals by year only
  18. How many records have you where the semesterid is equal 1 and 2 and 3 all at the same time? Try and semesterid in (1,2,3) i.e. 1 OR 2 OR 3 PS as those are the only three possible values, you could just leave that condition out ot the query.
  19. I haven't used a "student" table, but yes, you should have one. I am surprised you don't have one already (it's one of you basic entities). (I do have a subquery with the alias "student" which contains the total students for each group.) Suggested: +----------------+ +-------------+ | student | | subject | +----------------+ +-------------+ | regno PK |---------+ +----------| subjectid PK| | firstname | | | | subject | | lastname | | +-------------+ | +-------------+ | armsid | | | result | | | armslevelid | | +-------------+ | +----------------+ | | result_id PK| | +----------<| regno | | +--------------+ +--------<| year | | | year | +--------<| semester | | +--------------+ | | subjectid |>--------+ | yearid PK |-----+ | | result_type | | startdate | | +---------------+ | | pcent | | enddate | | | semester | | +-------------+ +--------------+ | +---------------+ | +---<| yearid PK |>-----+ | semester PK |>-----+ | startdate | | enddate | +---------------+
  20. 1 ) You want to rank the total score so you need to get that in the lowest level subquery 2) you can't apply the grade to the total score (ranges don't apply). I have applied it to the students' average scores. Hopefully, this is what you want... +--------+------------+-------+-------+------+----------------+---------------+-----------+ | yearid | semesterid | regno | total | rank | Total students | Average grade | comment | +--------+------------+-------+-------+------+----------------+---------------+-----------+ | 1 | 1 | 4663 | 865 | 2 | 2 | B2 | V Good | | 1 | 1 | 6073 | 969 | 1 | 2 | A | Excellent | +--------+------------+-------+-------+------+----------------+---------------+-----------+ Query SELECT yearid , semesterid , regno , total , rank , numstudents as `Total students` , grade as `Average grade` , comment FROM ( SELECT yearid , semesterid , @seq := CASE WHEN yss <> @prev THEN 1 ELSE @seq + 1 END as seq , @rank := CASE WHEN total = @prevtot THEN @rank ELSE @seq END as rank , @prevtot := total as total , @prev := yss as yss , regno , armsLevelId , armsid , avgtotal FROM ( SELECT yearid , semesterid , subjectid , concat(yearid, semesterid, armsid, armsLevelId) as yss , regno , SUM(total) as total , ROUND(AVG(total)) as avgtotal , armsLevelId , armsid FROM subject_position GROUP BY yearid, semesterid, armsid, armsLevelId,regno ORDER BY yearid, semesterid, armsid, armsLevelId, total DESC ) sorted JOIN (SELECT @prev := '', @seq := 0, @rank := 0, @prevtot := 0) as init ) ranked JOIN grade ON avgtotal BETWEEN grade.lomark and grade.himark JOIN ( SELECT yearid , semesterid , armsLevelId , armsid , COUNT(DISTINCT regno) as numstudents FROM subject_position GROUP BY yearid, semesterid, armsid, armsLevelId ) students USING (yearid, semesterid, armsid, armsLevelId) WHERE -- regno = 4663 and -- uncomment for individual student armsLevelId='1' and armsId='1' and semesterid='1' and yearid='1' ORDER BY regno;
  21. Use COUNT(DISTINCT regno) as students What as arms and armsLevel?
  22. Looking at you latest output examples I would suggest you lose the "subject_position" table. All you need to store are the highlighted result values below. All the rest (totals, positions) can be derived by querying the data TABLE: result +-------------+--------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------------------+------+-----+---------+----------------+ | result_id | int(11) | NO | PRI | NULL | auto_increment | | regno | int(11) | YES | MUL | NULL | | | year | varchar(45) | YES | | NULL | | | semester | varchar(45) | YES | | NULL | | | subjectid | int(11) | YES | MUL | NULL | | | result_type | enum('CA1','CA2','Exam') | YES | | NULL | | | pcent | int(11) | YES | | NULL | | +-------------+--------------------------+------+-----+---------+----------------+ Note: I have omitted armsid and armsLevel as I haven't clue what they are and where they belong in the schema.
  23. Here's my function for ordinal suffices function ordSuffix($n) { $str = "$n"; $t = $n > 9 ? substr($str,-2,1) : 0; $u = substr($str,-1); if ($t==1) return $str . 'th'; else switch ($u) { case 1: return $str . 'st'; case 2: return $str . 'nd'; case 3: return $str . 'rd'; default: return $str . 'th'; } } echo ordSuffix(1).'<br>'; // 1st echo ordSuffix(11).'<br>'; // 11th echo ordSuffix(101).'<br>'; // 101st echo ordSuffix(2).'<br>'; // 2nd echo ordSuffix(3).'<br>'; // 3rd echo ordSuffix(8).'<br>'; // 8th
  24. Try changing the `tran_term_taxonomy`.`description` = "" to `tran_term_taxonomy`.`description` = '' It won't like you using " in a string enclosed by ".."
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.