简体   繁体   English

MySQL PHP嵌套结果

[英]MySQL PHP nested results

I have 3 tables: 我有3张桌子:

profile 轮廓

id | id | fname | fname | policy 政策

users 使用者

id | id | fullname | 全名| claimnum 索赔编号

and returnprod returnprod

claim_id | Claim_id | prodname | 产品名称| description 描述

My SQL is this: 我的SQL是这样的:

("SELECT *
,CONCAT(fname, ' ' ,lname) AS fullName 
,CONCAT(firstname, ' ' ,lastname) AS InsName 
,a.address AS ADDRESS
,a.city AS CITY
    ,a.state AS STATE
    ,a.zip AS ZIP
    ,a.phone AS PHONE
    ,a.fax AS FAX
    ,a.email AS EMAIL
FROM profile a 
INNER JOIN returnprod b ON a.policy = b.claim_id 
INNER JOIN users c ON a.adjuster_id = c.id
WHERE date >= '$Start' AND date <= '$End'

Jump ahead a little bit to the loop 向前跳一点

`$products[]= $row;`  
`foreach ($products as $product)
{
  if ($row['policy'] === $product['claim_id']) {  
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount,$product['product']);     
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $product['comment']);    
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowcount, $product['anotes']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowcount, $product['price']);
}`

This is working fine if there is only one product. 如果只有一种产品,这可以正常工作。 The result will look something like this... 结果看起来像这样...

row 1: claim_id | 第1行:claim_id | firstname | 名| adjuster name 调节器名称
row 2: productname | 第2行:产品名称| description | 描述| etc 等等

If I have more than one product this happens... 如果我有多个产品,就会发生这种情况...

row 1: claim_id | 第1行:claim_id | firstname | 名| adjuster name 调节器名称
row 2: productname | 第2行:产品名称| description | 描述| etc 等等
row 3: claim_id | 第3行:claim_id | firstname | 名| adjuster name 调节器名称
row 4: productname | 第4行:产品名称| description | 描述| etc 等等
row 5: productname | 第5行:产品名称| description | 描述| etc 等等

Instead of grouping all the products together and displaying all of them below the profile information it is giving me the first result under the profile then giving me the profile information again with the first product and then the second product. 与其将所有产品组合在一起并在配置文件信息下方显示所有产品,不如将配置文件下的第一个结果提供给我,然后再将第一个产品和第二个产品提供给我配置文件信息。

How do I go about grouping all products underneath the profile the first time? 第一次如何将所有产品归类到配置文件下?

Example: 例:

row 1: claim_id | 第1行:claim_id | firstname | 名| adjuster name 调节器名称
row 2: productname | 第2行:产品名称| description | 描述| etc 等等
row 3: productname | 第3行:产品名称| description | 描述| etc 等等

Thank you for taking the time to read through this. 感谢您抽出宝贵的时间阅读此内容。

$products[] = $row seems to indicate that this is not the only loop; $products[] = $row似乎表明这不是唯一的循环; more context might help. 更多上下文可能会有所帮助。 At the least, you will want to sort your DB results by whatever you intend to group by (ORDER BY a.id, a.policy) . 至少,您将希望根据要分组的对象(ORDER BY a.id, a.policy)对数据库结果进行排序。 Then you could keep a reference to the last-seen policy and skip the header if it matches. 然后,您可以保留对上次看到的策略的引用,如果匹配则跳过标题。

$last_id = '';
foreach ($rows as $row) {
    if (! $row['policy'] === $last_id ) {
        // Write claim_id | firstname | adjuster name
    } 
    // Write productname | description | etc
    $last_id = $row['policy'];
}

Assuming that is what you are after... 假设那是你所追求的...

I went about this a different way. 我以不同的方式去做。

instead of using FOREACH I ended up using WHILE. 而不是使用FOREACH,我最终使用WHILE。

while($row = mysql_fetch_array($result)){
// CODE TO QUERY PROFILE INFORMATION

$productquery=("SELECT  * FROM returnprod WHERE claim_id = '$POLICY'");
while($product = mysql_fetch_array($prods)){

//CODE TO QUERY THE PRODUCTS ASSOCIATED WITH THE PROFILE
}
}

Then I added GROUP BY a.policy in the $result 然后我在$ result中添加了GROUP BY a.policy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM