4

I want to show two foreach loop data dynamically inside one table, but the design of the table break after dynamic loop data. My blade.php code is given below:

<table>
  <tr>
     <th>Name</th>
     <th>Buy Rate</th>
     <th>Sell Rate</th>
  </tr>
  <tr>
   <?php foreach($datav as $data){ ?>
     <td><?php echo $data->name; ?></td>
     <td><?php echo $data->buy_rate; ?></td>
   <?php } ?>
  <?php foreach($sells as $sell){ ?>
     <td><?php echo $sell->rate; ?></td>
  </tr>
  <?php } ?>
</table>

My route is:

Route::get('/','WelcomeController@test');

My Controller Code:

public function test(){

$datav= DB::table('localcurrency')
              ->where('status',1)
              ->get();
$sells= DB::table('localcurrency2')
              ->where('status',1)
              ->distinct()
              ->get();

return view('home.home_content')
        ->with('datav',$datav)
        ->with('sells',$sells);

}

How can I run this?

9
  • 1
    Put the </tr> outside of the second foreach. Commented Mar 18, 2019 at 17:18
  • You're potentially going to have to merge the arrays together. Please can you show your route/controller code? Are $datav and $sells arrays or collections? Also, is this in a .blade.php file? Commented Mar 18, 2019 at 17:19
  • Are these datasets linked somehow? Commented Mar 18, 2019 at 17:41
  • @ross-wilson i share my route, controller and .blade page code. Commented Mar 18, 2019 at 17:48
  • why don't you use @foreach construction ? Commented Mar 18, 2019 at 17:49

2 Answers 2

3

It is better if you merge both $datav and $sells in one array.

In this above scenario firstly your $datav array will complete then it goes to second array.

So it will not create a complete table. Or the table alignment will be wrong.

Rest depends on what you proceed in the array.

public function test(){

$datav= DB::table('localcurrency')
          ->where('status',1)
          ->get();
$sells= DB::table('localcurrency2')
          ->where('status',1)
          ->distinct()
          ->get();
$sells=$sells->toArray();
$results=array();
foreach($datav as $key=>$data)
{
   $newarr=array();
   $newarr['name']=$data->name;
   $newarr['buy_rate']=$data->buy_rate;
   $newarr['sell_rate']=$sells[$key]->rate;
   $results[]=$newarr;
}
return view('home.home_content')
    ->with('results',$result);

}

Your views looks like this

<table>
   <tr>
    <th>Name</th>
    <th>Buy Rate</th>
    <th>Sell Rate</th>
   </tr>
   @foreach($results as $result)
     <tr>
         <td>{{$result['name']}}</td>
         <td>{{$result['buy_rate']}}</td>
         <td>{{$result['sell_rate']}}</td>
    </tr>
  @endforeach

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

3 Comments

can you please show me the sample code for two loop?
This is more of a comment then it is an answer. Show how you would approach the situation.
Now check my updated answer. @ner Earlier user have not mention the controller code
0
  • you better to use Blade template engine syntax @foreach($values as $value) {{$value}} @endforeach
  • you better understand what structure of output you want to get
  • your task is not resolved in this case because if you want to fill
    one table row by row by some data, this data should be interconnected one to one relationship, so you should do it inside of one cycle
  • you can't fill first every values of first two columns of table and then another column's values. Table should be filled row by row

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.