MySQL和PHP——每一种?
我有以下代码:MySQL和PHP——每一种?,php,mysql,loops,foreach,Php,Mysql,Loops,Foreach,我有以下代码: SELECT q21, q21coding AS Description FROM `tresults_acme` WHERE q21 IS NOT NULL AND q21 <> '' ORDER BY q21coding 现在,我想在PHP页面的一个表中显示它,但由于我希望它的显示方式,我遇到了一些问题,它需要如下所示: Content needs updating ---------------------- [List all the comments r
SELECT q21, q21coding AS Description FROM `tresults_acme` WHERE q21 IS NOT NULL AND q21 <> '' ORDER BY q21coding
现在,我想在PHP页面的一个表中显示它,但由于我希望它的显示方式,我遇到了一些问题,它需要如下所示:
Content needs updating
----------------------
[List all the comments relating to this description]
Difficulty in navigating/finding content
----------------------------------------
[List all the comments relating to this description]
等等
现在我认为这是PHP中的一个For-Each循环,但我很难理解它——任何想法和建议都非常受欢迎
谢谢,简单的方法
prev_desc
设置为NULL
文本
description
不等于prev_desc
为新“部分”添加说明并设置prev_desc$文本){
打印“.$desc.”;
foreach($text作为$text){
打印$text.“
”;
}
}
1正如@bobince所指出的,确保直接进入最终HTML的内容被正确转义,例如,请参见。您只需要跟踪上次显示的标题。我不知道您正在使用哪个库访问数据库,因此关于如何访问列/行的详细信息将略有不同,但这里是一种伪代码:
$lastHeading = '';
foreach($rows as $row)
{
if ($lastHeading != $row['Description'])
{
if ($lastHeading != '')
echo '</ul>';
$lastHeading = $row['Description'];
echo "<h1>$lastHeading</h1>";
echo '<ul>';
}
echo '<li>'.$row['Text'].'</li>';
}
if ($lastHeading != '')
echo '</ul>';
$lastHeading='';
foreach($行作为$行)
{
如果($lastHeading!=$row['Description']))
{
如果($lastHeading!='')
回声“”;
$lastHeading=$row['Description'];
回显“$lastHeading”;
回声“”;
}
回显“- ”.$row['Text']”。
;
}
如果($lastHeading!='')
回声“
”;
这增加了在
中添加注释的功能,不确定您是否需要这样做
这是因为您已按“描述”列排序。这意味着您知道具有相同“描述”的所有行都将合并在一起。您可以为每个部分创建多个查询,或者多次循环数据,并使用php根据描述类型进行过滤
$descriptions = array('Content needs updating','Difficulty in navigating/finding content');
$rows = <fetch all rows from the query>;
foreach($descriptions as $description)
{
echo '<h1>',$description,'</h1>';
foreach($rows as $row)
{
if ($row['description'] == $description)
{
echo $row['text'],'<br />';
}
}
}
$descriptions=array('内容需要更新','导航/查找内容有困难');
$rows=;
foreach($description作为$description)
{
回显“”,$description“”;
foreach($行作为$行)
{
if($row['description']=$description)
{
echo$row['text'],“
”;
}
}
}
查看jqgrid以显示此表+搜索和编辑开始查看:在插入HTML时,请记住在描述(和文本,如果不应该是HTML标记)上使用htmlspecialchars()
,以避免HTML注入问题。
$items = array(
'Content needs updating' => array(
'Lack of ...',
'The intra...'
),
...
);
foreach ($items as $desc => $texts) {
print '<h1>' . $desc . '</h1>';
foreach ($texts as $text) {
print $text . '<br />';
}
}
$lastHeading = '';
foreach($rows as $row)
{
if ($lastHeading != $row['Description'])
{
if ($lastHeading != '')
echo '</ul>';
$lastHeading = $row['Description'];
echo "<h1>$lastHeading</h1>";
echo '<ul>';
}
echo '<li>'.$row['Text'].'</li>';
}
if ($lastHeading != '')
echo '</ul>';
$descriptions = array('Content needs updating','Difficulty in navigating/finding content');
$rows = <fetch all rows from the query>;
foreach($descriptions as $description)
{
echo '<h1>',$description,'</h1>';
foreach($rows as $row)
{
if ($row['description'] == $description)
{
echo $row['text'],'<br />';
}
}
}