gpt4 book ai didi

php - 无法使简单的 if/else 语句起作用

转载 作者:行者123 更新时间:2023-11-28 23:25:52 25 4
gpt4 key购买 nike

我在我的 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. 编号
  2. person_id
  3. 人名

我使用的数组是;

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

)

您会看到 Gina 存在 - 所以如果 ?name=Gina 在我的 URL 中应该显示一个勾号但是 ?name=Fred? name=John 等在我的 URL 中应该显示一个叉号。

问题:名称存在时显示勾号,名称不存在时不显示叉号。

最佳答案

一个工作示例:-

<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']."%'";

关于php - 无法使简单的 if/else 语句起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com