[英]How to change font colour based on ID number from output
so I have a JSON url that I am pulling post titles from and that all works, but what I want to do is have it so that depending on the userID, determines what colour the post would be, so for example if the userID is 1 then make the font colour red. 所以我有一个要从中提取帖子标题的JSON URL,并且所有工作正常,但是我想要做的就是拥有它,以便根据userID来确定帖子的颜色,例如,如果userID为1然后将字体颜色设为红色。
$data = file_get_contents($json_url);
$posts = json_decode($data);
foreach ($posts as $post) {
echo $post->title . '<br>';
}
Wrap your title in a <span>
or <div>
and give it a class related to the userId: 将标题包裹在
<span>
或<div>
并给它一个与userId相关的类:
foreach ($posts as $post) {
echo sprintf('<span class="titleUser%d">%s</span>', $post->userId, $post->title);
}
(assuming $post->userId
is your userId, change this to fit your needs) (假设
$post->userId
是您的userId,请更改此值以适合您的需求)
You'll end up with different classes like titleUser1
, titleUser27
etc. 您将得到不同的类,例如
titleUser1
, titleUser27
等。
Define the styles for the classes you want to change in your CSS: 定义要在CSS中更改的类的样式:
.titleUser1 {color: red}
.titleUser27 {color: green}
No need to create if
clauses for every user, just add a new class to your style when needed. 无需为每个用户创建
if
子句,只需在需要时向您的样式添加一个新类。
Try this: 尝试这个:
foreach ($posts as $post) {
if($post->userId == 1){
echo "<div style='color:red'>".$post->title ."</div><br>";
}
elseif ($post->userId == 1){
echo "<div style='color:blue'>".$post->title ."</div><br>";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.