Showing records of 2 different categories

2 posts by 2 authors in: Forums > CMS Builder
Last Post: February 20, 2014   (RSS)

By Daryl - February 20, 2014

Hi Koen,

If I understand it correctly, you want to show one project and all the buildings (and their records) under that project in projectDetail.php, is that right?

What I'd usually do in this kind of cases is to, first, load the project records. Example:

  // load record from 'projects'
  list($projectsRecords, $projectsMetaData) = getRecords(array(
    'tableName'   => 'projects',
    'where'       => "`num` = '".mysql_escape(@$_REQUEST['projectRecordNum'])."'", // @$_REQUEST['projectRecordNum'] is the project record num that you want to load
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $projectsRecord = @$projectsRecords[0]; // get first record
  if (!$projectsRecord) { dieWith404("Record not found!"); } // show error message if no record found

And then load all the buildings that are linked to that project. In your case is via dropdown list field, for example, let's call that field "parent_project":

  // load records from 'buildings'
  list($buildingsRecords, $buildingsMetaData) = getRecords(array(
    'tableName'   => 'buildings',
    'loadUploads' => true,
    'allowSearch' => false,
    'where'       => 'parent_project = "'.mysql_escape($projectsRecord['num']).'"',
  ));

Now that I have all the data, I can now display them in the page. Since the $buildingsRecords might have multiple records, I will display them by looping through each. Example:

// display the project records
echo htmlspecialchars($projectsRecord['name']);
echo htmlspecialchars($projectsRecord['date']);
//.. and so on

// display all the buildings under this project
if ($buildingsRecords){
  foreach ($buildingsRecords as $building){
    echo htmlspecialchars($building['name']);
    echo htmlspecialchars($building['number']);
    //.. and so on
  }
}

Hope this helps!

Daryl Maximo
PHP Programmer - interactivetools.com