0

I am able to display my data from db. I want to display in a table under tabs but then my data appears crooked. The rows are not straight. why is that happening to the table?

it is arranged kind of this way.

the updated code below doesn't provide the right format for the table Table

  id  name
     1    James
    4   Michel
   2  Jonathan  

HTML

<div class="row">
    <div class="col-md-7">

        <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                @foreach($items as $item)
                    <li><a href="#tab_{{ $item->id }}" data-toggle="tab" >{!!$item->name!!}</a></li>
                @endforeach             
            </ul>
            <div class="tab-content">
                @foreach($items as $item)
                    <div class="tab-pane active" id="tab_{{ $item->id }}">
                        @foreach($item->products as $product)
                            <table class="table">
                                <thead>
                                    <tr> 
                                        <th>No#</th>
                                        <th>Name</th>
                                        <th>Action</th>
                                    </tr>
                               </thead>
                               <tbody>
                                    <tr>
                                        <td>{{$product->id }}</td>
                                        <td>{{ $product->name}}</td>
                                    </tr>
                                </tbody>
                            </table>
                        @endforeach         
                    </div>    
                @endforeach         
            </div>
        </div>
    </div>

Updated Code

Update

<div class="row">
    <div class="col-md-7">

        <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                @foreach($items as $item)
                    <li><a href="#tab_{{ $item->id }}" data-toggle="tab" >{!!$item->name!!}</a></li>
                @endforeach             
            </ul>
            <div class="tab-content">
                @foreach($items as $item)
                    <div class="tab-pane active" id="tab_{{ $item->id }}">
                            <table class="table">
                                <thead>
                                    <tr> 
                                        <th>No#</th>
                                        <th>Name</th>
                                        <th>Action</th>
                                    </tr>
                               </thead>
                               <tbody>
                        @foreach($item->products as $product)

                                    <tr>
                                        <td>{{$product->id }}</td>
                                        <td>{{ $product->name}}</td>
                                    </tr>
                        @endforeach         

                                </tbody>
                            </table>
                    </div>    
                @endforeach         
            </div>
        </div>
    </div>
3
  • what's your final HTML output? Commented Nov 16, 2017 at 8:32
  • This ain't only HTML. The foreach is from a framework language. What framework do you use? And what Quentin says is the awnser Commented Nov 16, 2017 at 8:38
  • In your code 'th' number 3 but under tbody I can see only 2 'td', is this a cause? Commented Nov 16, 2017 at 8:41

2 Answers 2

1

The start of your foreach products is before the <table> start tag.

You are, however, trying to generate one row per product, so it should be immediately before the <tr> start tag.

The matching endforeach should likewise be moved to just after the </tr> end tag.

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

Comments

0

You need to move the @foreach() + corresponding @endforeach inside the <tbody>, otherwise you are outputting a new table for each product. This is how it should be:

<table class="table">
    <thead>
        <tr> 
            <th>No#</th>
            <th>Name</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
    @foreach($item->products as $product)
        <tr>
            <td>{{ $product->id   }}</td>
            <td>{{ $product->name }}</td>
        </tr>
    @endforeach      
    </tbody>
</table>

4 Comments

what about the @foreach() for the tab-content . this line ` <div class="tab-content"> @foreach($items as $item) <div class="tab-pane active" id="tab_{{ $item->id }}">`
do i have to maintain it?
You'll probably need to keep it as it generates the tabs, each of which contains a table of products.
Looks good to me, but it is up to you to decide if it does what you want.

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.