php如果表单字段为空则设置一个变量

时间:2014-09-15 21:58:12

标签: php jobamatic

目前,我的API正在发出以下http请求:

http://api.simplyhired.com/a/jobs-api/xml-v2/q-Golf%golf/k-golf?pshid=mypshid&ssty=2&cflg=r&auth=myauth&clip=76.68.98.34

我需要在更改API之后按照以下格式进行格式化:

http://api.simplyhired.com/a/jobs-api/xml-v2/q-golf/sb-rd/ws-10/pn-1?pshid=mypshid&ssty=2&cflg=r&auth=myauth&clip=76.68.98.34

q =搜索字词以及我需要它做的是默认为" golf"如果搜索关键字部分为空(或第一次访问者)

所以我需要编写类似下面的代码:

如果$ query为null,那么$ query =' golf' else $ query = trim($ query);

我不理解以下内容,并认为这是需要调整的内容。这是在默认搜索项的插件设置部分中设置的。我只想改变代码本身。

if( strpos( $query, 'golf' ) === false) { 
        $query .= 'golf';
    }

以下是我正在使用的WP插件的php代码:

    <?php
require_once(dirname(__FILE__) . '/RemoteConnector.php');
require_once(dirname(__FILE__) . '/JobamaticXMLParser.php');

define('JOBAMATIC_INVALID_URL_ERROR', 3000);
define('JOBAMATIC_XMLPARSER_ERROR', 3001);

class SimplyHiredJobamaticAPI {
  protected $pshid;
  protected $jbd;
  protected $ssty;
  protected $cflg;
  protected $auth;
  protected $clip;
  protected $error;
  protected $code;
  protected $data;

  const JOBAMATIC_ENDPOINT = 'http://api.simplyhired.com/a/jobs-api/xml-v2/';


  public function __construct($pshid, $jbd) {
    $this->pshid = trim($pshid);
    $this->jbd = trim($jbd);
    $this->ssty = 2;
    $this->cflg = 'r';
    $this->auth = 'myauth';
    $this->clip = $_SERVER['REMOTE_ADDR'];
    $this->error = NULL;
    $this->code = NULL;
    $this->data = NULL;
  }

  public function search($query, $size = 10, $page = 0, $location = '', $miles = 5, *emphasized text*$sort = 'rd', $constant = 'golf') {


    if( strpos( $query, 'golf' ) === false) {
        $query .= 'golf';
    }
    $params = array(
      'q' => rawurlencode(trim($query)),
      'sb' => $sort,
      'ws' => $size,
      'pn' => (intval($page) < 1 ? 0 : intval($page)),
    'k'  => $constant,
    );

    if (!is_null($location) && $location != '') {
      $params['l'] = rawurlencode($location);
    }

    if (!is_null($location) && intval($miles) > 0) {
      $params['m'] = $miles;
    }

    $this->call($params);
    if($this->code == 200) {
      return $this->data;
    } else {
      return FALSE;
    }
  }

  protected function call($criteria) {
    $url = self::JOBAMATIC_ENDPOINT . '%s?';

    $api_identity = array(
      'pshid' => $this->pshid,
      /*'jbd' => $this->jbd,*/
      'ssty' => $this->ssty,
      'cflg' => $this->cflg,
      'auth' => $this->auth,
      'clip' => $this->clip,

    );

    $identity_string = array();
    foreach ($api_identity as $key => $value) {
      $identity_string[] = $key . '=' . $value;
    }

    $url .= implode('&', $identity_string);

    $params = array();

    foreach ($criteria as $key => $value) {
      $params[] = $key . '-' . $value;
    }

    $url = sprintf($url, implode('/', $params));
    echo $url;
    try {
      $connector = new RemoteConnector($url);
      $this->code = $connector->getStatus();
    } catch (Exception $e) {
      $this->error = $e->getMessage();
      $this->code = JOBAMATIC_INVALID_URL_ERROR;
      return;
    }

    $this->parseXML($connector->__toString());
  }

  protected function parseXML($data) {
    $parser = JobamaticXMLParser::getParser();
    if(!$this->data = $parser->parse($data)) {
      $this->code = JOBAMATIC_XMLPARSER_ERROR;
      $this->error = 'JobamaticXMLParser error: ' . $parser->getErrorMessage();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

if这个小$query条件基本上是这样说:如果golf NOT 中包含单词golf,请附加(ADD)单词{{1 }到$query字符串的末尾。

if( strpos( $query, 'golf' ) === false) { 
    $query .= 'golf';
}

根据您的问题,您当然需要将其更改为:

if(empty($query) || $query == "" || $query == NULL) {
    $query = "golf";
} else {
    $query = trim($query);
}

那你正在追逐的是什么?检查一下,如果查询字符串为空或为空,请向其添加golf或仅修剪它?