Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shawdow's avatar

Running <div> tag with condition only once inside the foreach

hi,

I am trying to fetch the attribute1.attribute2,attribute3 values from the database make it to works dynamically

when i inspect the page source code the<div> tag runs inside the loop

how can make the <div> tag to run only once inside the loop

when i put the textbox inside the loop the textbix data does not gets read inside to make it work i need to make the tag to run only any solution pls share

@foreach($attr as $attrs)  

                 <div class="some" id="some_{{ $attrs->category_id }}" style="display:none;">

                        <strong>{{ $attrs->attribute1 }}<strong><br>


                        <strong>{{ $attrs->attribute2 }}<strong><br>

                            <strong>{{ $attrs->attribute3 }}<strong><br>

Thanks,

0 likes
42 replies
bobbybouwmann's avatar

So I'm not really sure what you mean here but I think you want the div object only once?

Anyway Laravel can help you do this kind of stuff!

@foreach ($attr as $attrs)
    @if ($loop->first)
            <div>Your div here</<div>
    @endif

    <strong>{{ $attrs->attribute1 }}</strong>

    <strong>{{ $attrs->attribute2 }}</strong>

    <strong>{{ $attrs->attribute3 }}</strong>

@endforeach 

Documentation: https://laravel.com/docs/5.5/blade#the-loop-variable

Shawdow's avatar

it does not work gives me error

Trying to get property of non-object

@for($i=0; $i<sizeof($attr); $i++)  

          @if($loop->first)

        <div class="some" id="some_{{ $attr[$i]->category_id }}" style="display:none;">  

    @endif   
                     <?php $j = $i+1; ?>                    
                    <strong>{{ $attr[$i]->{'attribute_'.$j} }}</strong><br>

        {!! Form::text('attributevalue_'.$j, null, array('placeholder' => '','class' => 'form-control')) !!}<br>
                             
                        @endfor    
Snapey's avatar

it only works if you foreach over an eloquent object. It does not work if you travel back to 2006 and use a simple counter

show what your data looks like and we might be able to help

1 like
zainudinnoori's avatar

Do like this :

@foreach ($attr as $attrs)
    @if ($loop->first)
       <div class="some" id="some_{{ $attrs->category_id }}" style="display:none;">
    @endif
    <div>
<strong>{{ $attrs->attribute1 }}<strong><br>
<strong>{{ $attrs->attribute2 }}<strong><br>
<strong>{{ $attrs->attribute3 }}<strong><br>
    </div>
    <p>This is user {{ $user->id }}</p>
@endforeach
Shawdow's avatar

@Snapey

I am trying put the table name numbers into arrays try to increment those values based on the size of table

In the attribute table it has list of columns like attribute_1,attribute_2,attribute_3 etc so in the strong tag i am trying fetch

also in the textbox i have attrvalue_1,attrvalue_2.. declared variable $j trying increment

so when i run the below code the <div> `` ` tag loops into into every time so i am trying to make the ``` run once hope it understands

reason i have used for loop because i can access through arrays instead of using foreach


@for($i=0; $i<sizeof($attr); $i++)  

 <div class="some" id="some_{{ $attr[$i]->category_id }}" style="display:none;">  

                     <?php $j = $i+1; ?>                    
                    
<strong>{{ $attr[$i]->{'attribute_'.$j} }}</strong><br>

{!! Form::text('attrvalue_'.$j, null, array('placeholder' => '','class' => 'form-control')) !!}<br>
                             
     @endfor     

Snapey's avatar

show what your data looks like and we might be able to help

Shawdow's avatar

@Snapey this how the attributes table looks like

{#817
         +"id": 2,
         +"attribute_1": "Size",
         +"attribute_2": "weight",
         +"attribute_3": "Capacity",
         +"attribute_4": "Some",
         +"attribute_5": "Some 1",
         +"attribute_6": "Some 2",
         +"attribute_7": "Some 3",
         +"category_id": 5,
         +"created_at": "2018-01-20 11:11:02",
         +"updated_at": "2018-01-20 11:11:02",
       },

Snapey's avatar

ok, so you just want to iterate over the columns?

and some of those columns might be empty?

What does the attributes table relate to?

Shawdow's avatar

@Snapey

it relates to the categories table if the user selects the categories based on the matched category_id column i need to display the list of attributes which is present in the attributes table

Snapey's avatar

in your original question what did foreach($attr as $attrs) do? Why is there more than one set of attributes for a category?

Shawdow's avatar

@Snapey $attr is the variable that i have accessed from the controller the query as like below

$attr = DB::table('attributes')->select('*')->get();

Shawdow's avatar

this is what the solution that works for me instead repeating the numbers i am trying to access through arrray using forloop in the blade file

but this not the good practise to write the blade code as below i think!!!

@foreach($attr as $attrs)  

                <div class="some" id="some_{{ $attrs->category_id }}" style="display:none;">

                        <strong>{{ $attrs->attribute_1 }}<strong><br>
  {!! Form::text('attvalue_1', null, array('placeholder' => '','class' => 'form-control')) !!}<br>

                        <strong>{{ $attrs->attribute_2 }}<strong><br>
  {!! Form::text('attrvalue_2', null, array('placeholder' => '','class' => 'form-control')) !!}<br>


    ... continued till 10

Snapey's avatar

So you are not trying to show the attributes of a specific category?

You just want to dump the list of category attributes. As there is only 7 I would probably just list them. What was wrong with your first attempt?

Show your full loop including the form elements

Shawdow's avatar

@Snapey this solution works perfectly but trying access through using for loop but its not working


@foreach($attr as $attrs)  

                <div class="some" id="some_{{ $attrs->category_id }}" style="display:none;">

                        <strong>{{ $attrs->attribute_1 }}<strong><br>
  {!! Form::text('attvalue_1', null, array('placeholder' => '','class' => 'form-control')) !!}<br>

                        <strong>{{ $attrs->attribute_2 }}<strong><br>
  {!! Form::text('attrvalue_2', null, array('placeholder' => '','class' => 'form-control')) !!}<br>


    ... continued till 10 both attrvalue_1 and attribute_1

Snapey's avatar

Well if you want good practice, I would get rid of Form library for a start.

If you want to not list out 7 (or you say 10) attributes you can either, juggle strings or create a list of attributes

I don't like this but here you go

for ($i=1,$i<10, $i++) {
                        <strong>{{ $attrs->{'attribute_'.$i} }}<strong><br>
  {!! Form::text({'attvalue_'.$i}, null, array('placeholder' => '','class' => 'form-control')) !!}<br>

}

Shawdow's avatar

@Snapey this solution holds good but the actual implementation is using of <div> tag inside the loop make the <div> tag loop through only once inside loop. using the <div> tag so that i can fetch the attributes by comparing the category_id column.

Snapey's avatar

Cannot understand your comment. If you want to include some html use ``` on its own line before the code and again afterwards

Snapey's avatar

You need to show the full code. Why not just move the div to where you need it?

You have one row of attributes inside the foreach loop.

Your div can be outside the foreach, can be before the attribute for loop or it can be inside the for loop

Shawdow's avatar

@Snapey one thing i forgot to tell you sorry using the <div> tag i have applied javascript to show the list attributes as soon as the Subcategory drop down is selected so i need the tag inside the loop

below is code sir

public function addProducts(){
        
     $subcat =  DB::table('categories')->select('*')->where('parent_id','!=',NULL)->get();

     $attr = DB::table('attributes')->select('*')->get();

          return view ('addproduct',compact('subcat','attr'));

}

blade template

<div class="col-xs-12 col-sm-12 col-md-12">

        <div class="col-xs-6 col-sm-6 col-md-6">
 
                <div class="form-group">

                    <strong>Select SubCategory:</strong>
                  
                      <select required class="selectpicker" name ="parent_id" id="parent_id">
                     
                 <option value="" ></option>
                    @foreach ( $subcat  as  $subcats)
                                 <option  value="{{ $subcats->parent_id }}"> {{ $subcats->name }} 
                                     </option>
                    @endforeach
               </select>
                
    
            
    @foreach($attr as $attrs)  

                <div class="some" id="some_{{ $attrs->category_id }}" style="display:none;">

                        <strong>{{ $attrs->attribute_1 }}<strong><br>
  {!! Form::text('attvalue_1', null, array('placeholder' => '','class' => 'form-control')) !!}<br>

                        <strong>{{ $attrs->attribute_2 }}<strong><br>
  {!! Form::text('attrvalue_2', null, array('placeholder' => '','class' => 'form-control')) !!}<br>

@endforeach                                          
            </div>    

                                       
            </div>                                            
            </div>     
                                      
        
    </div>
   </div>

   <script>

        $('#parent_id').on('change',function(){
            $(".some").hide();
            var some = $(this).find('option:selected').val();
            $("#some_" + some).show();
        });

      </script>

Snapey's avatar

Clean up the formatting of your code and you will be able to see what you are doing more clearly

I think you just need to put the closing div inside the foreach loop also

@foreach($attr as $attrs)  

  <div class="some" id="some_{{ $attrs->category_id }}" style="display:none;">

    <strong>{{ $attrs->attribute_1 }}<strong><br>
    {!! Form::text('attvalue_1', null, array('placeholder' => '','class' => 'form-control')) !!}
    <br>
        
    <strong>{{ $attrs->attribute_2 }}<strong><br>
    {!! Form::text('attrvalue_2', null, array('placeholder' => '','class' => 'form-control')) !!}
    <br>
        
  </div>

@endforeach 
Shawdow's avatar

@Snapey that is where Problem lies with closing <div> tag inside the foreach loop the loop runs with <div also. so i am trying run the <div> tag inside the foreach loop only once that's not possible in my written solution sir.

Snapey's avatar

You are not making sense.

You say you want the div to carry the category_id.

The category_id changes every loop of the foreach($attr as $attrs)

I thought you wanted a div around all the attributes of one category so that you could display it when a user changed the contents of a select box?

Shawdow's avatar

@Snapey yeah you are exactly right sir!! I want <div> tag around all the attributes of one category so that i would display the attributes as user changes the select box .i.e category

But inside the <div> if i use the text box field for every attributes. data gets Null when i print in the console that's what the problem i am facing when i use the text box inside the for each loop.

Snapey's avatar

Can't possibly guess.

Can you display your page, view the html source in the browser and paste it here so we see the final html

Shawdow's avatar

@Snapey

<div class="col-xs-12 col-sm-12 col-md-12">

        <div class="col-xs-6 col-sm-6 col-md-6">
 
                <div class="form-group">

                    <strong>Select SubCategory:</strong>
                  
                      <select required class="selectpicker" name ="parent_id" id="parent_id">
                     
                 <option value="" ></option>
                                                     <option  value="1"> Smart Phone 
                                     </option>
                                                     <option  value="1"> Tablets 
                                     </option>
                                                     <option  value="5"> Refrigirator 
                                     </option>
                                   </select>
        

                <div class="some" id="some_1" style="display:none;">  
               
                    <strong>Color</strong><br>

        <input placeholder="" class="form-control" name="attrvalue_1" type="text"><br>

                 </div>
                    

                <div class="some" id="some_5" style="display:none;">  
               
                                        
                    <strong>Color</strong><br>

        <input placeholder="" class="form-control" name="attrvalue_2" type="text"><br>

                 </div>
                          
            </div>                             
            </div>  
Snapey's avatar

ok so you have a div per attribute. Your code is not as like this;

@foreach($attr as $attrs)  

  <div class="some" id="some_{{ $attrs->category_id }}" style="display:none;">

    <strong>{{ $attrs->attribute_1 }}<strong><br>
    {!! Form::text('attvalue_1', null, array('placeholder' => '','class' => 'form-control')) !!}
    <br>
        
    <strong>{{ $attrs->attribute_2 }}<strong><br>
    {!! Form::text('attrvalue_2', null, array('placeholder' => '','class' => 'form-control')) !!}
    <br>
        
  </div>

@endforeach 

You will need to show the code you are currently using.

Next

Please or to participate in this conversation.