以下是存储在$ performset中的查询结果。
$sqlperform = $ilance->db->query("
SELECT
q.title_per
FROM " . DB_PREFIX . "perform q
LEFT JOIN " . DB_PREFIX . "perform_answers a ON (q.cid = a.cid)
WHERE a.user_id = '" . $res_vendor['user_id'] . "'
");
if ($ilance->db->num_rows($sqlperform) > 0)
{
while ($rows = $ilance->db->fetch_array($sqlperform))
{
$perform .='<li>'.$rows['title_per'].'</li>';
}
$performset .='<ul>'.$perform.'</ul>';
}
echo
我从特定变量得到的值是 Adobe Flex,C ++ Builder,C#/。Net,C / C ++ / Unix,C / C ++ / Win32SDK,Oracle DBA, 数据输入,
我得到的输出是
Adobe Flex, C++ Builder, C#/.Net, C/C++/Unix, C/C++/Win32SDK, Oracle DBA,
Data Entry, Oracle DBA, Adobe Flex ,C++ Builder, C#/.Net , Data Entry
C/C++/Unix, C/C++/Win32SDK
但我需要的输出是
Data Entry C++ Builder C/C++/Win32SDK
Oracle DBA C#/.Net
Adobe Flex C/C++/Unix
适用于此的html就是这样
<ul>
<li>Data Entry</li>
<li>Oracle DBA</li>
<li>Adobe Flex</li>
</ul>
<ul>
<li>C++ Builder</li>
<li>C#/.Net</li>
<li>C/C++/Unix</li>
</ul>
我如何在php中编写脚本以产生如此多的结果
答案 0 :(得分:0)
您需要创建一个在while循环中递增的变量,如果变量可以被3整除,则需要添加</ul><ul>
。然后只需在循环前抛出<ul>
,然后再放</ul>
。大致相似的东西应该有用......(未经测试)
if ($ilance->db->num_rows($sqlperform) > 0) {
$i = 0;
$performset = '<ul>';
while ($rows = $ilance->db->fetch_array($sqlperform)) {
if ($i % 3) {
$perform .= '</ul><ul>';
}
$perform .='<li>'.$rows['title_per'].'</li>';
$i++;
}
$performset .='</ul>';
}
答案 1 :(得分:0)
$performset .='<ul>'.$perform.'</ul>';
<ul> + $performset + </ul>
答案 2 :(得分:0)
在这种情况下,使用array_chunk()是完美的。
看看这个例子,可能看起来很复杂但可能就是你需要的方式
if ($ilance->db->num_rows($sqlperform) > 0)
{
$drows = array();
while($rows = $ilance->db->fetch_array($sqlperform)) {
$drows[] = $rows;
}
$chunks = array_chunk($drows, 3, true);
foreach($chunks as $chunk) {
$set = "<ul>";
foreach($chunk as $key => $value) {
$set .= '<li>'.$value['title_per'].'</li>';
}
$set .= "</ul>";
echo $set;
}
}
$ar1 = array(1,2,3); //db row
$ar2 = array($ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1);
$drows = $ar2; //collection of db rows
$chunks = array_chunk($drows, 3, true);
foreach($chunks as $chunk) {
$set = "<ul>";
foreach($chunk as $key => $value) {
$set .= '<li>'.$key.$value[0].'</li>';
}
$set .= "</ul>";
echo $set;
}