0

I'm getting a problem while I want to loop through an array and display it with Laravel blade engine

My controllers code is like this

    $table =  DB::table('tables')->select('id')->where('name', strtolower($name))->first();

    $columns =  Column::where('table_id', $table->id)->get();

    foreach ($columns as $col) {
        $data[] = $col->col_name;
    };

    $content = DB::table($name)->select(...$data)->get();


    return view('back.group.view-table', compact('content', 'columns', 'data'));

And my blade view code is like this

<table class="table table-striped">
            <thead>
                <tr>
                    @foreach($columns as $column)
                        <th>{{ $column->dis_name }}</th>
                    @endforeach
                </tr>
            </thead>
            <tbody>
                @foreach ($content as  $value)
                    <tr>
                        
                        @for ($i = 0; $i < count($columns); $i++)
                            <td>{{ $value->key = $data[$i] }}</td>
                        @endfor
                        
                    </tr>
                @endforeach
                
            </tbody>
        </table>

This is the result which I get with the following code:
This is the result which I get with the following code

The result I want to have:
The result I want to have

1
  • $content as $value then {{ put here your array index name }} Commented Mar 2, 2017 at 9:52

6 Answers 6

4

php has a function called array_keys();

so it looks something like this I think

$results = DB::table($name)->select(...$data)->get();
$content = $results->first();
$keys = array_keys($content->toArray());

this will get you the keys. If you need the keys from each result just loop over $results and for each $result get the keys, using the array_keys syntax.

Sign up to request clarification or add additional context in comments.

1 Comment

1

Another solution would be using array_flip() it will flip the keys and values around.

Comments

0

Change like this

<table class="table table-striped">
     <thead>
       <tr>
        @foreach($columns as $column)
          <th>{{ $column->dis_name }}</th>
        @endforeach
        </tr>
      </thead>
      <tbody>
      @foreach ($content as  $value)
      <tr>
         <td>{{ $value->name }}</td>
      </tr>
      @endforeach

     </tbody>
</table>

6 Comments

I know that and i alredy did it but i want the users to get automatically the columns wich are coming from a table selected by the user if u understand
here foreach loop already do same thing what you want ... once check this
what i mean is if the table user has columns id username email etc etc i want to be displayed dynamiclly because he will select another table after with different columns ex. products id name price etc etc thats why i want that to be dynamic
first check $content array value by print_r if loop have more than one value then loop automatically repeat printing tr with its value ...
Collection {#196 ▼ #items: array:2 [▼ 0 => {#194 ▼ +"onomateponimo": "Mukja Eluert" +"email": "[email protected]" +"kinito": "123456789" } 1 => {#190 ▼ +"onomateponimo": "Eluert " +"email": "[email protected]" +"kinito": "123456789" } ] }
|
0

use array_keys() to get all the keys from an array, you also can use foreach($arr as $key => $value)

Comments

0

I Found the answer i just change my view from this

<table class="table table-striped">
        <thead>
            <tr>
                @foreach($columns as $column)
                    <th>{{ $column->dis_name }}</th>
                @endforeach
            </tr>
        </thead>
        <tbody>
            @foreach ($content as  $value)
                <tr>

                    @for ($i = 0; $i < count($columns); $i++)
                        <td>{{ $value->key = $data[$i] }}</td>
                    @endfor

                </tr>
            @endforeach

        </tbody>
    </table>

To this

<table class="table table-striped">
            <thead>
                <tr>
                    @foreach($columns as $column)
                        <th>{{ $column->dis_name }}</th>
                    @endforeach
                </tr>
            </thead>
            <tbody>
                @foreach ($content as  $value)
                    <tr>

                        @foreach ($data as $key)
                            <td>{{ $value->$key }}</td>
                        @endforeach

                    </tr>
                @endforeach

            </tbody>
        </table>

Comments

-1
$hayStack = ['name' => 'John', 'age' => 30, 'sex' => 'm'];
$searchValue = 30;
$keyName = array_keys($hayStack, $searchValue)[0]; 

array_keys() will search all possible keys containing search value. Considering $hayStack to be an array, there won't be any duplicate key, so our result will be at index 0.
For search value is 30, result will be 'age'. Here array_keys is an inbuilt function https://www.php.net/manual/en/function.array-keys.php

1 Comment

Hey welcome to SO! Please explain your solution and ideally add references to the docs. I don't think the poster can understand it as you wrote it.

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.