这可能比我做的要简单,但我对 php/mySQL 还很陌生,并且遇到了一些障碍。
我有来自两个表的数据
1-雇主有一般联系信息
2-职责有雇主负责的工作
这是一对多的关系,因为每个雇主都有许多职责。当我独立显示每个雇主时,我知道如何显示职责列表,但在此应用程序中,我需要显示雇主列表,其中包含每个雇主在其公司信息与同一页面上列出的所有公司之后的职责。
我目前拥有它的方式我得到了
控制器
// Display employment
try
{
$result = $pdo->query('SELECT d.*, m.*,
date_format(start_date,"%M, %Y") AS started,
date_format(end_date,"%M, %Y") AS ended FROM employers m
LEFT JOIN duties d ON d.e_id = m.id');
}
catch (PDOException $e)
{
$error = 'Error fetching employers from the database!' . $e->getMessage();
include '/error.html.php';
exit();
}
foreach ($result as $row)
{
$employers[] = array(
'id' => $row['id'],
'name' => $row['name'],
'city' => $row['city'],
'state' => $row['state'],
'started' => $row['started'],
'ended' => $row['ended'],
'title' => $row['title'],
'duty' => $row['duty']
);
}
风景
<section id="employers">
<h2>Employers</h2>
<ul>
<?php foreach ($employers as $employer): ?>
<li>
<?php htmlout($employer['id']); ?>
<?php htmlout($employer['name']); ?>
<?php htmlout($employer['city']); ?>
<?php htmlout($employer['state']); ?>
<?php htmlout($employer['duty']); ?>
</li>
<?php endforeach; ?>
</ul>
</section>