下面我将根据之前的选择创建一个选择菜单。选择作为数组保存在我的数据库中,其中portfolio_tags
= hfw_portfolio_tags id。表格格式如下:
+---------------------+---------+----------------+
| hfw_portfolio_items | | |
+---------------------+---------+----------------+
| id | name | portfolio_tags |
| 1 | name #1 | ["1","3","4"] |
| 2 | name #2 | ["2","3","4"] |
+---------------------+---------+----------------+
+--------------------+----------+--+
| hfw_portfolio_tags | | |
+--------------------+----------+--+
| id | tag_name | |
| 1 | tag #1 | |
| 2 | tag #1 | |
| 3 | tag #1 | |
| 4 | tag #1 | |
+--------------------+----------+--+
<?php
$table_name_tags = $wpdb->prefix . "HFW_portfolio_tags";
$rows = $wpdb->get_results("SELECT * FROM"." $table_name_tags"." ORDER BY id ASC");
foreach($rows as $rowsTags) {
$portfolio_tags = stripslashes ($rowsTags->id);
$tagName = stripslashes ($rowsTags->tag_name);
$queryHere = 'SELECT * FROM '.$table_name.' WHERE id='.$id.'';
$tagRows = $wpdb->get_results( $wpdb->prepare( $queryHere, APP_POST_TYPE));
//exit(var_dump($queryHere));
foreach ($tagRows as $tagFromDB ){
$items_select = stripslashes ($tagFromDB->portfolio_tags);
$arr = json_decode($items_select,true);
$tagId = stripslashes ($tagFromDB->id);
if (is_array($arr)){
foreach ($arr as $eachSelectedDB ){
if($eachSelectedDB == $portfolio_tags){
echo '<option value="'.$portfolio_tags.'" selected>'.$tagName.'</option>';
}
elseif($portfolio_tags !== $eachSelectedDB){
}
}
}
echo '<option value="'.$portfolio_tags.'">'.$tagName.'</option>';
}
}
?>
问题是当我输出时 - 我通过foreach
运行的标签输出两次作为整体选择MYSQL并且输出也将它们计为存在。有没有办法在hfw_portfolio_tags表的最终输出中跳过这些?
答案 0 :(得分:0)
试试这个:
<?php
$table_name_tags = $wpdb->prefix . "HFW_portfolio_tags";
$rows = $wpdb->get_results("SELECT * FROM"." $table_name_tags"." ORDER BY id ASC");
foreach($rows as $rowsTags) {
$portfolio_tags = stripslashes ($rowsTags->id);
$tagName = stripslashes ($rowsTags->tag_name);
$queryHere = 'SELECT * FROM '.$table_name.' WHERE id='.$id.'';
$tagRows = $wpdb->get_results( $wpdb->prepare( $queryHere, APP_POST_TYPE));
//exit(var_dump($queryHere));
foreach ($tagRows as $tagFromDB ){
$items_select = stripslashes ($tagFromDB->portfolio_tags);
$arr = json_decode($items_select,true);
$tagId = stripslashes ($tagFromDB->id);
echo '<option value="'.$portfolio_tags.'" '.(is_array($arr) && in_array($portfolio_tags, $arr) ? 'selected' : '' ) .'>'.$tagName.'</option>';
}
}
?>
捕获正在使用:
(is_array($arr) && in_array($portfolio_tags, $arr) ? 'selected' : '' )
而不是foreach