This topic is locked
[SOLVED]

count child record and display in List Page of Master record

4/19/2022 8:11:53 AM
PHPRunner General questions
P
ppradhan@live.com author

Hi,
can anybody help me to show 'count of child records' in the master List Page?

Master Table Name: 'Class' with fields class_id, class_name
Detail/Child Table Name: 'Students' with fields std_id, std_name, class_id

All I want is: to count the number of students in detail/child table and display in the List Page of master table: Class.
Expected Output of Master Table: List page =>
Class_id
Class_name
(New Text) Student Number: count the record from child/detail table to display here.

Both tables are in Master Detail Relationship with key as class_id.
I've removed the detail link and counter that came default in the page designer because I'm using vertical layout and the child record appeared randomly.

F
fdiazp 4/19/2022

If in the relation you check count
img alt

other option is in sql of the parent put a (select count(*) from child.id = parent.id ) as qchilds

P
ppradhan@live.com author 4/19/2022

@fdiazp4,
I've removed the icon/detail_link and record counter becuase I'm using vertical layout in the list page which causes issue visually.
I want to use SQL or CustomCode to display count of child record to be displayed in the Master list page.

K
kohle 4/19/2022

Hi,

you can add the count as field to your master sql :
select class_id, class_name, (select count(*) from students where class_id = class.class_id) as number_students
from class

rgs
J.

P
ppradhan@live.com author 4/19/2022

@kohle,
bro I get this error following your suggested way:

Fatal error: Uncaught mysqli_sql_exception: In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'softedsr_office_digi.sch_class_sections.id'; this is incompatible with sql_mode=only_full_group_by in D:\WebServer\htdocs\my_digi\connections\MySQLiConnection.php:157 Stack trace: #0 D:\WebServer\htdocs\my_digi\connections\MySQLiConnection.php(157): mysqli_query(Object(mysqli), 'select count()...') #1 D:\WebServer\htdocs\my_digi\connections\Connection.php(635): MySQLiConnection->query('select count()...') #2 D:\WebServer\htdocs\my_digi\classes\datasource\table.php(639): Connection->getFetchedRowsNumber('SELECT\r\n`sch_cl...') #3 D:\WebServer\htdocs\my_digi\classes\listpage.php(2891): DataSourceTable->getCount(Object(DsCommand)) #4 D:\WebServer\htdocs\my_digi\classes\listpage.php(2292): ListPage->calculateRecordCount() #5 D:\WebServer\htdocs\my_digi\sch_class_sections_list.php(153): ListPage->prepareForBuildPage() #6 {main} thrown in D:\WebServer\htdocs\my_digi\connections\MySQLiConnection.php on line 157

K
kohle 4/19/2022

Copy your sql from the query page into mysqladim and check for errors
and copy your sql from the query page here to see

P
ppradhan@live.com author 4/20/2022

@Kohle, thank you.

select class_id, class_name, (select count(*) from students where class_id = class.class_id) as number_students did work. But I had to remove WHERE condition from the SQL that I was using to fetch records according to the logged in user from the session: meaing only ONE WHERE clause is accepted.

my old code was:
select
class_id,
class_name,
(select count(*) from students where class_id = class.class_id)
from class
WHERE (school_id = ':session.SCHID')