简体   繁体   English

如果一个值小于另一个值,则突出显示该值

[英]Highlight a value if its less than another value

Ok, So this had to be edited because there wasn't enough room to keep commenting between myself and larsAnders. 好的,因此必须对此进行编辑,因为我和larsAnders之间没有足够的空间来发表评论。

Basically this has been totally changed since the original post to a more efficient way. 从最初的帖子开始,这基本上已经完全改变为一种更有效的方式。 This code is working out if the stock we currently hold is lower than our minimum stock requirements, it will highlight the required row to tell me that I need to order more of that specific stocked part. 如果当前持有的存货低于我们的最低存货要求,此代码正在计算中,它将突出显示必填行以告诉我需要订购更多的特定库存零件。

The First column is what we have in stock (lloblevel) The Secound column is what we should have in stock at all times (ilevel) 第一栏是库存量(lloblevel)Secound栏是我们始终库存量(ilevel)

The following is what I have so far 以下是我到目前为止所拥有的

index.php index.php

<head>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="1px">
<?php for ($i=0; $i<count($lloblevel_val);$i++) { ?>
<tr>
<td>
    <?php if ($lloblevel_val[$i] > $ilevel_val[$i]) {
        echo '<span class="highlight">'.$lloblevel_val[$i].'</span><br/>';
    } else {
        echo $lloblevel_val[$i].'<br/>'; 
    }?>
</td>
<td>
    <?php if ($ilevel_val[$i] > $lloblevel_val[$i]) {
        echo '<span class="highlight">'.$ilevel_val[$i].'</span><br/>';
    } else {
        echo $ilevel_val[$i].'<br/>'; 
    }?>
</td>
</tr>
<?php }//end for ?>
</table>
</body>

style.css style.css

@charset "utf-8";
/* CSS Document */
.highlight {
    background-color:#CC0000;
}

levels.php levels.php

<?PHP

$file_handle = fopen("stockexport.csv", "r");
echo $file_handle;

while (!feof($file_handle) ) {

    $line_of_text = fgetcsv($file_handle, 1024);
    print_r($line_of_text);
    $ilevel_val[] = $line_of_text[2];
    $lloblevel_val[] = $line_of_text[1];

}

fclose($file_handle);

?>

First few rows of my csv 我的csv的前几行

    ItemNumber,ItemTitle,Level,MinimumLevel,InOrderBook,Level_LessOrderBook
    "##### BATTLEFIELD 4 VOUCHER #####","##### BATTLEFIELD 4 VOUCHER #####","108","10","14","94"
"##### BRONZE GAME VOUCHER #####","##### BRONZE GAME VOUCHER #####","170","0","0","170"
"##### CRYSIS 3 VOUCHER #####","##### CRYSIS 3 VOUCHER #####","56","10","2","54"
"##### GOLD GAME VOUCHER #####","##### GOLD GAME VOUCHER #####","56","0","0","56"
"##### RAID 0 (STRIPE) SERVICE #####","##### RAID 0 (STRIPE) SERVICE #####","0","-1","0","0"
"##### RAID 1 (MIRROR) SERVICE #####","##### RAID 1 (MIRROR) SERVICE #####","0","-1","0","0"
"##### SAINTS ROW IV VOUCHER #####","##### SAINTS ROW IV VOUCHER #####","20","2","0","20"
"##### SILVER GAME VOUCHER #####","##### SILVER GAME VOUCHER #####","21","10","24","0"
"##### TOMB RAIDER VOUCHER #####","##### TOMB RAIDER VOUCHER #####","31","10","7","24"
"##### WINDOWS 7 PROFESSIONAL x64 TRIAL INSTALL & UPDATE #####","##### WINDOWS 7

Many thanks to larsAnders for the many hours he has taken to help me get this far. 非常感谢larsAnders花费了许多时间来帮助我实现这一目标。

One way to do it for this structure, is to load output of each php file into javascript variable. 为此结构执行此操作的一种方法是将每个php文件的输出加载到javascript变量中。 Then parse this variables into multidimensional array. 然后将此变量解析为多维数组。 And then build table by javascript. 然后用javascript建立表格。
Second way (which is better) is to combine php files into one. 第二种方法(更好)是将php文件合并为一个。 Do all logic there and build html table also there by printing 'titleval1val2'. 在那里做所有逻辑,并通过打印'titleval1val2'在那建立html表。

Since you're reading the same csv-file in each of your php-files you have the information of each available at the point where you print them. 由于您要在每个php文件中读取相同的csv文件,因此在打印它们时,您将获得每个可用的信息。 You could simply put an if-statement around one of them and check the other, and output some html with coloured styling accordingly. 您可以简单地在其中一个中放置一个if语句,然后检查另一个,然后输出带有彩色样式的html。

But you probably want to refactor your code and maybe spend some more time studying, because you current approach is not going to scale much beyond this kind of processing. 但是您可能想要重构代码,并可能花更多的时间研究代码,因为您当前的方法将不会扩展到这种处理之外。

Assign the values to variables, and then work with them in the index.php file... 将值分配给变量,然后在index.php文件中使用它们。

Combine lloblevel.php and ilevel.php into levels.php, and capture both values in arrays: 将lloblevel.php和ilevel.php合并到level.php中,并捕获数组中的两个值:

<?PHP

$file_handle = fopen("stockexport.csv", "r");

while (!feof($file_handle) ) {

    $line_of_text = fgetcsv($file_handle, 1024);
    $ilevel_val[] = $line_of_text[3];
    $lloblevel_val[] = $line_of_text[5];

}

fclose($file_handle);

?>

Within index.php, add an include statements to the top of the file. 在index.php中,在文件顶部添加一个include语句。 This brings both arrays in to the main file. 这会将两个数组都带入主文件。

<?php include 'levels.php'; ?>

Then, add this code where the values will print to the page: 然后,添加以下代码,将值打印到页面上:

<?php for ($i=0; $i<count($lloblevel_val);$i++) { ?>
<tr>
<td>
    <?php if ($lloblevel_val[$i] > $ilevel_val[$i]) {
        echo '<span class="highlight">'.$lloblevel_val[$i].'</span><br/>';
    } else {
        echo $lloblevel_val[$i].'<br/>'; 
    }?>
</td>
<td>
    <?php if ($ilevel_val[$i] > $lloblevel_val[$i]) {
        echo '<span class="highlight">'.$ilevel_val[$i].'</span><br/>';
    } else {
        echo $ilevel_val[$i].'<br/>'; 
    }?>
</td>
</tr>
<?php }//end for ?>

And finally, in your stylesheet, add a highlight class 最后,在样式表中,添加一个高亮显示类

.highlight {
    background-color:#CC0000;
}

There might be many possible ways to do that ... 可能有许多可能的方法可以做到这一点...

Add id to your <td> tags id添加到您的<td>标签

<td id="llob"><?php include 'lloblevel.php'; ?></td>
<td id="i"><?php include 'ilevel.php'; ?></td>

Add JavaScript 添加JavaScript

<script type="text/javascript">
var llob = document.getElementById("llob").innerHTML;
var i = document.getElementById("i").innerHTML;
if( llob < i ){
    document.getElementById("llob").style.backgroundColor = "red";
}
</script>

Can't you just combine both of them into a single file? 您不能将它们都合并为一个文件吗?

<?PHP

$file_handle = fopen("stockexport.csv", "r");

while (!feof($file_handle) ) {

$line_of_text = fgetcsv($file_handle, 1024);
if(intval($line_of_text[3]) < intval($line_of_text[5])) { 
    print "<span class='highlight'>" . $line_of_text[3] . "</span><BR>";
    print $line_of_text[5] . "<BR>";
}
else { 
    print "<span class='highlight'>" . $line_of_text[5] . "</span><BR>";
    print $line_of_text[3] . "<BR>";
}

}

fclose($file_handle);

?>

Then add a class rule in the CSS file named hightlight with the background-color property. 然后在带有high-light属性的CSS文件中使用background-color属性添加一个类规则。

Note: This would work if the values are intergers. 注意:如果值是整数,这将起作用。

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

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