2013-09-16 18 views
-2

Я хочу отправлять уведомления нескольким пользователям на моем сайте. Их идентификаторы находятся в массиве ($ userinput). $ userinput содержит идентификаторы членов, разделенные запятой в начале каждого значения (например, «7,1,2,3»).PHP MySQL Несколько строк из массива

Я хочу, чтобы выполнить запрос, как это:

$sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES ('$user_id_here', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') "; 

Как я смог бы отправить на все эти член?

+2

И что вы уже пробовали? –

ответ

1

Вы должны циклу массив и динамически строить запрос:

$sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES"; 
$parts = array(); 
foreach($userinput as $user) { 
    $parts[] = "('$user', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') "; 
} 
$sqlnot .= implode(',', $parts); 
1
$id_string = "7,1,2,3";  
    $user_ids = explode(",", $id_string); 

    foreach($user_ids as $user_id_here){ 
     $sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES ('$user_id_here', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') "; 
    // execute your query here 
    } 
0

Если вы хотите вставить несколько раз, то Еогеасп является вариантами очарования как:

$ids = explode(",", $userinput); 

foreach($ids as $user_id){ 
    $sqlnot = "INSERT INTO lb_notif (to_id, time_sent, from_id, subject, message) VALUES ('$user_id_here', now(), '$logOptions_id', '$title', 'You have been invited to this group chat: <a href=profilechat.php?id=$logOptions_id&chat=$act_item_id>Click here to enter</a>.') "; 

} 
Смежные вопросы