按副标题对PHP排序
我正在尝试按SQL表中的子标题对该表进行排序。我试图得到的输出是:按副标题对PHP排序,php,sql,sorting,loops,foreach,Php,Sql,Sorting,Loops,Foreach,我正在尝试按SQL表中的子标题对该表进行排序。我试图得到的输出是: ItemHeader1 -item1 -item2 -item3 ItemHeader2 -item4 -item5 ect. 但我得到的是: ItemHeader1 -item1 -item1 -item1 ect. 以下是我的PHP代码: $index_content_enquirers = mysql_query("SELECT * FROM index_content_enquirers WHERE lang_att
ItemHeader1
-item1
-item2
-item3
ItemHeader2
-item4
-item5
ect.
但我得到的是:
ItemHeader1
-item1
-item1
-item1
ect.
以下是我的PHP代码:
$index_content_enquirers = mysql_query("SELECT * FROM index_content_enquirers WHERE lang_attribute = '$lang_identify' ORDER BY item_sectionname_english ASC, item_english_title ASC") or die(mysql_error());
$get_index_content_enquirers = mysql_fetch_assoc($index_content_enquirers);
$contentinfo = array();
foreach($get_index_content_enquirers as $row) {
$contentinfo[$get_index_content_enquirers['item_sectionname_english']][] = $get_index_content_enquirers['item_english_title'];
}
foreach($contentinfo as $contenttitle => $item_titles) {
echo $contenttitle;
foreach($item_titles as $item_title) {
echo "<br />". $item_title;
}
}
$index\u content\u inquiriers=mysql\u query(“从index\u content\u inquiriers中选择*,其中lang\u属性=“$lang\u identification”按项目的顺序\u sectionname\u english ASC,item\u english\u title ASC”)或die(mysql\u error());
$get\u index\u content\u inquiriers=mysql\u fetch\u assoc($index\u content\u inquiriers);
$contentinfo=array();
foreach($get\u index\u content\u查询者作为$row){
$contentinfo[$get_index_content_Inquiriers['item_sectionname_english']][=$get_index_content_Inquiriers['item_english_title'];
}
foreach($contentinfo作为$contenttitle=>$item\u titles){
echo$contenttitle;
foreach($item\u title作为$item\u title){
回显“
”。$item_title;
}
}
提前谢谢 首先-不要使用mysql函数,因为它们已被弃用。使用mysqli或PDO 在此循环中:
foreach($get_index_content_enquirers as $row) {
$contentinfo[$get_index_content_enquirers['item_sectionname_english']][] =
$get_index_content_enquirers['item_english_title'];
}
您应该使用$row
而不是$get\u index\u content\u queryers
。这样就可以了:)
请阅读有关查询中转义字符串的内容。您很可能正在为自己设置SQL注入攻击。当我使用$row时,它会给我一个“警告:非法字符串偏移”错误?
foreach($get_index_content_enquirers as $row) {
$contentinfo[$row['item_sectionname_english']][] =
$row['item_english_title'];
}