如何根据获取url值定义条件

时间:2016-12-25 09:16:08

标签: php if-statement get conditional

当用户点击"列表视图"链接然后我想向他们展示"列表视图" HTML和他们点击"网格视图"我想展示"网格视图" HTML。 我已经定义了以下链接来点击 -

import Foundation
import UIKit

class Web {
    var ar1 = [Double]()
    var ar2 = [Double]()

    func getData(str: String) -> [Double] {
    let request = NSMutableURLRequest(url: URL(string: str)!)
      httpGet(request as URLRequest!){
            (data, error) -> Void in
            if error != nil {
                print(error ?? "Error")
            } else {
                let delimiter = "\t"
                let lines:[String] = data.components(separatedBy:     CharacterSet.newlines) as [String]

                for line in lines {           
                    var values:[String] = []
                    if line != "" {
                        values = line.components(separatedBy: delimiter)
                         let str1 = (values[0])//FIRST COLUMN
                        let str2 = (values[1])//SECOND COLUMN
                       let db1 = NumberFormatter().number(from: str1)?.doubleValue
                        self.ar1.append(db1!)
                        let db2 = NumberFormatter().number(from: str2)?.doubleValue
                        self.ar2.append(db2!)
                    }
                }
            dump (self.ar1) // "ar1" accessible HERE (returns all elements)
        }
    }//end of request
        //BUT I WANT TO RETURN "ar1" HERE !
       // So that I can use it in my MAIN class
        dump (self.ar1) // "ar1" not accessible here (returns ZERO elements)
        return self.ar1
    }
        func httpGet(_ request: URLRequest!, callback: @escaping (String, String?) -> Void) {
            let session = URLSession.shared
            let task = session.dataTask(with: request, completionHandler: {
                (data, response, error) -> Void in
                if error != nil {
                callback("", error!.localizedDescription)
                } else {
                let result = NSString(data: data!, encoding:
                    String.Encoding.ascii.rawValue)!
                callback(result as String, nil)
                }
            })
            task.resume()
}
    }

然后我使用PHP get方法定义了以下条件,以向用户显示所需的输出 -

<a href="?view=list">List View</a> <br>
<a href="?view=grid">Grid View</a>

这种情况不起作用。如果我改变&#34;查看&#34;到&#34; view_1&#34;和&#34; view_2&#34;从URL然后我的条件也正常。

<?php
if( isset( $_GET['view'] ) == 'list' ){
  echo "This is List view";
}else if( isset($_GET['view'] ) == 'grid' ){
  echo "This is Grid view";
}

但我不想改变&#34;视图&#34;键。我只想保留两个URL的相同键和不同值来执行条件语句 有可能吗?

2 个答案:

答案 0 :(得分:0)

isset()仅检查变量是否存在且不为null并返回布尔值(true / false)。

要检查值,首先需要检查它是否存在(isset),然后检查值,如下所示::

if (isset($_GET['view']) && $_GET['view'] == 'list') {
    // your code here.
}

您可以在此处阅读更多内容:http://php.net/manual/en/function.isset.php

替代

如果您想使您的代码和条件更具可读性,可以这样做:

// Get the value of $_GET['view'] once, if it exists (pre PHP 7)
$view = isset($_GET['view']) ? $_GET['view'] : null;

// PHP 7.x (new shorter syntax for the above)
$view = $_GET['view'] ?? null;


if ($view == 'list') {
    // your code
}

答案 1 :(得分:0)

你有没有试过这个..

if(isset($_GET['view']))
{
     if($_GET['view'] == 'list')
     {
      //code here
     }
    elseif ($_GET['view'] == 'grid')
    {
    //code here
    }
}