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


To to categorise by one variable in fetchAll group

我有这段代码可以工作。DB连接是在文件的前面定义的,与此处显示无关。安全地假设它工作正常-因为它是。:-)

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";
}

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

  <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>

现在看看一些输出与大多数输出有何不同?

如何简单地将标题中带有"已审计财务账目"的项目分开,然后重新排列显示的输出,使其变为:

  <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大师。但我正在努力做到这一点,所以请耐心等待我的回答。非常感谢。

您可以在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";
}

您也可以将结果吐出一个无序的列表,结果几乎相同。

您需要的是:分组方式

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