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

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

现在看看一些输出与大部分输出有何不同?

我怎样才能简单地分离出标题中带有“经审计的财务账目”的项目,然后重新排列显示的输出,使其成为:

  <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.ini 文件中完成。检查我的代码,看看它是否适合你。这是使用您的原始代码编写的,并对其进行修改以(希望)产生您想要的结果。

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

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

关于php - 要按 fetchAll 组中的一个变量进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36318445/

相关文章:

php - 在 PHP 登录脚本中使用 session 和 session 变量

php - 如何组合这些mysql查询

php - 引用错误 : jQuery is not defined

mysql - 如何选择每个间隔的最大时间戳和最小时间戳

php - 你如何为每个表格行递增一个数字?

php - 从 MYSQL 点转换为 Swift CLLocationCoordinate2D

php - 扩展核心 PHP 类而不是依赖注入(inject)?

Java:如何检查 Windows 上是否安装了 MySQL?

php - 使用 COUNT(*) 和 SUM() 时如何打印或回显行

mysql - 选择同一表中用户支付订单的计数