在检查两个复选框时没有进入正确的循环

时间:2014-04-21 11:28:01

标签: javascript php html checkbox

我有一个代码,其中我有两个类别的复选框,一个品牌和其他折扣百分比。基于这些复选框,div正在过滤。div的过滤发生好,但有一个小问题。 下面是firstphp页面

<html>
<head>
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style type="text/css">
#image{
width:250px;
height:250px;
border:1px solid black;
}
</style>
</head>
<body>

<script type="text/javascript">
    function get_check_value() {
        var c_value = [];

        $('input[name="brand"]:checked').each(function () {
            c_value.push(this.value);
        });
        return c_value.join(',');

    }
    function get_disc_value(){
    var d_value=[];
        $('input[name="discount"]:checked').each(function () {
            d_value.push(this.value);
        });
        return d_value.join(',');
        }

    $(document).ready(function(){
   checkboxValidate = function (e) {
   if(e)e.preventDefault();
    alert("hi");
        //var os = $('#originState').val();
       //var c = $('#commodity').val();
        //var ds = $('#destState').val();
        var ser = get_check_value();
        var disc=get_disc_value();
        //var queryString = "os=" + os;
        var data = "?ser=" + ser;
        var queryString = "&ser=" + ser;
       // alert(ser);
       $.ajax({
       //alert("ajax");
        type: "POST",
        url: "sortingajax.php",
        data: {ser:ser,disc:disc},
        dataType :  'html',
        success: function (b) {
           // alert(a+' ok. '+b)
            $('#results').html(b);
            console.log(b);
        }
    });

    } 
    $( "[type=checkbox]" ).change(checkboxValidate);

    checkboxValidate();
});
</script>


brand
    <input type="checkbox" name="brand" value="Sunbaby" id="check" />Sunbaby
    <br/>
    <input type="checkbox" name="brand" value="Advance Baby" id="check"/>Advance Baby
    <br/>
    store
    <br/>
    <input type="checkbox" name="discount" value="10" />10
    <br/>
    <input type="checkbox" name="discount" value="20" />20
    <br/>
    <input type="checkbox" name="discount" value="30" />30
    <br/>


<button id="btnSubmit">sort</button>
<div id="image">
<img src="http://img5a.flixcart.com/image/sunglass/4/u/y/mb-d4-09b-miami-blues-free-size-275x275-imadzkhuchryqjgp.jpeg" width="250px" height="250px"/>
</div>
<div id="results">
sdfsdfsdfsdfdsfgsdgsbsfgvf
</div>
</body>
</html>

Asortingajax.php-我在哪里检查数据库

<?php

include('connection.php');
$query=$_POST['ser'];

$query2=$_POST['disc'];


$query=explode(",",$query);
$query = array_filter($query);
$query2=explode(",",$query2);
$query2 = array_filter($query2);
$result=count($query);
$result1=count($query1);
//echo $result;
echo $query;
echo $query1;
echo $result1;
$parts = array();
$brandarray=array();
$discarray=array();
$limit = 10;
    $offset = 0;
    foreach( $query as $queryword ){
    $brandarray[] = '`BRAND` LIKE "%'.$queryword.'%"';

}
foreach( $query2 as $discword ){
    $discarray[] = '`DPERCENT` < '.$discword.'';

}
if(!empty($query) && !empty($query2))
{ 
echo "both loops";
$countsql2='SELECT * FROM xml WHERE  ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') ';
print($countsql2);
    $combinesql=mysql_query($countsql2);
    $androws123 = mysql_num_rows($combinesql);  
$countArray1=array();
echo "<br />";
echo $androws123;
$totalrows=0;
$orsqlrows=0;
while($row = mysql_fetch_array($countsql3)) {
    // Append to the array


    $countArray1[] = $row;



    //echo $row['PID']."<BR />";
}
if(empty($countArray1))
{
echo "or";
    $orsql='SELECT * FROM xml WHERE  ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') ';
    $orsql1=mysql_query($orsql);
    $orsqlrows = mysql_num_rows($orsql1);   
    $countArray2=array();
echo $orsqlrows;
while($row = mysql_fetch_array($countsql1)) {
    // Append to the array


    $countArray2[] = $row;



    //echo $row['PID']."<BR />";
}
}
$totalrows=$orsqlrows+$androws123;
echo $orsqlrows;
echo "hi";
echo $androws123;

if($totalrows==$androws123)
{
echo "and";
foreach( $brandcheck as $queryword ){
    $brandarray[] = '`BRAND` LIKE "%'.$queryword.'%"';

}

$brandsql='SELECT * FROM XML WHERE ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') limit '.$offset.', '.$limit.' ';
    $brandsql1=mysql_query($brandsql);
$numrows = mysql_num_rows($brandsql1);  
$countArray=array();
//print($brandsql);
echo "<br />";
while($row = mysql_fetch_array($brandsql1)) {
    // Append to the array


    $countArray[] = $row;



    //echo $row['PID']."<BR />";
}

}
else{
foreach( $brandcheck as $queryword ){
    $brandarray[] = '`BRAND` LIKE "%'.$queryword.'%"';

}
echo "orloop";
$brandsql='SELECT * FROM XML WHERE  ('.implode ('OR',$brandarray).') AND ('.implode ('OR',$discarray).') limit '.$offset.', '.$limit.' ';
//print($brandsql);
    $brandsql1=mysql_query($brandsql);
$numrows = mysql_num_rows($brandsql1);  
$countArray=array();
//print($brandsql);
echo "<br />";
echo $numrows;
while($row = mysql_fetch_array($brandsql1)) {
    // Append to the array


    $countArray[] = $row;



    //echo $row['PID']."<BR />";
}

}

}
?>


<?php
foreach($countArray as $array)
{
?>

<div>
<a href="index23.php?name=<?php $array['IMAGEURL']?>"><img src="<?php echo $array['IMAGEURL']?>"/></a></div>

<?php $i++; } ?>

我是第一个php页面。当我一次检查一个品牌复选框和一个折扣复选框时,意味着检查品牌和折扣中的一个是正确过滤的。但是当我检查两个复选框以外的折扣复选框和品牌的一个,我没有正确过滤div,而div应该被过滤。

帮助我们,我在上面的代码中做错了...

1 个答案:

答案 0 :(得分:0)

你是否回应了查询,看看你得到了什么?在我看来,只要没有真正的内爆和胶水,它就能正常工作。使用过('OR')。我认为您需要将胶水部分更改为' OR '(注意它周围的空间)。