不能让简单的if / else语句工作


Can't get simple if / else statement to work

我正在我的DB中搜索一个表,如果一个名称与我在URL中传递的$_GET['name']变量匹配,则返回一个刻度。

如果名称存在,则显示勾号,如果名称不存在,则显示叉号。

目的是填充可用性的轮换表/时间表。

我到目前为止的代码;

<?php foreach($rota_sun as $rota_sunday): ?>
       <?php if(strpos($rota_sunday['person_name'], $_GET['name']) !== false) { ?>
              <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
              <?php } else { ?>
              <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>
      <?php } ?>
 <?php endforeach; ?>

我的查询代码是;

$query = "SELECT
                rota_sunday.id AS rota_id,
                rota_sunday.person_id,
                rota_sunday.person_name
            FROM rota_sunday WHERE rota_sunday.person_name = '$_GET['$name']'
        "; 
    try 
    { 
        $stmt = $conn->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $rota_sun = $stmt->fetchAll(PDO::FETCH_ASSOC);
  1. id
  2. person_id
  3. person_name

我使用的数组是;

Array
(
    [0] => Array
        (
            [rota_id] => 16
            [person_id] => 0
            [person_name] => Gina
        )
)

您将看到Gina存在-因此,如果?name=Gina在我的URL中应该显示一个勾号,但?name=Fred?name=John等在我的URL中应该显示一个叉。

Issue:当名称存在时,勾号显示,但当名称不存在时,叉号不显示

工作示例:-

<link rel="stylesheet" href="font-awesome-4.6.3/css/font-awesome.min.css">
<?php
  error_reporting(E_ALL); //check all type of errors
  ini_set('display_errors',1); // display those if any happen
  $_GET['name'] = 'fiksun';
  $rota_sun = Array
(
    '0' => Array
        (
            'rota_id' => 16,
            'person_id' => 0,
            'person_name' => 'Gina'
        ),
    '1' => Array
        (
            'rota_id' => 16,
            'person_id' => 0,
            'person_name' => 'fiksun'
        )
)
?>
<?php foreach($rota_sun as $rota_sunday): ?>
    <?php if($rota_sunday['person_name']== $_GET['name']) { ?>
        <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
      <?php } else { ?>
        <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
  <?php } ?>
<?php endforeach; ?>

输出:http://prntscr.com/cf4ecd

注意:-从这里下载font-awesome库:- http://fontawesome.io/get-started/

将完整文件夹放入当前工作目录

正确添加css文件

这是工作目录结构:- http://prntscr.com/cf4fuk

更改查询:-

$query = "SELECT rota_sunday.id AS rota_id, rota_sunday.person_id AS person_id, rota_sunday.person_name AS person_name FROM rota_sunday WHERE rota_sunday.person_name Like '%".$_GET['$name']."%'";

这个查询将只从数据库中选择一个人。

$query = "SELECT
            rota_sunday.id AS rota_id,
            rota_sunday.person_id,
            rota_sunday.person_name
        FROM rota_sunday WHERE rota_sunday.person_name = " . $_GET['name'] . "LIMIT 1"; 
try 
{ 
    $stmt = $conn->prepare($query); 
    $stmt->execute(); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to run query: " . $ex->getMessage()); 
} 
$rota_sun = $stmt->fetch(PDO::FETCH_ASSOC);

如果这个人存在,那么$rota_sun将包含result;如果不存在,那么这将返回false,您将获得红色叉。

   <?php if($rota_sun) { ?>
          <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
   <?php } else { ?>
          <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>
   <?php } ?>