Nested looping within a table structure

I had a Users table and a group table. When I load the group table, it loads the users which have the same group_id as the group table id. It works, but my issue is that the foreach loop was quite messy. The output ends up looking like this...

However, I would like to format the output to look like this.

Here is my code.

 <tbody>
     <?php $i = 0; ?>
     @foreach ($auser as $res)
     <?php $i += 1; ?>
     <tr @if($i%2==0) class="bgcolor" @endif>
         <td>{{ $res->id }}.</td>
         <td id="showdet">{{ $res->nama }}</td>
         <td>{{ $res->description }}</td>
         <?php $user = DB::table('users')->where('devgroup',$res->id)->get();?>
         @foreach($user as $mem)
             <td>{{ $mem->name }}</td>
         @endforeach
         <td>
             <a class="default-btn" href="/pms/admin/group/edit/{{ $res->id }}">Edit</a>&nbsp;&nbsp;
             <a type="submit" class="default-btn del" data-id="devgroup" href="#"{{ $res->id }}">Delete</a>
         </td>
     </tr>
     @endforeach
 </tbody>

Can anyone help me figure out what I did wrong in the above code snippet? Thank you for your assistance.

Answer №1

For every member, a new TD should be created. The nested foreach loop should look like this:

<td>
    @foreach($user as $mem)
        {{ $mem->name }}<br>
    @endforeach
</td>

The outcome will be:

<td>
    Name 1<br>
    Name 2<br>
    Name 3<br>
</td>

If you are unsure about the template engine being used, include a loop condition within and avoid adding <br> after the last member.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How come this doesn't function properly in PHP: $a = explode(' ', $loadFunction)[0];

Is it possible to access an array without using another variable? For instance $loadFunction = "a b c d"; $a= explode(' ', $loadFunction)[0]; I am encountering a Parser error in PHP 5.3.3 Is there a workaround for this? ...

Guide on sending an AJAX request in jQuery to PHP while another request is still being processed

I am currently working on a feature that requires the use of HTML, jQuery, PHP, C++, and MySQL. The process involves making an AJAX call from PHP to C++ (via protocols) for server installation, which will take some time to complete. During this time, I ne ...

I'm encountering a "confirm" error within the data table. Any suggestions on how to resolve this issue?

When I try to use two datatables columns in confirm, an error occurs when the text 'do you want cancel?' is displayed. The issue seems to be with the text itself and not the code. How should we go about fixing this problem? This is my current cod ...

"Encountering an issue with CodeIgniter where the cart feature

Having just started with CI, I am currently working on developing an e-commerce website. However, I am facing challenges when it comes to adding products to the cart. Below, you can find the code for both the Jscript and controller that I have been using. ...

Error Encountered: AngularJS Form Email - SyntaxError: An unexpected token '<' was found in the code

My attempt to send an email from my AngularJS website involves the following setup: Contact.index.html: <form name="userForm" class="well form-search"> <input type="text" ng-model="name" class="input-medium search-query" placeholder="Name" ...

Making changes to HTML on a webpage using jQuery's AJAX with synchronous requests

Seeking assistance to resolve an issue, I am currently stuck and have invested a significant amount of time. The situation at hand involves submitting a form using the traditional post method. However, prior to submitting the form, I am utilizing the jQue ...

Looking for assistance with MySQL DISTINCT functionality

I need to extract unique names from a mysql table to use in an autocomplete feature with jQuery UI. However, there are duplicate names in the table and I want to avoid displaying them in the suggestions. Additionally, I also require selecting other colum ...

The most effective way to incorporate user accounts into URLs is by using mod_rewrite

I am currently working on a website that manages user accounts in the following manner: www.mysite.com/username www.mysite.com/username/user-action I have some understanding of mod_rewrite and I know that I could use something like this: RewriteRule ^( ...

Creating modular MVC components in Zend Framework is a key skill that developers need to master

I've been facing challenges in creating modular reusable components within my Zend Framework application. I'm not talking about Zend Framework modules, but rather the ability to have a reusable MVC widget. The issues I'm encountering may be ...

What's causing this error with jQuery and JSON?

When utilizing the following code snippet: - $.getJSON("admin.php?format=json", { module: "data", action: "allBusinessUnitsByClientName", clientname : $('#client').val() }, function(json) { $.each(json.items, function(i,item){ alert ...

What is the best way to choose every username from my list of members?

Looking for a way to select and display all the usernames from the members table in your MySQL database? Check out this code snippet: $stmt = $db->prepare("SELECT username FROM members"); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_A ...

generate a different identifier for ajax requests avoiding the use of JSON

The AJAX request functions in the following way: success: function(data) { $('#totalvotes1').text(data); } However, I need it to work with unique IDs as follows: success: function(data) { $('#total_' + $(this).attr("id")). t ...

Showing the values of selected checkboxes in a select dropdown - here's how!

Link to the jsfiddle : https://jsfiddle.net/a1gsgh11/9/ The JavaScript code seems to not be working on the js fiddle platform. My main concern is that I would like the selected checkbox values to display immediately without needing to submit any buttons. ...

Converting user's birthdate input from HTML to their age using PHP

I am facing an issue with retrieving the correct date from a date-time-picker named "dob" and passing it to PHP. When I try to post the date into the database user_age column, it only displays '2017'. However, if I manually set $dob to '10/0 ...

Error message: "PHP echo function has a syntax error"

I am struggling to properly display the URL. Currently, it is working but there is a quotation mark in front of the link. Strangely, the link still functions correctly. echo "<br>" . $result['text'] . "&nbsp;&nbsp;<em>(Contr ...

Having trouble with Laravel retrieving unique values from a MySQL column? Let's troubleshoot and

I'm attempting to retrieve unique values for a dropdown from a specific column, I've tested these queries in my controller, however, the results are coming back empty. Attempted Controller Query 1 public function create() { $categories = Ca ...

PHP is encountering difficulties when attempting to fetch extensive requests

I have developed a PHP service that retrieves JSON data from a table consisting of 2100 rows. My development setup includes: Xampp with Apache and MySQL Windows 10 PHP-7.0.8 I am using PDO in PHP to execute the query. The service used to function flawl ...

Find the total of the first two entries in a table using PHP SQL

Is there a way to calculate the total sum of the daily_rate_price field from only the first two rows in the daily_rates table? daily_rate_id daily_rate_date daily_rate_price daily_rate_reservation 1480 2021-11-18 280.00 320 1481 2021-11-19 280.00 ...

Insert multiple text box values as a new entry in an SQL database

Currently, I am implementing a code snippet to incorporate additional text boxes within a form. The main purpose is to enable users to input multiple languages they are proficient in. <script> jQuery(function($) { var i = 0; ...

Retrieve the substring following a specific keyword within an array of strings

I am working with a txt file that I have read as an array, where each line in the txt file is stored as an element in the array. My objective is to extract only the array elements that start with the word "Keywords:". I need to retrieve all the words follo ...