使用php Web服务基于日期从表中获取所有数据

时间:2015-08-18 09:30:43

标签: php soap

我写了一个php web服务来获取coins大于0时的所有数据。这是代码:
organisationListClient-Copy.php

<?php
require_once("lib/nusoap.php");
// Create the client instance
$client = new nusoap_client("http://localhost/TTrockstars/ws/organisationListWS-copy.php");
//check if there were any instantiation errors, and if so stop execution with an error message:
$error = $client->getError();
echo ($error);
if ($error) {
    die("client construction error: {$error}\n");
}

// Call the SOAP method
$result = $client->call("getOrganisationList");

$error = $client->getError();

if ($error) {
    print_r($client->response);
    print_r($client->getDebug());
    die();
}

// Display the result
// echo "<pre>";
print_r($result);
//$fichero = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/images/'.$imgname, FILE_USE_INCLUDE_PATH);
//echo ($_SERVER['DOCUMENT_ROOT'].'/data/');
// echo "</pre>";



?>  

organisationListWS-Copy.php

<?php
    //****including config file****
include 'config.php';

function getOrganisationList() {

    $db = dataBaseConn();
    $con = mysqli_connect($db['DB_SERVER'],$db['DB_USER'],$db['DB_PASSWORD'],$db['DB_DATABASE']);

    if($con){
        mysqli_set_charset($con,"utf8");
        /*$orgListRS = mysqli_query($con,"SELECT name  FROM prop ");*/
        $orgListRS = mysqli_query($con,"SELECT id, name, image, extra_image  FROM prop where coins > 0");
        $orgList = [];

        while($orgListRow = mysqli_fetch_array($orgListRS, MYSQLI_BOTH)){

                //id
            $usrId = $orgListRow['id'];
            $usrId = str_replace(",", ",", $usrId);
            $usrId = str_replace(" ", " ", $usrId);
            $orgList[] = $usrId;

                //name      
            $usrName = $orgListRow['name'];
            $usrName = str_replace("’", "'", $usrName);
            $usrName = str_replace(" ", " ", $usrName);
            $orgList[] = $usrName;

                //image
            $usrImage = $orgListRow['image'];
            $usrImage = str_replace("", "", $usrImage);
            $usrImage = str_replace(" ", " ", $usrImage);
            $orgList[] = $usrImage;

                //extra image
            $usrExtimage = $orgListRow['extra_image'];
            $usrExtimage = str_replace(",", ",", $usrExtimage);
            $usrExtimage = str_replace(" ", " ", $usrExtimage);
            $orgList[] = $usrExtimage;


        }
        mysqli_free_result($orgListRS);
        $replyJson["status"]="SUCCESS";
        $replyJson["orgList"]= $orgList;
        mysqli_close($con);
        return json_encode($replyJson);

    }else{
        $replyJson["status"]="FAILED";
        return json_encode($replyJson);
    }

}

require_once("lib/nusoap.php");

$urlPrefix = nameSpaceURL();

$URL       = $urlPrefix . "/ws/organisationListWS-copy.php";
$namespace = $URL . '?wsdl';

$server = new soap_server();

$server->configureWSDL("WebServices for getting list of organisations");

$server->register("getOrganisationList",    
    array(),
                // return value(s):
    array('return'=>'xsd:string'),
                // namespace:
    $namespace,
                // soapaction: (use default)
    false,
                // style: rpc or document
    'rpc',
                // use: encoded or literal
    'encoded',
                // description: documentation for the method
    'Return list of organistaions');

$_HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : "";
$server->service($_HTTP_RAW_POST_DATA);

?>  

当我在浏览器中运行它时,我会获得所有ID,名称,图像和硬币大于0的额外图像。这是一个屏幕截图:
img
这是我的桌子结构:
img1
现在我需要像这样更改sql查询:

if (updated_date=="new_date") { 
//Send all data (return ids, names, images, extra images whose coins are greater than 0)
}
else { 
//Get ids, names, images and extra images which are added after updated date
}  

如何修改当前sql查询以实现该目标?

1 个答案:

答案 0 :(得分:0)

这很简单,如果条件存在,您只需要为查询创建新条件,并将其添加到原始查询中。如果condiftion不存在,$extraCriteria将为空,并且不会对原始查询进行任何限制。

$extraCriteria = '';
if ($updated_date !== "new_date") { 
    //Get ids, names, images and extra images which are added after updated date
    $extraCriteria = " AND updated_date > '$updated_date'";
}  


$orgListRS = mysqli_query($con,"SELECT id, name, image, extra_image  
                                 FROM prop 
                                 where coins > 0 
                                 $extraCriteria");

我当然猜测你的列和变量的实际名称,但我希望这能给你一个基本的想法。