}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    $base_instance->query("UPDATE {$base_instance->entity['KNOWLEDGE']['FLASHCARDS']} SET value={$new_value},shown=shown+1,last_shown={$now},word_loop=0 WHERE word_id='{$knowledge_id}' AND user='******' AND category_id=0");
    $base_instance->query("UPDATE {$base_instance->entity['KNOWLEDGE']['FLASHCARDS']} SET word_loop=1 WHERE word_loop=0 AND user={$userid} AND category_id=0 AND value > -1 AND value < {$now} ORDER BY value LIMIT 1");
    # find a new word from active words that is due and add to loop
    $aff_rows = mysqli_affected_rows($base_instance->db_link);
    if ($aff_rows == 0) {
        increase_loop_words(1);
    }
    # if nothing found add a new word to active words and add to loop
    echo '<head>', _CSS_NAV, '<meta http-equiv="refresh" content="10;url=status.php"></head><font size="2">Removed from Loop</font>';
}
#
function increase_loop_words($how_many)
{
    global $base_instance, $html_instance;
    $how_many--;
    $userid = $base_instance->user;
    $added_new_words = 0;
    $sql = 'SELECT ID FROM ' . $base_instance->entity['KNOWLEDGE']['MAIN'] . ' WHERE user='******' ORDER BY RAND()';
    $data = $base_instance->get_data($sql);
    for ($index = 1; $index <= sizeof($data); $index++) {
        $ID = $data[$index]->ID;
    $diff = $total_loop_words - $loop_words;
    $base_instance->query("UPDATE {$base_instance->entity['KNOWLEDGE']['FLASHCARDS']} SET word_loop=0 WHERE word_loop=1 AND user='******' AND category_id='{$category_id}' ORDER BY shown DESC LIMIT {$diff}");
}
if (empty($go_back)) {
    $data = $base_instance->get_data("SELECT * FROM {$base_instance->entity['KNOWLEDGE']['FLASHCARDS']} WHERE user={$userid} AND category_id={$category_id} AND value > -1 AND value < {$now} AND word_loop=1 ORDER BY last_shown LIMIT 1");
    # get next word in loop to show it
    #echo 'hereee<br>';
    if (!$data) {
        # no words in the loop
        #echo 'here 2 <br>';
        $base_instance->query("UPDATE {$base_instance->entity['KNOWLEDGE']['FLASHCARDS']} SET word_loop=1 WHERE user={$userid} AND category_id={$category_id} AND value > -1 AND value < {$now} ORDER BY value LIMIT {$loop_words}");
        $aff_rows = mysqli_affected_rows($base_instance->db_link);
        if ($aff_rows < $loop_words) {
            # not found enough active words for loop
            $diff = $loop_words - $aff_rows;
            increase_loop_words($diff);
        }
        $data = $base_instance->get_data("SELECT * FROM {$base_instance->entity['KNOWLEDGE']['FLASHCARDS']} WHERE user={$userid} AND category_id={$category_id} AND value > -1 AND word_loop=1 ORDER BY last_shown LIMIT 1");
        # get next word in loop to show it
    }
} else {
    # user has clicked "go back"
    $data = $base_instance->get_data("SELECT * FROM {$base_instance->entity['KNOWLEDGE']['FLASHCARDS']} WHERE user={$userid} AND category_id={$category_id} AND word_id={$go_back}");
}
# chosen word which will be shown to user
if (!empty($data)) {
    $word_id = $data[1]->word_id;
    $shown = $data[1]->shown;
} else {
    $base_instance->show_message('Finished', 'No knowledge saved or no knowledge in the queue, check again later.');
    exit;