PHP在foreach循环中分别检查每个字符串是否相等



我想让我的表更可见。我的表将从数据库创建。很简单,比如:

Test1 | Test2 ...
---------------
Yes   | No    ...
Yes   | Yes   ...
No    | Yes   ...
Yes   | No    ...

我希望所有的Yes都是绿色的,所有的No都是红色的。

表项将由foreach循环创建。

我有一个例子,但我用数组代替,因为它更容易显示问题。但我认为原理是一样的。(我只测试了第一行)

<!DOCTYPE html>
<html>
<body>
<?php
$contacts = array('N', 'J', 'N', 'N', 'J');
$demo_str = 'J';
?>
<table style="border-radius: 10px; overflow: hidden; background: #fff; box-shadow: 5px 5px 5px #dfdfdf;" class="shadow">
<thead>
<tr>
<td style="width: 180px;">Test1</td>
<td style="width: 105px;">Test2</td>
<td style="width: 100px;">Test3</td>
<td style="width: 80px; ">Test4</td>
<td style="width: 80px; ">Test5</td>
<td style="width: 98px; ">Test6</td>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact): ?>
<tr>
<td><?php  if ($demo_str == $contact) {
echo "<html>
<head>
<style>
.redifnull1 {
color: green;
}
</style>
</head>
</html>";
}
else {
echo "<html>
<head>
<style>
.redifnull1 {
color: red;
}
</style>
</head>
</html>";
};
?><strong class="redifnull1"><?=$contact['test1']?></strong></td>
<td><?=$contact['test2']?></td>
<td><?=$contact['test3']?></td>
<td><?=$contact['test4']?></td>
<td><?=$contact['test5']?></td>
<td><?=$contact['test6']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

问题是,它只比较"$contacts = array('N', 'J', 'N', 'N', 'N', ->'J'<-);"如果最后一个是"n",那么一切都是绿色的。一切都是红色的。

有人知道如何解决这个问题吗?非常感谢,

最好

编辑(我如何从数据库获取):

// Connect to MySQL database
$pdo = pdo_connect_mysql();
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$records_per_page = 8;

$stmt = $pdo->prepare('SELECT * FROM contacts ORDER BY date LIMIT :current_page, :record_per_page');
$stmt->bindValue(':current_page', ($page-1)*$records_per_page, PDO::PARAM_INT);
$stmt->bindValue(':record_per_page', $records_per_page, PDO::PARAM_INT);
$stmt->execute();
$contacts = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_contacts = $pdo->query('SELECT COUNT(*) FROM contacts')->fetchColumn();

创建不同的<style>块不像这样工作。页面中的所有样式都会合并,选择器的最后一个样式优先。

你也不能在一个文档中有多个<html><head>标签。每次循环都会创建新的对象。

相反,创建一个单独的样式块,定义两个类,redifnullgreenifnull。然后在表行中使用适当的类。
<!DOCTYPE html>
<html>
<head>
<style>
.rediffnull {

color: red;
}
.greenifnull {
color: green;
}
</head>
<body>
<?php
$contacts = array('N', 'J', 'N', 'N', 'J');
$demo_str = 'J';
?>
<table style="border-radius: 10px; overflow: hidden; background: #fff; box-shadow: 5px 5px 5px #dfdfdf;" class="shadow">
<thead>
<tr>
<td style="width: 180px;">Test1</td>
<td style="width: 105px;">Test2</td>
<td style="width: 100px;">Test3</td>
<td style="width: 80px; ">Test4</td>
<td style="width: 80px; ">Test5</td>
<td style="width: 98px; ">Test6</td>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact): ?>
<tr>
<td>
<td><strong class="<?= ($contact == $demo_str) ? "greenifnull" : "redifnull" ?>"><?=$contact['test1']?></strong></td>
<td><?=$contact['test2']?></td>
<td><?=$contact['test3']?></td>
<td><?=$contact['test4']?></td>
<td><?=$contact['test5']?></td>
<td><?=$contact['test6']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

试一下:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.green { color: green; }
.red { color: red; }
</style>
</head>
<body>
<?php
$contacts = array('N', 'J', 'N', 'N', 'J');
$demo_str = 'J';
?>
<table style="border-radius: 10px; overflow: hidden; background: #fff; box-shadow: 5px 5px 5px #dfdfdf;" class="shadow">
<thead>
<tr>
<td style="width: 180px;">Test1</td>
<td style="width: 105px;">Test2</td>
<td style="width: 100px;">Test3</td>
<td style="width: 80px; ">Test4</td>
<td style="width: 80px; ">Test5</td>
<td style="width: 98px; ">Test6</td>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact): ?>
<tr>
<td>
<strong class="<?= $contact === 'J' ? 'green' : 'red'; ?>"><?=$contact['test1']?></strong>
</td>
<td><?=$contact['test2']?></td>
<td><?=$contact['test3']?></td>
<td><?=$contact['test4']?></td>
<td><?=$contact['test5']?></td>
<td><?=$contact['test6']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

我喜欢以一种以后可以添加更多样式的方式来处理问题:

<?php
$contacts = array('N', 'J', 'N', 'N', 'J');
$contact_styles = [
'J' => 'rediffnull',
'N' => 'greenifnull',
];
?>

你可以这样做:

<td>
<strong class="<?=$contact_styles[$contact[0]]?>"><?=$contact[0]?></strong> 
</td>

最新更新