Php 而mysqli_fetch_assoc在有列而不是行的情况下工作
对不起,我不知道如何理解标题中的这个 基本上我有一张两排的桌子 现在我想显示引用,然后显示页面上的所有文档列 但是你可以看到我在两行上有相同的引用。如果用户上传具有相同引用的文档,我希望能够在引用下显示所有文档 例如,我希望在第页上看起来像这样:Php 而mysqli_fetch_assoc在有列而不是行的情况下工作,php,mysql,Php,Mysql,对不起,我不知道如何理解标题中的这个 基本上我有一张两排的桌子 现在我想显示引用,然后显示页面上的所有文档列 但是你可以看到我在两行上有相同的引用。如果用户上传具有相同引用的文档,我希望能够在引用下显示所有文档 例如,我希望在第页上看起来像这样: 11111111 word.jpg a17.gif Matthew Smart CV.docx word.jpg a17.png Matthew Smart CV.docx 这就是我到目前为止所做的,我被卡住了 PHP/MYSQL: <
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>
我不知道怎么做
有人能帮我吗?这样我就可以从中学习了。您需要数组中的“按引用分组”:
// 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";
}
或者“经典方式”:在while循环之前为当前引用添加temp变量,并且每次迭代都将之前的引用与当前引用进行比较 您需要数组中的“按引用分组”:
// 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";
}
或者“经典方式”:在while循环之前为当前引用添加temp变量,并且每次迭代都将之前的引用与当前引用进行比较 您需要数组中的“按引用分组”:
// 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";
}
或者“经典方式”:在while循环之前为当前引用添加temp变量,并且每次迭代都将之前的引用与当前引用进行比较 您需要数组中的“按引用分组”:
// 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";
}
或者“经典方式”:在while循环之前为当前引用添加temp变量,并且每次迭代都将之前的引用与当前引用进行比较 假设您最终将在结果集中查询多个
reference
值,CBroe在主注释线程中提到的技术涉及在while
循环的每次迭代中存储reference
的最后一个值。如果该值与上一个值不同,则打印输出。如果相同,则不需要在该循环中打印,而只需打印其他列
选项1将变量与上一次迭代的值进行比较:
注意:我在这里使用的是非常原始的输出,只不过是带换行符的裸echo
// 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']
);
}
选项2-索引阵列存储(可能是最好的选项,当然是我会使用的选项)
现在,假设要将这些存储到数组中。这将非常方便,因为您可以通过reference
列创建一个索引数组,并为其他列创建子数组。这更容易,并且不需要检查值是否更改。您只需在每次迭代时添加一个子数组
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 ($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";
}
}
因此,您可以使用嵌套的foreach
循环它。外部获取参考
,内部获取其他值:
$query = "SELECT reference, GROUP_CONCAT(CONCAT_WS('||', doc1, doc2, doc3) SEPARATOR '||') AS docs FROM customers GROUP BY reference";
选项3-查询黑客:
最后一个是可以在查询中使用的groupby
hack。我并不完全推荐它,但我想证明它是可能的。如果与普通的CONCAT_WS()
一起使用,则可以在一行中生成引用
,后面是所有其他文档,它们之间用类似|
的分隔符隔开。在PHP中,您只需要一个循环,并在分隔符|
上执行explode()
1111111, word.jpg||a17.gif||Matthew Smart CV.docx||word.jpg||a17.gif||Matthew Smart CV.docx
这将产生按字面结构排列的行,如:
// 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']));
}
也就是说,列中的引用
,然后所有其他字符串由|
连接成一个名为文档
的列
同样,我不建议实际使用它,但可以这样做。假设您最终将在结果集中查询多个参考值,在主注释线程中提到的CBroe技术涉及在while
循环的每次迭代中存储引用的最后一个值。如果该值与上一个值不同,则打印输出。如果相同,则不需要在该循环中打印,而只需打印其他列
选项1将变量与上一次迭代的值进行比较:
注意:我在这里使用的是非常原始的输出,只不过是带换行符的裸echo
// 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']
);
}
选项2-索引阵列存储(可能是最好的选项,当然是我会使用的选项)
现在,假设要将这些存储到数组中。这将非常方便,因为您可以通过reference
列创建一个索引数组,并为其他列创建子数组。这更容易,并且不需要检查值是否更改。您只需在每次迭代时添加一个子数组
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 ($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";
}
}
因此,您可以使用嵌套的foreach
循环它。外部获取参考
,内部获取其他值:
$query = "SELECT reference, GROUP_CONCAT(CONCAT_WS('||', doc1, doc2, doc3) SEPARATOR '||') AS docs FROM customers GROUP BY reference";
选项3-查询黑客:
最后一个是可以在查询中使用的groupby
hack。我并不完全推荐它,但我想证明它是可能的。如果与普通的CONCAT_WS()
一起使用,则可以在一行中生成引用
,后面是所有其他文档,它们之间用类似|
的分隔符隔开。在PHP中,您只需要一个循环,并在分隔符|
上执行explode()
1111111, word.jpg||a17.gif||Matthew Smart CV.docx||word.jpg||a17.gif||Matthew Smart CV.docx
这将产生按字面结构排列的行,如:
// 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']));
}
也就是说,列中的引用
,然后所有其他字符串由|
连接成一个名为文档
的列
同样,我不建议实际使用它,但可以这样做。假设您最终将在结果集中查询多个参考值,在主注释线程中提到的CBroe技术涉及在while
循环的每次迭代中存储引用的最后一个值。如果该值与上一个值不同,则打印输出。如果相同,则不需要在该循环中打印,而只需打印其他列
选项1将变量与上一次迭代的值进行比较:
注意:我在这里使用的是非常原始的输出,只是bar