-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Does this make it easier? SELECT a.ledger_code , l.ledger_name , SUM(IF(type='Dr',amount, null)) as Debits , SUM(IF(type='Cr',amount, null)) as Credits FROM acc_tbl a JOIN ledger_tbl l USING (ledger_code) GROUP BY a.ledger_code; +-------------+---------------------+--------+---------+ | ledger_code | ledger_name | Debits | Credits | +-------------+---------------------+--------+---------+ | 18 | Maintenance | 2400 | | | 30 | Annual Subscription | | 300 | | 5 | cash | 300 | 2400 | +-------------+---------------------+--------+---------+
-
I have been looking at your data model - not a pretty sight Foreign keys in a table should match the primary keys of the referenced table. Your primary keys are INT but the foreign keys in the course table are all VARCHAR - they too should be INT. And why is the FK of tblsubject called "courseTitle"? (I couldn't see any relationship between course and subject until I saw the join your query) Date fields should be type DATE (yyyy-mm-dd). (Already changed in my version of tblstudent) Departments belong to a Faculty, so if you know the department you know the faculty. There is no need for "facultyid" in tblcourse or tblstudent. tblsession is only referenced by tblstudent. I would have thought there is a relationship between session (eg 2021/2022) and semester. Other than via the class/level there is, surprisingly, no relationship between student and course which, to me, seems the main relationship in an educational environment. Do your courses only last for a single semester? Are course and course unit part of the same entity or does a course have many units? YOUR CODE I haven't got it to run yet as changes are required. Your form has Level/Semester/Faculty put your post is expected to contain Level/Semester/Department/Faculty. For reasons known only to you, the semesterId is assigned to $sessionId. The form input should be Department instead of Faculty as faculty is redundant if you have the department. The query will change accordingly. As you only want to display data, POST should be GET.
-
It seems mathematics and Year 7 are a poor example if everyone in Year 7 takes mathematics. In Years 10/11/12 where some students take Science, some Arts and some Commercial, how do you then know who takes which subject? (I may know my way around databases but I do not know the relationships between all the entities in your specific scenario.)
-
I ran your query, providing some values in the WHERE clause SELECT tblcourse.Id, tblcourse.courseTitle, tbllevel.levelName, tblfaculty.facultyName, tbldepartment.departmentName, tblsemester.semesterName, tblcourse.levelId, tblcourse.semesterId, tblcourse.facultyId, tblcourse.departmentId, tblsubject.Subjects from tblcourse INNER JOIN tbllevel ON tbllevel.Id = tblcourse.levelId INNER JOIN tblsubject ON tblsubject.id = tblcourse.courseTitle INNER JOIN tblsemester ON tblsemester.Id = tblcourse.semesterId INNER JOIN tblfaculty ON tblfaculty.Id = tblcourse.facultyId INNER JOIN tbldepartment ON tbldepartment.Id = tblcourse.departmentId where tblcourse.levelId ='3' and tblcourse.semesterId ='1' and tblcourse.departmentId ='7' and tblcourse.facultyId ='9'; The output contains a single "class", ie Year 7... +----+-------------+-----------+-------------+----------------+--------------+---------+------------+-----------+--------------+------------------------------+ | Id | courseTitle | levelName | facultyName | departmentName | semesterName | levelId | semesterId | facultyId | departmentId | Subjects | +----+-------------+-----------+-------------+----------------+--------------+---------+------------+-----------+--------------+------------------------------+ | 21 | 1 | Year 7 | Adventurous | Year 7 | First Term | 3 | 1 | 9 | 7 | English Language | | 22 | 2 | Year 7 | Adventurous | Year 7 | First Term | 3 | 1 | 9 | 7 | Mathematics | | 31 | 12 | Year 7 | Adventurous | Year 7 | First Term | 3 | 1 | 9 | 7 | Yoruba | | 23 | 13 | Year 7 | Adventurous | Year 7 | First Term | 3 | 1 | 9 | 7 | French | | 24 | 15 | Year 7 | Adventurous | Year 7 | First Term | 3 | 1 | 9 | 7 | Basic Science and Technology | | 32 | 16 | Year 7 | Adventurous | Year 7 | First Term | 3 | 1 | 9 | 7 | History | | 25 | 19 | Year 7 | Adventurous | Year 7 | First Term | 3 | 1 | 9 | 7 | Business Studies | +----+-------------+-----------+-------------+----------------+--------------+---------+------------+-----------+--------------+------------------------------+ Can you explain better just what the problem is that you are having? You gave the impression that all classes were being output.
-
Just pass the array to the function as one of the arguments $array = [5, 10, 20]; echo array_sum($array); // 35
-
Applying the same logic to the SQL... Table:file +----+-------+------------+ | id | fname | date | +----+-------+------------+ | 1 | FileA | 2022-08-23 | | 2 | FileB | 2022-08-24 | | 3 | FileC | 2022-08-25 | +----+-------+------------+ $res = $pdo->query("SELECT fname FROM file WHERE NOW() BETWEEN date AND date + INTERVAL 1 DAY + interval 14 HOUR - INTERVAL 1 SECOND "); foreach ($res as $r) echo $r['fname'] . '<br>'; Output FileB
-
We haven't a clue what your schema looks like. If I were doing this from the schema at the bottom of this page ( http://barringtondrew.co.uk/?page=3 ) I would use this query... mysql> SELECT classname -> , GROUP_CONCAT(DISTINCT subject ORDER BY subject SEPARATOR ', ') as subjects -> FROM class c -> JOIN pupil USING (classid) -> JOIN choice USING (pupilid) -> JOIN subject USING (subjectid) -> GROUP BY classname; +-----------+--------------------------------------------------------------------------------------+ | classname | subjects | +-----------+--------------------------------------------------------------------------------------+ | Class A | Biology, Computing, Economics, English, Geography, German, History, Maths, Physics | | Class B | Chemistry, Computing, Economics, English, Geography, German, History, Maths, Physics | | Class C | Chemistry, Computing, Economics, English, Geography, German, Maths, Physics | | Class D | Biology, Chemistry, Computing, Economics, English, German, History, Maths, Physics | | Class E | Biology, Computing, Economics, English, Geography, German, History, Maths, Physics | | Class F | Biology, Chemistry, Computing, Economics, Geography, German, History, Maths, Physics | +-----------+--------------------------------------------------------------------------------------+
-
OK. Have you done it?
-
I would change your method. A file dated 24/08/2022 is viewable from 2022-08-24 00:00:00 until 2022-08-25 13:59:59, so if the time now is between those times, show the file. $files = [ 'FileA' => '23/08/2022', 'FileB' => '24/08/2022', 'FileC' => '25/08/2022' ]; foreach ($files as $fname => $date) { $now = new DateTime('now'); $view = viewable($date); if ($view[0] <= $now && $now < $view[1] ) { echo $fname . '<br>'; } } function viewable($date) { $dt = DateTime::createFromFormat('d/m/Y', $date); $dt->setTime(0,0,0); $end = (clone $dt)->add(new DateInterval('P1DT14H')); return [$dt, $end]; }
-
In that case, The time element of $file_date will depend on when you run the code. Which day it appears on will depend on whether you view it in the morning or afternoon
-
The time element of $todays_date will depend on when you run the code $todays_date = DateTime::createFromFormat('d/m/Y', '24/08/2022'); echo $todays_date->format('Y-m-d H:i:s'); // --> 2022-08-24 14:59:27 You only need to set the default timezone once at the top (or in your php.ini file. It will be used automatically by you new DateTime() calls after that. That's why it's called the default. You can compare DateTime objects directly without formatting them if ($todays_date > $yesterday)
-
-
The way to get the Dr and Cr totals is to use a group aggregation query. It seems strange that this wasn't covered in your course. Example mysql> SELECT a.ledger_code -> , type -> , SUM(amount) as total -> FROM acc_tbl a -> GROUP BY ledger_code, type; +-------------+------+-------+ | ledger_code | type | total | +-------------+------+-------+ | 18 | Dr | 2400 | | 30 | Cr | 300 | | 5 | Cr | 2400 | | 5 | Dr | 300 | +-------------+------+-------+
-
Sorry, I gave it a try but I couldn't come up with a query that gave your expected results from the data provided. (Perhap it's because pictures of data don't load very well into my test database) I could only get +-------------+---------+---------+ | ledger_code | Debit | Credit | +-------------+---------+---------+ | 5 | 300.00 | 2400.00 | | 18 | 2400.00 | | | 30 | | 300.00 | | | 2700.00 | 2700.00 | +-------------+---------+---------+ What queries have you tried?
-
(float) is casting $thenumber as a float. But number_format() function definition (from the manual) is number_format( float $num, int $decimals = 0, string|null $decimal_separator = ".", string|null $thousands_separator = ",") : string therefore $thenumber is coerced to float type anyway. So, as demonstrated above, it makes no difference.