2011-08-27 93 views
0

我从这个论坛学到了很多东西,并且在此先感谢。基本上,我试图对多个表的数据库查询的结果做“脚注”。我的表格有几种生物材料的“参考书目”,但我无法以更具可读性的方式整合结果。我想我需要使用多维数组,但我认为必须有一个更优雅的方式。在PHP代码MySQL的部分是:MySQL/PHP:整合来自多个表格的脚注结果

$queryFromAgentBW = "SELECT DISTINCT reports.ID, reports.link, agent_names.ID, agent_names.Name, agent.BW_Actor_List, agent.Common_Name, agent.Reference, actor_list.ID 

         FROM agent_names, agent 

         JOIN actor_list ON(agent.BW_Actor_List = actor_list.ID) 

         JOIN reports ON(agent.Reference = reports.ID) 

         WHERE agent_names.ID = agent.Agent_Name AND BW_Actor_List = '".mysql_real_escape_string($a)."'"; 


$resultFromAgentBW = mysql_query($queryFromAgentBW); 

     //check result; show error for debugging 
     if (!$resultFromAgentBW) 
     { 
    $message = 'Invalid query:'.mysql_error()."\n"; 
    $message .= 'Whole query:'.$queryFromAgentBW; 
    die($message); 
     } 
    while ($rowBW = mysql_fetch_assoc($resultFromAgentBW)) 
    { 

// Need to get all this in an array and then print out later so agents 
      are listed only once with all of the corresponding reference numbers 
$bwArray[] = $rowBW; 


       } 

,而PHP的 “漂亮的打印” 的代码部分:

foreach ($bwArray as $bw) 
    {  
echo "Name: {$bw['Name']}<br />" 
. "Ref: {$bw['Reference']}<br />" 
. "Link: {$bw['link']}<br /><br />"; 
    } 

结果是现在:

Name: Abrin toxin 
    Ref: 1 
    Link: C:\wamp\www\References\Abrin\AbrinandRicin_Patocka.pdf 

    Name: Abrin toxin 
    Ref: 6 
    Link: C:\wamp\www\References\Abrin\TheEmergencyResponseSafetyandHealthDatabase_   Biotoxin_ ABRIN.pdf 

    Name: Adenovirus 
    Ref: 9 
    Link: C:\wamp\www\References\Adenovirus\Adenovirus (Serotypes 40 & 41)_PHAC .pdf 


    Name: Adenovirus 
    Ref: 13 
    Link: C:\wamp\www\References\Adenovirus\AdenovirusSerotype31InfectioninaNewbornGirlandReviewoftheLiterature.pdf 

,但理想情况下是:

Abrin Toxin [1, 6] 
    Adenovirus [9, 13] 

其中数字是现在显示为文本的href链接(PDF文档参考)。感谢任何帮助或指导,在这种情况下什么是最好的!

+1

这不是一个论坛,你知道 –

+0

阿肖克学OOP,那么如何实现DAO 。然后如何将观点从逻辑中分离出来。这意味着在你的情况下:不要在同一个文件中执行两个操作:使用数据库和交织html。 – Flavius

+0

@ Col. Shrapnel-哎呀!道歉,我应该说Q&A网站! @ Flavius - 是的,改进我的面向对象和DAO的理解能够提高我的整体技能,并可能优化代码,但Andrej L(在您评论之前发布)的简单提示/指导是我所需要的,并且真正回答了问题。此外,当你没有特别选择这个项目的原因,或者我的项目背景时,你对wamp的评论显示了省级的观念或更坏的无知。 – Ashok

回答

1

你应该子句添加GROUP_CONCAT功能和组到您的查询,使各项工作在MySQL

SELECT group_concat(agent.Reference SEPARATOR ','), agent_names.ID, agent_names.Name 

        FROM agent_names, agent 

        JOIN actor_list ON(agent.BW_Actor_List = actor_list.ID) 

        JOIN reports ON(agent.Reference = reports.ID) 

        WHERE agent_names.ID = agent.Agent_Name AND BW_Actor_List = '".mysql_real_escape_string($a)."' 
GROUP BY agent_names.ID 
+0

这正是我所需要的。你是对的 - 在mysql中工作更干净更快。 – Ashok

1

你只需要在数组中正确地聚合结果。最后两个循环更改为:

while ($rowBW = mysql_fetch_assoc($resultFromAgentBW)) { 
    $bwArray[$rowBW['Name']][] = $rowBW; 
} 
foreach ($bwArray as $name => $refs) { 
    echo 'Name: ' . $name . ' ['; 
    for ($i = 0; $i < count($refs); $i++) { 
     echo ($i > 0 ? ' ' : '') . '<a href="' . $ref['link'] . '">' . $ref['Reference'] . '</a>'; 
    } 
    echo ']<br />'; 
} 

我忽略的数据与htmlspecialchars()逃逸更好的可读性。

+0

谢谢 - 这个帮助我得到了引用,并且在合并来自Andrej的更新后的SELECT语句之后,我也需要正确链接。 – Ashok

相关问题