在启用或禁用某些条件的情况下运行PHP代码
我在这里已经问过这个问题。 我对数据库做了一些修改。 以前给出的答案可能是正确的,我试过,但不起作用。 这些答案可能超出了我对PHP的理解 下面是同一问题的改写版本。 我要替换的实际代码在启用或禁用某些条件的情况下运行PHP代码,php,mysql,sql,arrays,for-loop,Php,Mysql,Sql,Arrays,For Loop,我在这里已经问过这个问题。 我对数据库做了一些修改。 以前给出的答案可能是正确的,我试过,但不起作用。 这些答案可能超出了我对PHP的理解 下面是同一问题的改写版本。 我要替换的实际代码 if ($condition1 >= $offer1 AND $condition2 >= $offer2 AND $condition3 >= $offer3 AND $condition4 >= $offer4 ) { //run code } el
if ($condition1 >= $offer1 AND $condition2 >= $offer2 AND
$condition3 >= $offer3 AND $condition4 >= $offer4 )
{ //run code }
else { //some error messages }
$condition1 and all are numeric value
$offer1 and all are numeric value
我想用新代码替换此代码。下面是示例
if ($offercurrent[0]>=$offerpoints[0] AND $offercurrent[1]>=$offerpoints[1] AND
$offercurrent[2]>=$offerpoints[2] AND $offercurrent[3]>=$offerpoints[3] )
{ //run code }
else { //some error messages }
$offerstatus = enabled or disabled
$offercurrent = numeric values
$offerpoints = numeric values
$offercurrent $offerpoints status
50 51 enabled
100 99 enabled
122 865 disable
NNN offern disable
数据库值
while($row = mysql_fetch_array($sql))
{
$offername[] = $row['name'];
$offercurrent[] = $row['current'];
$offerstatus[] = $row['status'];
$offerpoints[] = $row['points'];
}
问题:仅当offerstatus=enabled时,代码才应运行。一是符合这一条件。它应该检查所有要约和要约的发生情况。
我已尝试用此代码替换以前的代码
$check=false;
for ($i=0;$i<count($offerstatus);$i++) {
//echo "$offerstatus[$i]";
if ($offerstatus[$i]=="enabled") {
if($offercurrent[$i]>=$offerpoints[$i]) {$check=true;}
echo "$offername[$i]";}
}
echo "$check";
if ($check=true) { //run code }
else {//error messages}
$check=false;
对于($i=0;$i=$offerpoints[$i]){$check=true;}
回显“$offername[$i]”;}
}
回显“$check”;
如果($check=true){//运行代码}
else{//错误消息}
使用此代码检查Always返回1(true)
它只检查第一个值,而忽略其余值。通过在代码中添加else解决了此问题
$check=false;
for ($i=0;$i<=count($offerstatus);$i++) {
//echo "$offerstatus[$i]";
if ($offerstatus[$i]=="enabled") {
if($offercurrent[$i]>=$offerpoints[$i]) {$check=true;}
else{ $check=false;
break; //if $condition is less than $offer it will get out of loop.
}
}
}
if ($check=true) { //run code }
else {//error messages}
$check=false;
对于($i=0;$i=$offerpoints[$i]){$check=true;}
else{$check=false;
break;//如果$condition小于$offer,它将退出循环。
}
}
}
如果($check=true){//运行代码}
else{//错误消息}
您正在ifif($offercurrent[$i]>=$offerpoints[$i])中为$check赋值true。如果这是真的,那么检查将是真的。if($check=true)
需要是if($check==true)
或者if($check==true)
使用单个=
为变量赋值,而不是检查它。可能的重复