Foreach循环和数组构建

时间:2013-06-24 23:19:38

标签: php string

我有一个管理区域,他们可以一次删除多个用户。这是处理删除的代码的一部分。基本上它会遍历用户ID并删除每个标记为已检查的ID。

if ($_POST['doDelete'] == 'Delete') {
    if (!empty($_POST['u'])) {
        foreach ($_POST['u'] as $uid) {
            $id = escape($uid);
            $delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
            if (!$delete) {
                $msg->add('e', QUERY_ERROR);
                redirect('users.php');
                exit();
            }
        }
    }
    /* we need a way to iterate over users deleted */
    $msg = new Messages();
    $msg->add('s', QUERY_DELETE_SUCCESS);
    redirect('users.php');
    exit();
}

function get_user_name_from_id($user_id)
{
    if ($_SESSION['user_level'] == ADMIN_LEVEL) {
        $viewUserMod = 1;
    } else {
        $config = Nemesis::select("usr_view_cm", "config");
        $row_config = $config->fetch_assoc();
        $viewUserMod = $row_config['usr_view_cm'];
    }
    if (is_numeric($user_id) && $viewUserMod == 1) {
        $sql = Nemesis::select("full_name", "users", "id = {$user_id}");
        if ($sql->num_rows > 0) {
            $user_name = $sql->fetch_assoc();
            return $user_name['full_name'];
        } else {
            // user name cannot be matched with db, either error, or most likely user was deleted
            return 'User ' . $user_id;
        }
    } else {
        return $user_id;
    }
}

在哪里说QUERY_DELETE_SUCCESS我想输出类似“Deleted Bob,Jack,Tim” .etc的东西我有一个使用用户ID并获取其名字的函数。问题是迭代完成后。显然,这些用户不再存在在数据库中,我无法得到他们的名字。 有没有办法在循环中运行此函数,并构建字符串或数组。可以输出该消息来代替消息吗?

2 个答案:

答案 0 :(得分:1)

当您使用以下内容循环uid时,可以将名称添加到数组中:

$names = array();
if (!empty($_POST['u'])) {
    foreach ($_POST['u'] as $uid) {
        $names[] = get_user_name_from_id($uid);
        $id = escape($uid);
        $delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
        if (!$delete) {
            $msg->add('e', QUERY_ERROR);
            redirect('users.php');
            exit();
        }
    }
}

然后,当您想要输出确认消息时,可以将该数组转换为逗号分隔的字符串,如下所示:

$names = implode(', ',$names);
$message = "Deleted $names";

答案 1 :(得分:1)

你应该能够做到这一点:

if ($_POST['doDelete'] == 'Delete') {
    $deleted = array();
    if (!empty($_POST['u'])) {
        foreach ($_POST['u'] as $uid) {
            $id = escape($uid);
            $username = get_user_name_from_id($uid);
            $delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
            if (!$delete) {
                $msg->add('e', QUERY_ERROR);
                redirect('users.php');
                exit();
            }
            $deleted[] = $username // push name to array after deletion is successful
        }
    }
    /* The $deleted array now holds the names of the deleted users.
     * Do with it what you want.
     */
    $names = implode(",", $deleted)
    $msg = new Messages();
    $msg->add('s', QUERY_DELETE_SUCCESS . " Deleted: $names");
    redirect('users.php');
    exit();
}

这里可以进行一些改进,包括效率(将许多小的单个查询组合成几个较大的查询)和错误处理(不要重定向第一个错误 - 而是在所有处理完成到页面后重定向有一个成功和错误的列表),但这是基本的想法。

这是一个快速更改,可以执行所有操作,即使其中一个操作错误:

if ($_POST['doDelete'] == 'Delete') {
    $deleted = array();
    $errored = array();
    if (!empty($_POST['u'])) {
        foreach ($_POST['u'] as $uid) {
            $id = escape($uid);
            $username = get_user_name_from_id($uid);
            $delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
            if (!$delete) {
                $errored[] = $username;
            } else {
                $deleted[] = $username // push name to array after deletion is successful
            }
        }
    }
    /* The $deleted array now holds the names of the deleted users.
     * The $errored array now holds the names of users who were not deleted due to errors.
     * Do with them what you want.
     */
    $msg = new Messages();
    $names_deleted = implode(",", $deleted)
    $msg->add('s', QUERY_DELETE_SUCCESS . " Deleted: $names_deleted");
    if (count($errored) > 0) {
        $names_errored = implode(",", $errored)
        $msg->add('e', QUERY_ERROR . " Did not delete: $names_errored");
    }
    redirect('users.php');
    exit();
}