Страницы

Поиск по вопросам

понедельник, 15 июля 2019 г.

Как сделать условие внутри массива?

Есть массив данных, которые потом циклом добавляет в базу, но быват что нет некоторых данных соответственно эту часть массива исключать.
if ($_GET['addvid'] == "leadadd") {
$json_output = array();
if (isset($_POST['name_lead'])) {$name = checkrequest(iconv("UTF-8", "WINDOWS-1251", $_POST['name_lead']));} if (isset($_POST['phone_lead'])) {$phone = $_POST['phone_lead'];} if (isset($_POST['email_lead'])) {$email = checkrequest($_POST['email_lead']);} if (isset($_POST['channel_lead'])) {$source = checkrequest($_POST['channel_lead']);} if (isset($_POST['status_lead'])) {$status = checkrequest($_POST['status_lead']);} if (isset($_POST['address_lead'])) {$address_lead = checkrequest(iconv("UTF-8", "WINDOWS-1251", $_POST['address_lead']));} if (isset($_POST['comment_lead'])) {$comment_lead = checkrequest(iconv("UTF-8", "WINDOWS-1251", $_POST['comment_lead']));} if (isset($_POST['group_lead'])) {$id_group = checkrequest($_POST['group_lead']);}
if (isset($_POST['name_lead']) or isset($_POST['phone']) or isset($_POST['email_lead'])) { /* то можно занести информацию */
$id_c = $myrow_cabinet['id']; $id_staff = $userdata['id_staff'];
$result_status = mysql_query("SELECT id FROM status WHERE id = '".$status."' and id_cabinet = '$id_c'"); $myrow_status = mysql_fetch_array($result_status);
$dates = date('Y-m-d H:i:s', time()); $add_vid = "hand"; $result = mysql_query("INSERT INTO lids (id_cabinet,id_staff,name,tel,id_type,email,source,comment,address,status,status_view,add_vid,datetime) VALUES ('$id_c','$id_staff','$name','$phone','$id_group','$email','$source','$comment_lead','$address_lead','$myrow_status[id]','1','$add_vid','$dates')"); $id_new_lead = mysql_insert_id();
if ($result == 'true') { $json_output = array('succes'=>1, 'url_new_lead'=>'/'.$id_c.'/leads/details-'.$id_new_lead.'', 'msg'=>'

Q
');
// Добавление названий форм $lead_add = array( array("vid_change" => "add", "coment_change" => "Добавлен клиент", "change_old" => "", "change_new" => ""), array("vid_change" => "name", "coment_change" => "", "change_old" => "", "change_new" => "$name"), array("vid_change" => "phone", "coment_change" => "", "change_old" => "", "change_new" => "$phone"), array("vid_change" => "email", "coment_change" => "", "change_old" => "", "change_new" => "$email"), array("vid_change" => "address", "coment_change" => "", "change_old" => "", "change_new" => "$address_lead"), array("vid_change" => "comment", "coment_change" => "$comment_lead", "change_old" => "", "change_new" => ""), array("vid_change" => "status", "coment_change" => "", "change_old" => "", "change_new" => "$myrow_status[id]"), array("vid_change" => "group", "coment_change" => "", "change_old" => "", "change_new" => "$id_group")
);
for ($i = 0; $i < count($lead_add); $i++) { $item = $lead_add[$i];
$date = date('Y-m-d H:i:s', time()); $vid_change = $lead_add[$i]['vid_change']; $coment_change = $lead_add[$i]['coment_change']; $change_old = $lead_add[$i]['change_old']; $change_new = $lead_add[$i]['change_new']; mysql_query("INSERT INTO change_history (id_lid,datetime,vid_change,coment_change,change_old,change_new,id_staff,id_cabinet) VALUES ('$id_new_lead','$date','$vid_change','$coment_change','$change_old','$change_new','$id_staff','$id_c')");
}
} else { $json_output = array("succes"=>0, "msg"=>"
N
Что-то не работает, сообщите в тех.поддержку!
"); }
}
else {$json_output = array("succes"=>0, "msg"=>"
N
Вы не заполнили обязательные поля!
");}
echo json_encode($json_output); exit; }
Т.е. например не был указан в форме емайл, значит array("vid_change" => "email", "coment_change" => "", "change_old" => "", "change_new" => "$email"), не участвует в формировании массива...
Как такое сделать?


Ответ

Элемента с "vid_change"=> "address" существовать не будет, т.к. не указана переменная $address.
$lead_add = array( array("vid_change" => "add", "coment_change" => "Добавлен клиент", "change_old" => "", "change_new" => ""), array("vid_change" => "name", "coment_change" => "", "change_old" => "", "change_new" => "$name"), array("vid_change" => "phone", "coment_change" => "", "change_old" => "", "change_new" => "$phone"), array("vid_change" => "email", "coment_change" => "", "change_old" => "", "change_new" => "$email"), array("vid_change" => "address", "coment_change" => "", "change_old" => "", "change_new" => "$address_lead"), array("vid_change" => "comment", "coment_change" => "$comment_lead", "change_old" => "", "change_new" => ""), array("vid_change" => "status", "coment_change" => "", "change_old" => "", "change_new" => "$myrow_status[id]"), array("vid_change" => "group", "coment_change" => "", "change_old" => "", "change_new" => "$id_group") );
for($i = 0; $i < count($lead_add); $i++) { $item = $lead_add[$i];
if(!isset($$item["vid_change"])) { unset($lead_add[$i]); } }
sort($lead_add);
print_r($lead_add); ?>

Комментариев нет:

Отправить комментарий