简体   繁体   English

如何使用php和javascript显示特定结果的正确图像?

[英]how to display the proper image for the particular result using php and javascript?

i am doing one website project using php.my result are displayed like this 我正在使用php.my做一个网站项目,结果显示如下

Detected    Result
1.  CLEAN MX       0       clean site
2.  MalwarePatrol  0       unrated site
3.  ZDB Zeus       0       suspicious site
4.  K7AntiVirus    0       clean site
i am using this php code to get this result that is 我正在使用此php代码来获得以下结果

  $none = 0; $i = 0; foreach($result->scans as $key => $val) { if($i==0) { echo '<th></th>'; echo '<th></th>'; echo '<th>Detected</th>'; echo '<th>Result</th>'; } echo '<tr>'; echo '<td>'.intval($i+1).'.</td>'; echo '<td>'.$key.'</td>'; if(empty($val->detected)): echo '<td>'. $none .'</td>'; else: echo '<td>'. $val->detected .'</td>'; endif; echo '<td>'.$val->result.'</td>'; echo '</tr>'; $i++; } 

but i need to add some graphics into the result page. 但我需要在结果页面中添加一些图形。 firstly check the result is clean site or unrated site,suspicious site.then if that site is a clean site display green light image, if that site is unrated site display yellow light,if it is suspicious site display red light image. 首先检查结果是干净的站点还是未评级的站点,可疑站点。然后,如果该站点是干净的站点,则显示绿灯图像;如果该站点是未评级的站点,则显示黄灯图像;如果是可评级的站点,则显示绿灯图像。 like a www.onlinelinkscan.com 's result. 就像www.onlinelinkscan.com的结果一样。

finaly if that site gets most number of green light images display overall result is good,if that site gets most number of red light images display overall result is danger else display overall result is neutral.like this 最终,如果该站点获得最多的绿灯图像显示总体结果是好的,如果该站点获得最多的红光图像显示总体结果是好的,否则显示总体结果是中性的。

  Detected Result 1. CLEAN MX 0 clean site 2. MalwarePatrol 0 clean site 3. ZDB Zeus 0 clean site overall result:good overall result:danger overall result:neutral 

please help me friends i do not have such kind of knowledge in php and javascript. 请帮助我的朋友,我在php和javascript中没有这种知识。

No fancy php or javascript needed. 不需要花哨的php或javascript。 For styling something it is css you need. 对于样式,这是您需要的CSS。 Definitly not add img as they are content, and an icon to anacify your table would not be considered content. 绝对不要添加img因为它们是内容,并且用来使您的表无用的图标将不视为内容。

I would just add a class to each row to indicate the result. 我只是将一个类添加到每一行以指示结果。 Also I would keep a score to indicate the global result: 我还要保留一个分数来表示总体结果:

outside your foreach, prepare the score var 在您的foreach之外,准备分数var

$score = 0;

inside the foreach: 在foreach里面:

// determine which class to add
switch $val->result {
  case 'clean site': 
    $class = "clean";
    $score++;
    break;
  case "unrated site":
    $class = 'neutral';
    break;
  case "suspicious site":
    $class = 'dirty';
    $score--;
    break;
  default:
    // perhaps you should throw an esception here
    $class = '';
    break;
} 
// add it to your row
$out .= '<tr class="' . $class .'">';

Also note that I do not echo anything yet, I store it in a variable. 另请注意,我还没有回显任何内容,而是将其存储在变量中。 You would have to do that with each echo in your code. 您必须对代码中的每个回显进行此操作。 An d make sure you do not add the opening <table> tag yet, as we are going to add the 'global score class' here. d确保您尚未添加开头的<table>标记,因为我们将在此处添加'global score class'。

After the foreach loop has finished, you would have a global score. 在foreach循环完成之后,您将获得一个全局分数。 You could add this as a class to your table, and prepend it to your prepared output like so: 您可以将其作为一个类添加到表中,并将其添加到准备好的输出中,如下所示:

if ($score < 0) {
   $tableClass = 'dirty';
}
if ($score > 0) {
   $tableClass = 'clean';
}
if ($score == 0) {
   $tableClass = 'neutral';
}
$out = '<table class="' . $tableClass .'">' . $out;

All you need to do now is echo the $out variable. 您现在要做的就是echo $ out变量。

To apply the colors or icons or whatever you could add some simple css like this: 要应用颜色或图标或您可以添加一些简单的CSS的方法,如下所示:

table.neutral {
   border-color: grey;
}
table.clean {
   border-color: green;
}
...
tr.clean td:first-child{
   background-image: url(icon-clean.png) no-repeat left center;
   padding-left: 20px;
}
...

This is simple... First check your value, and assign an image to variable... 这很简单...首先检查您的值,然后将图像分配给变量...

 if($val->result=="clean site"){$image = "green-light.png";}
 elseif($val->result=="unrated site"){$image = "orange-light.png";}
 elseif($val->result=="suspicious site"){$image = "red-light.png";}

Then echo that image inside the table. 然后在表内回显该图像。

 echo "<td><img src='images/".$image."'></td>";

This means you have to create 3 images with the names as above. 这意味着您必须使用上面的名称创建3张图像。

And put them in your images folder 并将它们放在您的图片文件夹中

As for the other part of your question....it will involve counting the elements either with PHP, or better with SQL....but Im not gonna do that part for you. 至于您的问题的另一部分...。这将涉及使用PHP或SQL来更好地计数元素。...但是我不会为您做那部分。

Heres fuller code... 这是更完整的代码...

if(empty($val->detected)): 
     echo '<td>'. $none .'</td>';
  else: 
     echo '<td>'. $val->detected .'</td>';
  endif; 

  if($val->result=="clean site"){$image = "green-light.png";}
 elseif($val->result=="unrated site"){$image = "orange-light.png";}
 elseif($val->result=="suspicious site"){$image = "red-light.png";}

   echo "<td><img src='images/".$image."'></td>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM