Sunday, March 25, 2018

First foreach statement in if statement not initiating, skips straight to second one? - development

First foreach statement in if statement not initiating, skips straight to second one?

So I am having an issue. I have two if statements that request-> data from the form. However, when I have both checked, it is skipping over the first one and going to the second one. The foreach statements in the first one do not even initiate and I have no idea why.

Here is my code:

if ($request->location_verification == 'on') {
    $get_data = [];
    foreach (array_chunk($results, 50) as $chunk_result) {
        foreach ($chunk_result as $r) {

            $client_name = trim($r['first_name'] . ' ' . $r['last_name']);
            if ($client_name == '') {
                $check_name = ContactList::where('phone_number', $r['phone_number'])->first();
                if ($check_name) {
                    $client_name = trim($check_name->first_name . ' ' . $check_name->last_name);
                    if ($client_name == '') {
                        $client_name = 'no name';
                    }
                } else {
                    $client_name = 'no name';
                }
            }
            $client_name = urlencode($client_name);

            $url = url('user/get-location/' . Auth::guard('client')->user()->id . '/' . $r['phone_number'] . '/' . $client_name);
            $get_message = $message . "n" . $url;
            $r = array_merge($r, ['message' => $get_message]);
            array_push($get_data, $r);
        }
    }
} else {
    $get_data = $results;
}


if ($request->attachment_tracking == 'on') {
    $get_data = [];
    $lastMessage = MessageFiles::orderBy('message_id', 'desc')->first();
    if ($files = $request->file('files')) {
        if ($first == true) {
            if (count($lastMessage) >= 1) {
                $messageCount = $lastMessage->message_id + 1;
            } else {
                $messageCount = 1;
            }
            foreach ($files as $f) {
                $destinationPath = public_path() . '/assets/message_files/';
                $file_name = $f->getClientOriginalName();
                $extension = $f->getClientOriginalExtension();
// Available alpha caracters
                $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

// generate a pin based on 2 * 7 digits + a random character
                $pin = mt_rand(1000000, 9999999)
                    . mt_rand(1000000, 9999999)
                    . $characters[rand(0, strlen($characters) - 1)];

// shuffle the result
                $string = str_shuffle($pin);
                $f->move($destinationPath, $string . '.' . $extension);


                $tf = new MessageFiles();
                $tf->message_id = $messageCount;
                $tf->cl_id = Auth::guard('client')->user()->id;
                $tf->message = $get_message;
                $tf->file = $string . '.' . $extension;
                $tf->save();
                $first = false;
            }
        }
        foreach (array_chunk($results, 50) as $chunk_result) {
            foreach ($chunk_result as $r) {

                $client_name = trim($r['first_name'] . ' ' . $r['last_name']);
                if ($client_name == '') {
                    $check_name = ContactList::where('phone_number', $r['phone_number'])->first();
                    if ($check_name) {
                        $client_name = trim($check_name->first_name . ' ' . $check_name->last_name);
                        if ($client_name == '') {
                            $client_name = 'no name';
                        }
                    } else {
                        $client_name = 'no name';
                    }
                }
                $client_name = urlencode($client_name);

                $url = url('user/get-attachment/' . Auth::guard('client')->user()->id . '/' . $r['phone_number'] . '/' . $client_name);
                $get_message = $message . "n" . $url;
                $r = array_merge($r, ['message' => $get_message]);
                array_push($get_data, $r);
            }
        }
    } else {
        return redirect('user/sms/add-recurring-sms')->withInput(Input::all())->with([
            'message' => "No uploaded files found. Please upload a file to use attachment tracking.",
            'message_important' => true
        ]);
    }
}

Basically, location_verification is being run but the foreach statement is not.
Then when attachment_verification is run, the foreach (array_chunk runs perfectly but it doesn’t in location_verification.

I tried switching them around and the first foreach(array_chunk never runs. Could it be because of similar values?

I am super lost.

EDIT: array_push is not happening and the new message is not being updated for the first if statement.



from Laravel Questions and Answers https://laravelquestions.com/php/first-foreach-statement-in-if-statement-not-initiating-skips-straight-to-second-one/
via Lzo Media

No comments:

Post a Comment