php - 而 mysqli_fetch_assoc 在有列而不是行时工作

标签 php mysql

抱歉,我不知道如何理解标题中的这一点。

基本上我有一个包含两行的表格。

enter image description here

现在我想在我的页面上显示引用,然后显示所有文档列。

但是你可以看到我在两行上有相同的引用。如果用户上传具有相同引用的文档,我希望能够在引用下显示所有文档。

例如我想在页面上看起来像这样:

11111111
word.jpg
a17.gif
Matthew Smart CV.docx
word.jpg
a17.png
Matthew Smart CV.docx

这是我到目前为止所做的并且卡住了

PHP/MySQL:

    <?php

    $query  = "SELECT * ";
    $query .= "FROM customers ";
    $query .= "WHERE reference = 11111111";
    $results = mysqli_query($connection, $query);

    $cnt = 0;
    $customer_doc = array();
    while($customer_details = mysqli_fetch_assoc($results)) {
        $customer_ref = $customer_details["reference"];
        $cnt ++;
        $customer_doc[] = $customer_details['doc' . $cnt];
    }
    echo $customer_ref;


    ?>
    <pre>
        <?php
        print_r($customer_doc);
        ?>
    </pre>

我不知道该怎么做。

谁能好心帮助我,让我从中学习?

最佳答案

假设您最终会在结果集中查询多个 reference 值,CBroe 在主评论线程中提到的技术涉及存储 reference 的最后一个值> 在 while 循环的每次迭代中。如果该值与前一个值发生变化,则打印输出。如果相同,则无需在该循环中打印它,而只需打印其他列即可。

选项 1 将变量与上次迭代的值进行比较:

注意:我在这里使用非常原始的输出来执行此操作,只是带有换行符的裸露 echo

// Start with an empty value for the "last one"
$last = null;
while ($customer_details = mysqli_fetch_assoc($results)) {
  // Check if the newly fetched row differs from the previous value
  if ($customer_details['reference'] != $last) {
    // It is different! Print it out
    echo $customer_details['reference']  . "\n";
  }
  // Store this current one into the $last var for 
  // comparison on the next round
  $last = $customer_details['reference'];

  // Print the other columns...
  echo $customer_details['doc1'] . "\n";
  echo $customer_details['doc2'] . "\n";
  echo $customer_details['doc3'] . "\n";
}

选项 2 - 索引数组存储(可能是最好的选项,当然是我会使用的选项)

现在,假设您想将这些存储到一个数组中。这会很方便,因为您可以创建一个由 reference 列索引的数组,以及用于其他列的子数组。这更容易,您不需要检查值是否更改。您只需在每次 while 迭代时追加一个子数组。

// Array to hold all results:
$all = array();
while ($customer_details = mysqli_fetch_assoc($results)) {
  // Append a new sub-array of the 3 other cols to
  // the main array's key by reference using the []
  // array append syntax
  $all[$customer_details['reference']][] = array(
    'doc1' => $customer_details['doc1'],
    'doc2' => $customer_details['doc2'],
    'doc3' => $customer_details['doc3']
  );
}

数组现在看起来像

Array (
  '1111111' => Array (
     Array (
       'doc1' => 'word.jpg'
       'doc2' => 'a17.gif',
       'doc3' => 'Matthew Smart CV.docx'
     ),
     Array (
       'doc1' => 'word.jpg'
       'doc2' => 'a17.gif',
       'doc3' => 'Matthew Smart CV.docx'
     )
  ),
  '222222' => Array (
     Array (
       'doc1' => 'xyz.jpg'
       'doc2' => 'z17.gif',
       'doc3' => 'Matthew Smart CV.pdf'
     ),
     Array (
       'doc1' => 'xyz.jpg'
       'doc2' => 'z17.gif',
       'doc3' => 'Matthew Smart CV.pdf'
     ),
)

因此您可以使用嵌套的 foreach 对其进行循环。外部获取 reference,内部获取其他值:

foreach ($all as $reference => $rows) {
  // Write out the reference, which was the array key
  echo $reference . "\n";
  // Then in a loop, write out the others
  foreach ($rows as $row) {
    echo $row['doc1'] . "\n";
    echo $row['doc2'] . "\n";
    echo $row['doc3'] . "\n";
  }
}

选项 3 - 查询技巧:

最后一个是您可以在查询中使用的 GROUP BY hack。我不完全推荐它,但想证明它是可能的。如果你使用 GROUP_CONCAT()与普通的 CONCAT_WS() 一起,您可以在 一个 行中生成 reference,然后是所有其他文档,用类似 ||。那么在 PHP 中,您只需要一个循环并在分隔符 || 上执行 explode()

$query = "SELECT reference, GROUP_CONCAT(CONCAT_WS('||', doc1, doc2, doc3) SEPARATOR '||') AS docs FROM customers GROUP BY reference";

这将产生字面上结构如下的行:

1111111, word.jpg||a17.gif||Matthew Smart CV.docx||word.jpg||a17.gif||Matthew Smart CV.docx

也就是说,列中的 reference,然后是 || 连接的所有其他字符串作为名为 docs 的列。

// Execute the query, then fetch.
while ($customer_details = mysqli_fetch_assoc($results)) {
  echo $customer_details['reference'] . "\n";

  // Then explode() the || delimited string with linebreaks
  echo implode("\n", explode("||", $customer_details['docs']));
}

同样,我不建议实际使用它,但可以通过这种方式完成。

关于php - 而 mysqli_fetch_assoc 在有列而不是行时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28005960/

相关文章:

php - 使用 php 和 mysql 处理银行对账

php - CakePHP:向日期字段添加天数并在查找查询中比较它们

mysql - 改变当前的 MySql 查询以根据 id 组合结果

java - Java 中的时间戳和 mySql 中的时间戳

php - 找不到 Laravel 5 路由对象

php - 我可以根据 IP 禁止或限制某个国家/地区吗?

javascript - 动态创建和处理多个表单字段组

MYSQL 基于多列的Rank

mysql - MySQL 中具有并集的相同 CASE 条件的多个 THEN

PHP - 确定数据是否已删除