过滤多个复选框php的更好方法

时间:2017-10-25 21:27:09

标签: php mysql checkbox

我正在尝试制作一个php,mysql简单的复选框过滤器。 enter image description here

因此,复选框有3个部分,用户需要至少选择其中任何一个以显示结果。

我的代码

if(isset($_POST['cse'])){ $cse=1;} else $cse=0;
    if(isset($_POST['ee'])){ $ee=2;} else $ee=0;
    if(isset($_POST['me'])){ $me=3;} else $me=0;

    if(isset($_POST['btech'])){ $btech=1;} else $btech=0;
    if(isset($_POST['mtech'])){ $mtech=2;} else $mtech=0;
    if(isset($_POST['phd'])){ $phd=3;} else $phd=0;

    if(isset($_POST['y2014'])){ $y2014=14;} else $y2014=0;
    if(isset($_POST['y2015'])){ $y2015=15;} else $y2015=0;
    if(isset($_POST['y2016'])){ $y2016=16;} else $y2016=0;
    if(isset($_POST['y2017'])){ $y2017=17;} else $y2017=0;
    if(isset($_POST['cg'])){$cg=test_input($_POST['cg']);} else $cg=0;



if($cse!=0 && $btech!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$btech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}}


if($cse!=0 && $btech!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$btech AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($cse!=0 && $btech!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$btech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($cse!=0 && $btech!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$btech AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}


if($cse!=0 && $mtech!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$mtech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}


if($cse!=0 && $mtech!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$mtech AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($cse!=0 && $mtech!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$mtech AND year=$y2016 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($cse!=0 && $mtech!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$mtech AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($cse!=0 && $phd!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$phd AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($cse!=0 && $phd!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$phd AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($cse!=0 && $phd!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$phd AND year=$y2016 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($cse!=0 && $phd!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$cse AND batch=$phd AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}




if($me!=0 && $btech!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$btech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($me!=0 && $btech!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$btech AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($me!=0 && $btech!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$btech AND year=$y2016 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($me!=0 && $btech!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$btech AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}


if($me!=0 && $mtech!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$mtech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}


if($me!=0 && $mtech!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$mtech AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($me!=0 && $mtech!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$mtech AND year=$y2016 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($me!=0 && $mtech!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$mtech AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($me!=0 && $phd!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$phd AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($me!=0 && $phd!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$phd AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($me!=0 && $phd!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$phd AND year=$y2016 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($me!=0 && $phd!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$me AND batch=$phd AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($ee!=0 && $btech!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$btech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($ee!=0 && $btech!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$btech AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($ee!=0 && $btech!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$btech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($ee!=0 && $btech!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$btech AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}


if($ee!=0 && $mtech!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$mtech AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}


if($ee!=0 && $mtech!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$mtech AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($ee!=0 && $mtech!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$mtech AND year=$y2016 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($ee!=0 && $mtech!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$mtech AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}



if($ee!=0 && $phd!=0 && $y2014!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$phd AND year=$y2014 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($ee!=0 && $phd!=0 && $y2015!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$phd AND year=$y2015 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($ee!=0 && $phd!=0 && $y2016!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$phd AND year=$y2016 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

if($ee!=0 && $phd!=0 && $y2017!=0){
$sql = "SELECT * FROM tnp_users WHERE branch=$ee AND batch=$phd AND year=$y2017 AND cgpa>=$cg";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $entry_no=$row['entry_no'];
        echo "<tr><td> " . $row["entry_no"]."</td><td contenteditable='true' onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",0)'>" . $row["cgpa"]."</td><td contenteditable='true'  onBlur='saveToDatabase(this.innerHTML,".$row["user_id"].",1)' >".$row["strikes"]."</td></tr>";
    }

}
}

现在我手动完成了用户可以做的所有情况,在我的方法中有很多重复,如果没有复选框增加它将很难统计所有情况

1 个答案:

答案 0 :(得分:1)

我觉得某些事情在某个地方变得越来越复杂。

我将专注于这个陈述......

  

因此,复选框有3个部分,用户需要至少选择其中任何一个以显示结果。

在此示例中,我有三组复选框category[branch]category[batch]category[year]。如果不选择集合中的项目,则post变量不存在并通过isset()count()验证。

  

/! \本代码不是SQL注入安全

<?php
if( $_SERVER['REQUEST_METHOD'] === 'POST')
{
  if( isset( $_POST['category'] ) === true and count( $_POST['category'] ) === 3 )
  {
    $category = array_keys( $_POST['category'] );
    $selections = call_user_func_array('array_merge', $_POST['category']);
    $where = array();
    foreach( $_POST['category'] as $key => $value )
    {
      $where[] = $key . " IN ('" . implode("','",$value)."') ";
    }
    echo 'three areas selected; ' . implode( ', ', $category );
    echo 'you selected: ' . implode( ', ', $selections );
    echo 'WHERE ' . implode( ' AND ', $where );
  }
  else
  {
    echo 'please select atleast any one of each category';
  }
}

?>
<form method="post">
  <fieldset>
    <input type="checkbox" name="category[branch][]" value="cse">cse
    <input type="checkbox" name="category[branch][]" value="ee">ee
    <input type="checkbox" name="category[branch][]" value="me">me
  </fieldset>
  <fieldset>
    <input type="checkbox" name="category[batch][]" value="btech">btech
    <input type="checkbox" name="category[batch][]" value="mtech">mtech
    <input type="checkbox" name="category[batch][]" value="phd">phd
  </fieldset>
  <fieldset>
    <input type="checkbox" name="category[year][]" value="y2014">y2014
    <input type="checkbox" name="category[year][]" value="y2015">y2015
    <input type="checkbox" name="category[year][]" value="y2016">y2016
    <input type="checkbox" name="category[year][]" value="y2017">y2017
    <input type="checkbox" name="category[year][]" value="cg">cg
  </fieldset>
  <button>Submit</button>
</form>