如何检查 while 循环中当前是否存在某个值?
假设我的数据库中有这个表...
id | name | beginventory | sold | datesold
101 | rock | 100 | 5 | 2011-9-12
201 | paper | 50 | 10 | 2011-9-13
301 | pen | 30 | 20 | 2011-10-1
101 | rock | 100 | 10 | 2011-10-1
101 | rock | 100 | 10 | 2011-10-2
201 | paper | 50 | 15 | 2011-10-3
我想显示...的输出...
name | current | sold | remaining | date sold
rock | 100 | 5 | 95 | 2011-9-12
paper | 50 | 10 | 40 | 2011-9-13
pen | 30 | 20 | 10 | 2011-10-1
rock | 95 | 10 | 85 | 2011-10-1
rock | 85 | 10 | 75 | 2011-10-2
paper | 40 | 15 | 25 | 2011-10-3
这里我的脚本(不起作用)
while($info = mysql_query($getinfo)){
$name = $info['name'].'<br />';
$beginventory = $info['beginventory'].'<br />';
$sold = $info['sold'].'<br />'
$remaining = $beginventory - $sold.'<br />';
$date = $info['datesold'].'<br />'
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$current.'</td>';
echo '<td>'.$sold.'</td>';
echo '<td>'.$remaining.'</td>';
echo '<td>'.$date.'</td>';
echo '</tr>';
}
我的问题是如何在 while 循环中“告诉”PHP 如果“名称" 已经存在,而不是使用 $beginventory 来获取 $remaining,而应该使用之前的 $remaining 来获取当前的正确值。
我知道如果我将值存储在数据库中会更容易,如果我无法弄清楚这一点,这是我的最后一个选择。
Let's say I have this table in my database...
id | name | beginventory | sold | datesold
101 | rock | 100 | 5 | 2011-9-12
201 | paper | 50 | 10 | 2011-9-13
301 | pen | 30 | 20 | 2011-10-1
101 | rock | 100 | 10 | 2011-10-1
101 | rock | 100 | 10 | 2011-10-2
201 | paper | 50 | 15 | 2011-10-3
I want to display an output of...
name | current | sold | remaining | date sold
rock | 100 | 5 | 95 | 2011-9-12
paper | 50 | 10 | 40 | 2011-9-13
pen | 30 | 20 | 10 | 2011-10-1
rock | 95 | 10 | 85 | 2011-10-1
rock | 85 | 10 | 75 | 2011-10-2
paper | 40 | 15 | 25 | 2011-10-3
Here my script (doesn't work)
while($info = mysql_query($getinfo)){
$name = $info['name'].'<br />';
$beginventory = $info['beginventory'].'<br />';
$sold = $info['sold'].'<br />'
$remaining = $beginventory - $sold.'<br />';
$date = $info['datesold'].'<br />'
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$current.'</td>';
echo '<td>'.$sold.'</td>';
echo '<td>'.$remaining.'</td>';
echo '<td>'.$date.'</td>';
echo '</tr>';
}
My question is how do I "tell" PHP in a while loop that if the "name" already exist, instead of using $beginventory to get $remaining it should use the previous $remaining to get the correct value for current.
I know it is easier if I stored the values in the database, this is my last option if I can't figure this out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)