我有这个php函数,它检查数据库中的表并检查用户是否在字段中有特定的数字。此用户可能有几行,其中该字段具有该数字。
这个问题是它会多次运行if语句,触发生成的函数多次运行。我怎么能阻止这个,只是让函数说出"好的找到了al1和运行函数的匹配,转移到al2"
function countNewBadges() {
require "connect.php";
$count = mysqli_query($connection,"SELECT users.studentid, al1, al2, al3 FROM userbadges ub INNER JOIN users ON users.id = ub.user_id WHERE studentid = '".$_SESSION["studentid"]."'") or die(mysqli_error($connection));
while ($data = mysqli_fetch_array($count)) {
if ($data['al1'] == 1)
{
unlockedBadges();
}
else if ($data['al2'] == 1)
{
echo "No New Badges";
}
else if ($data['al3'] == 1)
{
echo "No New Badges";
}
}
}
答案 0 :(得分:1)
我怎么能让这个函数说“好的找到了al1和运行函数的匹配,转移到al2”
只需使用标志变量:
$flag = false;
while ($data = mysqli_fetch_array($count)) {
if ($data['al1'] == 1 && $flag === false)
{
unlockedBadges();
$flag = true;
}
else if ($data['al2'] == 1)
{
echo "No New Badges";
}
else if ($data['al3'] == 1)
{
echo "No New Badges";
}
}
答案 1 :(得分:0)
虽然user4035的回答会阻止多个数据条目真正只在你需要搜索后运行一个函数,然后按照标准使用(按照说法)行动。
function countNewBadges() {
require "connect.php";
$count = mysqli_query($connection,"SELECT users.studentid, al1, al2, al3 FROM userbadges ub INNER JOIN users ON users.id = ub.user_id WHERE studentid = '".$_SESSION["studentid"]."'") or die(mysqli_error($connection));
$foundit=false;
while ($data = mysqli_fetch_array($count)) {
if ($data['al1'] == 1)
{
$foundit=true;
}
else if ($data['al2'] == 1)
{
echo "No New Badges";
}
else if ($data['al3'] == 1)
{
echo "No New Badges";
}
}
if($foundit){
unlockedBadges();
}
}