0

这可能比我做的要简单,但我对 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>
    
  • 4

    1 回答 1

    0

    如果您想对每个员工的所有职责进行分组,您应该修改您的循环。这里有一个例子

    // Initialize employers
    $employers = array();
    
    // The current employee ID
    $employeeId = false;
    
    // The current new employee
    $newEmployee = false;
    
    // Loop on each line of employee-duty
    foreach ($result as $row)
    {
        // Check if we have to add a new employee
        if ($employeeId == false || $row['id'] != $employeeId)
        {
            // Store previous employee if exists
            if ($newEmployee != false)
                $employers[] = $newEmployee;
    
            // Create new employee
            $newEmployee = array(
                'id' => $row['id'],
                'name' => $row['name'],
                'city' => $row['city'],
                'state' => $row['state'],
                'started' => $row['started'],
                'ended' => $row['ended'],
                'title' => $row['title'],
                'duty' => array()
            );
    
            // Set the new employee ID
            $employeeId = $row['id'];
        }
    
        // In all case, add the duty
        $newEmployee['duty'][] = $row['duty'];
    }
    
    // Save the last employee that will not be saved in the loop
    if ($newEmployee != false)
        $employers[] = $newEmployee;
    

    duty在此代码的末尾,您将在条目中拥有所有具有所有职责的员工(唯一) 。

    重要提示:另一个解决方案是修改您的 SQL 查询,并使用group by聚合函数在 SQL 服务器端执行这些操作。这将简化 PHP 端的代码。

    祝你好运。

    于 2013-02-26T00:08:10.280 回答