[英]comparing rows mysql, php
Is there a way to compare strings from two different tables in php? 有没有办法比较PHP中两个不同表的字符串? For example:
例如:
Clothing Item Price tag
Scarf £5
Scarf £6
Scarf £4
Cloth £10
Shoe £15
I basically want to group the different items by their names. 我基本上想按照他们的名字对不同的项目进行分组。 For each different item, I want to alternate between two colours.
对于每个不同的项目,我想在两种颜色之间交替。 So if the item is a scarf, I will colour it maybe blue then because the next item is also a scarf, I will colour it blue as well.
所以,如果这件物品是围巾,我会将它涂成蓝色,然后因为下一件也是围巾,我也将它涂成蓝色。 After that since I have a cloth, it will be coloured yellow and because the next one is a shoe, it will be coloured blue.
之后,因为我有一块布,它会变成黄色,因为下一个是鞋,它会变成蓝色。 I came up with something using php:
我想出了一些使用php的东西:
$previous = "";
while ($row = mysql_fetch_array($result))
{
if ( $row['firstName'] != $previous) {
echo "<tr bgcolor = 'blue'>";
}
else {
echo "<tr bgcolor = 'yellow'>";
}
blah blah
}
When I do this, I don't get what I want. 当我这样做时,我得不到我想要的东西。 What I would get from this is the first scarf is yellow, while the other two are blue.
我从这里得到的是第一条围巾是黄色的,而另外两条是蓝色的。 Then because scarf != cloth, I get a yellow and also because cloth != shoe, I get yellow instead of a blue.
然后因为围巾!=布,我得到一个黄色,也因为布!=鞋子,我得到黄色而不是蓝色。
I can see straight away what the problem is but I don't know how to fix it. 我可以直接看到问题是什么,但我不知道如何解决它。 Please help.
请帮忙。 Thanks
谢谢
Keep track of the current color as a separate variable starting outside the loop and then change it when the item's first name changes: 跟踪当前颜色作为从循环外部开始的单独变量,然后在项目的名字更改时更改它:
$color = true;
$previous = '';
while ($row = mysql_fetch_array($result))
{
if ( $row['firstName'] != $previous) {
$color = !$color;
$previous = $row['firstName'];
}
echo "<tr bgcolor = '", ($color?'blue':'yellow'),"'>";
blah blah
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.