mandaryneks mandaryneks - 3 months ago 8
PHP Question

While loop and if - execute only one time

I have following code:

while($row = mysql_fetch_assoc($result))
{
if(strtolower($message) == $row['question'])
{
msg($row['answer']);
update($row['question']);
} else {
dodaj(strtolower($message), '', 0);
}
}


and I would like to execute function dodaj(); only one time, not a few times. How to do it? Please help, thank you very much.
Greetings.

Answer

Use a variable to remember if you called dodaj.

$called_dodaj = false;
while($row = mysql_fetch_assoc($result))
{
    if(strtolower($message) == $row['question'])
    {
        msg($row['answer']);
        update($row['question']);
    } elseif (!$called_dodaj) {
        dodaj(strtolower($message), '', 0);
        $called_dodaj = true;
    }
}