Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:11:07+00:00 2026-06-14T21:11:07+00:00

How can we display another list record inside in listed record using CakePHP? I

  • 0

How can we display another list record inside in listed record using CakePHP?

I have 3 table that consist: (staff,course,attendance)

(Staff)

staff_id    |   name    |   passport
------------------------------------------------
1       |   A   |   123
2       |   B   |   132
3       |   C   |   321

(Course)

c_id    |   name
----------------------
10  |   PHP
20  |   AJAX
30  |   XHTML

(Attendance)

at_id   |   staff_id    |   course_id
------------------------------------------------
1   |   1       |   10
2   |   1       |   20
3   |   1       |   30
4   |   2       |   20
5   |   2       |   30

I would like to generate some report to display listed of course that attend by our staff as you can see below:

Name    |   Course Attend
------------------------------
A   |   PHP
    |   AJAX
    |   XHTML
B   |   AJAX
    |   XHTML

Here I use table staff in displaying result and what should I do to get some another fetch result in my column Course Attend that related to attendance table which will be listed all the related result to their staff ids.

My Code:

(ReportController.php)

$view = $this->Staff->find('all');

$this->set('view', $view);

(View – report.ctp)

<table>
<?php foreach($view as $data): ?>
<td>
    <?php echo $data['Staff']['name']; ?>
</td>
<td>
    <?php // something to do here... ?>
</td>
<?php endforeach ?>
</table>

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:11:08+00:00Added an answer on June 14, 2026 at 9:11 pm

    It’s not very clear from your question, but it seems to me like you want to display a view that contains information from all the 3 tables.

    In this case, you will need to create Model associations.

    In the Attendance model:

    class Attendance extends AppModel {
        public $hasOne = array(
            'Staff' => array(
                'className'  => 'Staff',
                'foreignKey' => false,
                'conditions' => array('Staff.staff_id = Attendance.staff_id')
            ),
            'Course' => array(
                'className'  => 'Course',
                'foreignKey' => false,
                'conditions' => array('Course.c_id = Attendance.course_id')
            )
        );
    }
    

    In the controller, you can add a find(‘all’) query that will retrieve all the rows from Attendance, together with their corresponding Staff and Course rows.

    $attendance_rows = $this->Attendance->find('all', array('order' => 'Staff.name ASC, Staff.staff_id ASC'));
    $this->set("attendance_rows", $attendance_rows);
    

    In the view, you can use a foreach loop as usual:

    <table>
    <?php foreach($attendance_rows as $data): ?>
    <td>
        <?php echo $data['Staff']['name']; ?>
    </td>
    <td>
        <?php echo $data['Course']['name']; ?>
    </td>
    <?php endforeach ?>
    </table>
    

    To display the names of the Staff only once, you can add a variable in the view and reset it each time a new Staff row is found. This works if the find(‘all’) query is ordered.

    <table>
    <?php $temp = null; $no = 1;?>
    <?php foreach($attendance_rows as $data): ?>
    <td>
        <?php if ($temp != $data['Staff']['staff_id']) { echo $no; $no++;} ?>
    </td>
    <td>
        <?php if ($temp != $data['Staff']['staff_id']) { echo $data['Staff']['name']; $temp = $data['Staff']['staff_id']}; ?>
    </td>
    <td>
        <?php echo $data['Course']['name']; ?>
    </td>
    <?php endforeach ?>
    </table>
    

    One more suggestion: you should always name your primary keys as ‘id’, not ‘staff_id’ or ‘c_id’, since Cakephp knows how to bind models based on their names. If you create the models ‘the Cakephp way’, you won’t need to specify the ‘foreignKey’ or ‘conditions’ fields when creating associations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple application, that shows list of many items, where user can display
I have a list of items that I need to record its position in
I have a list view that is displaying data using the gridview. This list
I have a foreach loop in a view that display a list of files
Suppose I have ten list item, in that case I can use both div
Possible Duplicate: Is there a tool that can display a SVN repository visually (
Examotion has a player that can display the image for svg files as a
I have this code which can display drop down in div tag of html.
I have a list of dates in a table in a MySQL database (the
I am attempting to create a report that contains a list nested within another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.