我的ajax文件,其中获取复选框中的term_id
$(document).ready(function() {
// Checkboxes click function
$('input[type="checkbox"]').on('click',function(){
var arr=[];
jQuery("input:checked").each(function(){
arr.push($(this).val());
});
// alert(arr);
jQuery.get("<?= bloginfo('template_directory'); ?>/template_classifiedstheme/productBySubCategory.php", { termId: $(this).val()},function(data){
// alert(data);
jQuery("#result").show();
jQuery("#result").html(data);
});
// Here we check which boxes in the same group have been selected
// Get the current group from the class
var group = $(this).attr("class");
var checked = [];
// Loop through the checked checkboxes in the same group
// and add their values to an array
$('input[type="checkbox"].' + group + ':checked').each(function(){
checked.push($(this).val());
});
//refreshData(checked, group);
});
// function refreshData($values, $group){
// alert("You've requested: " + $values + " from the group " + $group);
// }
});
我的php代码,用于在post值中获取term_id
foreach ($childCatID as $kid)
{
$childCatName = $wpdb->get_row("SELECT name,slug, term_id FROM $wpdb->terms WHERE term_id=$kid");
echo '<li><label class="lab_brand">
<span id="sp_brand">
<input type="checkbox" rel="brand" id="input_chk" value="'.$childCatName->term_id.'" onclick="getValue(this.id)" />
</span>
<a href="'.get_term_link($childCatName->slug, 'category').'">'.$childCatName->name.'</a>
</label>
</li>';
}
echo'</ul>
</div>';
通过此查询获取期限ID的MY ajax文件
$subcategory_id=$_GET['termId'];
$querystr = "SELECT *
FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_posts p ON p.ID = wpr.object_id
WHERE taxonomy = 'category'
AND p.post_type = 'post'
AND p.post_status = 'publish'
AND wp_terms.term_id = $subcategory_id
但我们只能找到一个term_id
当我选中多个复选框时 只有一个值进入termId
答案 0 :(得分:0)
我将在这里做一个长镜头并假设您正在询问如何添加多个$ subcategory_id。
在这种情况下,我会将表单元素设置为name ='termId []',这样您就可以拥有一组值。
然后将查询更改为以(我假设您用引号括起来:
'和wp_terms.term_id IN('。implode(“,”,$ subcategory_id)。')';
否则,您只需在查询中添加多个AND子句即可。即
AND wp_terms.term_id ='$ variable'
答案 1 :(得分:0)
使用MySql函数implode记录将以逗号分隔格式存储到DB中。需要将此DB字段类型保留在varchar中。