要在fetchAll组中按一个变量进行分类

时间:2016-03-30 19:53:01

标签: php mysql

我有这个代码可行。数据库连接在文件中先前定义,与此处显示无关。安全地假设它正常工作 - 因为它是。 : - )

try {
    $stmt = $conn->prepare("SELECT * FROM table WHERE `type` = 'a' ORDER BY `title` DESC");
    $stmt->execute();
    $resultA = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }

if(count($resultA) == 0) {} else {
    // We have result(s) to show
    echo "  <h2>Section title in here</h2>\n\n";
    echo "  <p>";
    foreach ($resultA as $value) {
        echo "\n  &bull; <a href=\"".$value['uri']."\" target=\"_blank\">".$value['title']."</a><br>";
    }
    echo "</p>\n\n";
}

这给了我一个输出 - 正如预期的那样,因为数据库中实际上有数据要显示 - 就像这样:

  <h2>Section title in here</h2>

  <p>
  &bull; <a href="./lib/rep/REP_1459308496_2896806.pdf" target="_blank">2015 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308526_2307279.pdf" target="_blank">2014 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308872_1655063.pdf" target="_blank">2013 Audited Financial Accounts</a><br>
  &bull; <a href="./lib/rep/REP_1459308568_3163307.pdf" target="_blank">2013 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308849_1956569.pdf" target="_blank">2012 Audited Financial Accounts</a><br>
  &bull; <a href="./lib/rep/REP_1459308595_3408196.pdf" target="_blank">2012 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308834_1072958.pdf" target="_blank">2011 Audited Financial Accounts</a><br>
  &bull; <a href="./lib/rep/REP_1459308615_1515719.pdf" target="_blank">2011 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308819_1044010.pdf" target="_blank">2010 Audited Financial Accounts</a><br>
  &bull; <a href="./lib/rep/REP_1459308638_6083742.pdf" target="_blank">2010 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308737_1838789.pdf" target="_blank">2009 Audited Financial Accounts</a><br>
  &bull; <a href="./lib/rep/REP_1459308671_1675927.pdf" target="_blank">2009 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308713_2363893.pdf" target="_blank">2008 Audited Financial Accounts</a><br>
  &bull; <a href="./lib/rep/REP_1459308477_6178876.pdf" target="_blank">2008 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308421_966670.pdf" target="_blank">2007 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308393_535085.pdf" target="_blank">2006 Annual Report</a><br></p>

现在看看输出的某些与大多数输出​​有什么不同?

我如何简单地将这些项目与&#34;经审计的财务账户&#34;在标题内,然后重新排列显示的输出,使其成为:

  <h2>Section title in here</h2>

  <div style="width: 50%; float: right;">

   <p>
   &bull; <a href="./lib/rep/REP_1459308872_1655063.pdf" target="_blank">2013 Audited Financial Accounts</a><br>
   &bull; <a href="./lib/rep/REP_1459308849_1956569.pdf" target="_blank">2012 Audited Financial Accounts</a><br>
   &bull; <a href="./lib/rep/REP_1459308834_1072958.pdf" target="_blank">2011 Audited Financial Accounts</a><br>
   &bull; <a href="./lib/rep/REP_1459308819_1044010.pdf" target="_blank">2010 Audited Financial Accounts</a><br>
   &bull; <a href="./lib/rep/REP_1459308737_1838789.pdf" target="_blank">2009 Audited Financial Accounts</a><br>
   &bull; <a href="./lib/rep/REP_1459308713_2363893.pdf" target="_blank">2008 Audited Financial Accounts</a><br></p>

 </div>

  <p>
  &bull; <a href="./lib/rep/REP_1459308496_2896806.pdf" target="_blank">2015 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308526_2307279.pdf" target="_blank">2014 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308568_3163307.pdf" target="_blank">2013 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308595_3408196.pdf" target="_blank">2012 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308615_1515719.pdf" target="_blank">2011 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308638_6083742.pdf" target="_blank">2010 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308671_1675927.pdf" target="_blank">2009 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308477_6178876.pdf" target="_blank">2008 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308421_966670.pdf" target="_blank">2007 Annual Report</a><br>
  &bull; <a href="./lib/rep/REP_1459308393_535085.pdf" target="_blank">2006 Annual Report</a><br></p>

请记住,我不是一个php大师。但是我想到达那里,所以请耐心等待我的回答。谢谢!

2 个答案:

答案 0 :(得分:0)

你可以在php中完成。检查我的代码,看看它是否适合您。这是使用您的原始代码编写的,并将其修改为(希望)产生您想要的结果。

try {
    $stmt = $conn->prepare("SELECT * FROM table WHERE `type` = 'a' ORDER BY `title` DESC");
    $stmt->execute();
    $resultA = $stmt->fetchAll(PDO::FETCH_ASSOC);       
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }

if(count($resultA) == 0) {} else {
    // We have material to show. First set up the arrays to divide output into
    $posRight = array(); // array of output to float right
    $posNormal = array(); // array of output not floating right
    echo "  <h2>Section title in here</h2>\n\n";

    foreach($resultA as $value) {
        if (strpos($value['title'], 'Audited Financial Accounts') !== FALSE) {
            // We have the "Audited Financial Accounts" items returning here. Add these results into the $posRight array
            $posRight[] = $value;
        } else {
            // The rest of the result content arrives here. Add it to the $posNormal array
            $posNormal[] = $value;
        }
    }

    // We have our two arrays, output the float right results first
    echo "  <div style=\"width: 50%; float: right;\">";
    foreach($posRight as $value) {
        echo "\n   &bull; <a href=\"".$value['uri']."\" target=\"_blank\">".$value['title']."</a><br>";
    }
    echo "\n  </div>\n\n";

    // Now output the other set
    echo "  <p>";
    foreach($posNormal as $value) {
        echo "\n   &bull; <a href=\"".$value['uri']."\" target=\"_blank\">".$value['title']."</a><br>";
    }
    echo "</p>\n\n";
}

您还可以将结果吐出一个无序列表,结果大致相同。

答案 1 :(得分:-1)

您需要的是: GROUP BY

$stmt = $conn->prepare("SELECT * FROM table WHERE `type` = 'a' GROUP BY `title`");