William Willi William Willi - 3 months ago 7
MySQL Question

how to pass two parameter and get the filtered result in php?

I have made table in MYSQL with columns 'name','worker_id','local_job_id' here my php code gives the corresponding name for the given local_job_id
from the link

http://allwaysready.16mb.com/Cuboid/LocalCheckRequest.php?local_job_id[]=3


Gives

{"result":[{"worker_id":"2"},{"worker_id":"1"}]}


but what i want to change in this code is display the name if both worker_id AND local_job_id are matching .

"http://allwaysready.16mb.com/Cuboid/LocalCheckRequest.php?local_job_id[]=3=2&worker_id=2" then the 'name' which corresponding to the local_job_id=3 and worker_id=2 should be displayed.

<?php

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$local_job_id = $_GET['local_job_id'];
require_once ('dbConnect.php');

$local_job_request = array();
foreach($_REQUEST['local_job_id'] as $key => $val) {
$local_job_request[$key] = filter_var($val, FILTER_SANITIZE_STRING);
}

$local_job_ids = "'" . implode("','", $local_job_request) . "'";
$sql = "SELECT * FROM local_job_request WHERE local_job_id IN ({$local_job_ids})";
$r = mysqli_query($con, $sql);
$result = array();
while ($res = mysqli_fetch_array($r)) {
array_push($result, array(
"worker_id" => $res['worker_id']
));
}

echo json_encode(array(
"result" => $result
));
mysqli_close($con);
}

Answer

Try this might be helpful for you

if($_SERVER['REQUEST_METHOD']=='GET'){

    $local_job_id  = $_REQUEST['local_job_id'];
    $worker_id  = $_GET['worker_id'];
    require_once('dbConnect.php');

    $local_job_request = array();

           foreach ($local_job_id as $key => $val) {
           $local_job_request[$key] = filter_var($val, FILTER_SANITIZE_STRING);
            }
            $local_job_ids = "'" . implode("','", $local_job_request) . "'";

            $sql = "SELECT * FROM local_job_request WHERE local_job_id IN ({$local_job_ids})";

            if(isset($worker_id) && $worker_id != "") {
                $sql .= " AND worker_id = ".$worker_id;
            }
    $r = mysqli_query($con,$sql);

    $result = array();

            while($res = mysqli_fetch_array($r)){

    array_push($result,array(
        "worker_id"=>$res['worker_id']
        )
    );
            }
    echo json_encode(array("result"=>$result));

    mysqli_close($con);

}