0

Ok, So I'm running to this problem i"m trying to split/ index the values in a column in my database. So i put the I've been trying to put it into a string, and all the combination I have tried should work, but everytime I try to split the value i keep getting this error Array to string conversion and the errors shows my database as appose to a string or even at the very least a column. Now sorry if my code at this point looks like more work than it should be haha. That was out of desperation and a attempt to get it to work.

this my controller:

public function FSC_List($id)
{

    $fsg_find_id = fsgdata::find($id);
    $fsc_list_all = fscdata::all();
    $fsc_find_id = fscdata::find($id);
    $num_match = fscdata::all()->toArray();
    $fsc_num_col = fscdata::lists('fsc_number');
    $newstring = implode("",$fsc_num_col);
    $final = $newstring.str_split(0,2); 

return View('FSC_views.FSC_List', compact('fsg_find_id','fsc_list_all',
        'fsc_find_id', 'num_match', 'fsc_num_col', 'newstring','final'));
}

This is my view:

@extends('layout.master')
@section('content')
<div class="figure"><img src="/Content/images/figure2.jpg" /></div>
<div class="main">
    <h2>Search Results for FSG Number: {{ $fsg_find_id->fsg_number }}</h2>
        <ul>

        @foreach($fsc_num_col as $fsc_find_id->fsg_number)
            @if($fsg_find_id->fsg_number == $final);
                <a href='/NSN_list={{ $fsc_find_id->id }}'>{{$fsc_find_id->fsc_number}}</a></br>
            @endif
        @endforeach
    </ul>
</div>
@endsection

Note my goal is to have 2 numbers chosen by the users to match up with the first 2 digits of a bunch of 4 digit numbers in a database and to grab all of those that meet that criteria. Thanks in advance to anyone who can help.

2 Answers 2

1

The immediate issue looks like this line:

$final = $newstring.str_split(0,2);

Basically, you are trying to combine a string $newstring with an array str_split(0, 2) which is why you are getting the Array to string conversion error. Take a look at the manual entry for str_split - it doesn't look like you understand it properly.

If you are trying to get the first two characters of a string then you should be using substr. Something like:

$full_string = 'abcdefg';
$first_two = substr($full_string, 0, 2);  # $first_two = 'ab'
Sign up to request clarification or add additional context in comments.

Comments

0

this is probably not the best practice, but this is how I went about it, if anyone knows how to index more than one character at a time, by all means share haha.

@foreach($fsc_list_all as $fsc_find_id)
    @if($fsg_find_id->fsg_number[0] == $fsc_find_id->fsc_number{0} &&
      $fsg_find_id->fsg_number[1] == $fsc_find_id->fsc_number{1})

          <a href='/NSN_list={{ $fsc_find_id->id }}'>{{$fsc_find_id->fsc_number}}</a></br>

     @endif
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.