gpt4 book ai didi

php - 从数组中抓取然后忽略 MYSQL

转载 作者:行者123 更新时间:2023-11-29 21:50:48 25 4
gpt4 key购买 nike

下面我有这个,它根据以前的选择创建一个选择菜单。这些选择以数组形式保存在我的数据库中,其中 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 运行的标签被输出两次,作为整个 select MYSQL 的输出,并且输出也将它们计为存在。有没有办法在 hfw_portfolio_tags 表的最终输出中跳过这些?

最佳答案

试试这个:

<?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) ? '选定' : '' )而不是 foreach

关于php - 从数组中抓取然后忽略 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33661086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com