Button and Table dropdown shows duplicate values in Laravel 8 - php

I want to show no duplicates of the dropdown, I already set distinct in the controller query builder. Here's the blade view for this dropdown:
<div class="btn-group">
<button class="ml-2.5 mt-2 btn btn-primary " type="button">
Notifications: {{ $notifications }}
</button>
<button type="button" class="mt-2 btn btn-lg btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
#foreach ($getmahasiswa as $requests)
<li><a class="dropdown-item" href={{ url('/dashboard/nameasrequest',$requests->id_mhs) }}>{{ $requests->name }} | {{ $requests->id_mhs }}</a></li>
#endforeach
</ul>
</div>
This dropdown table automatically toggled all the data that have the same name, i want it to open normally one by one. Here's the blade view for the dropdown table:
<div class="container">
<table class="table table-condensed" id="myTable">
<thead>
<tr>
<th>Mata Kuliah</th>
</tr>
</thead>
#foreach($getmahasiswa as $collapse)
<tbody class="panel">
<tr data-bs-toggle="collapse" data-bs-target="#demo{{$collapse->id_mhs}}" data-bs-parent="#myTable">
<strong>
<td>{{ $collapse->mk }}</td>
</strong>
</tr>
<tr id="demo{{$collapse->id_mhs}}" class="collapse">
<td colspan="6" class="hiddenRow">
<div>
<strong>{{$collapse->name}}</strong>
</div>
</td>
<td class="align-self-end">
<div>
<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal{{$collapse->id_mhs}}" class="mr-3 text-sm bg-blue-500 hover:bg-green-700 text-white py-1 px-2 rounded focus:outline-none focus:shadow-outline">
<a href=# >APPLY ALL</a>
</button>
</div>
</td>
<div class="modal fade" id="exampleModal{{$collapse->id_mhs}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Mata Kuliah</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
#foreach($getmahasiswa as $collapses)
{{$collapses->mk}}
<br>
#endforeach
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</tr>
</tbody>
#endforeach
</table>
</div>
and here's the controller to fetch the data for both of views
$lr = DB::table('lr2')->get();
$notifications = DB::table('lr2')->distinct()->count('id_mhs');
$getmahasiswa = DB::table('lr2')->distinct()->select('lr2.id_mhs', 'users.name', 'lr2.mk')
->join('users', 'lr2.id_mhs', '=', 'users.id')->get();
return view('dashboard', compact('lr', 'notifications', 'getmahasiswa'));

Related

Delete button not work bootstrap modal and Laravel

I created one table that list items, and I added two buttons(edit and delete) so, the button edit is working, but the button delete is not working, and I'm making me carzy because I don't find the error. this is the table:
<table id="datatable" class="table align-items-center table-flush">
<thead class="thead-light">
<tr>
<th>text</th>
<th class="disabled-sorting" width="10%"></th>
<th class="disabled-sorting" width="10%"></th>
</tr>
</thead>
<tbody>
#foreach($Items as $item)
<tr>
<td>{{ $item->text }}</td>
<td><a href="{{ route('ItemsController.edit', $item->id) }}" class="btn btn-primary btn-fab btn-icon btn-round" title="Edit">
<i class="fa fa-edit"></i></a>
</td>
<td>
<a href="#" class="btn btn-primary btn-fab btn-icon btn-round btn-delete" title="Delete" data-id="{{ $item->id }}" data-toggle="modal" data-target="#modal-default" data-route="{{ route('ItemsController.destroy', $item->id) }}" data-title="{{ $item->id }}">
<i class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
</table>
the modal is:
<div class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Delete?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<form class="" action="" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger" name="button">yes, delete</button>
</form>
</div>
</div>
</div>
</div>
javascript data:
<script>
$(document).on('click', '.btn-delete', function(){
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
})
</script>
and controller function destroy
public function destroy(Items $item) {
$item->delete();
return redirect()->route('items.index')->with('success', 'Deleted Success');
}
I checked it line by line, and I don't know what I'm wrong, please help me
Many times thanks for getting into this.
You need to trigger your modal from JS.
If it is bootstrap 4 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
$("#modal-default").modal();
If it is bootstrap 5 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
modal = new bootstrap.Modal(document.getElementById("modal-default"));
modal.show();

Modal resets the counter back to 1

Not sure how did it reset/s itself even though it's inside the foreach loop. Here's a snippet from of my blade view:
<tbody>
<?php $counter = 1; ?>
#foreach ($guidelines as $guideline)
<tr>
<td class="text-center">{{ $counter }}</td>
<td>{{ $guideline->description }}</td>
<td>
<i class="far fa-edit"></i>
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal"><i class="far fa-trash-alt"></i></button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Confirm Delete</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this?</p>
</div>
<div class="modal-footer">
Confirm
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</td>
<?php $counter++; ?>
</tr>
#endforeach
</tbody>
Edit works with /general-guidelines/1/edit, /general-guidelines/2/edit, /general-guidelines/5/edit, etc. But /delete would always end up at /1/delete
First, because you have only one modal (#myModal), and your button always reference to the(#myModal), so that it will always open up the first modal. Try the solution #myModal{{ $loop->index }}.
Secondly, You can use $loop->iteration instead of $counter in the foreach loop.
Try this.
<tbody>
#foreach ($guidelines as $guideline)
<tr>
<td class="text-center">{{ $counter }}</td>
<td>{{ $guideline->description }}</td>
<td>
<i class="far fa-edit"></i>
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal{{ $loop->index }}"><i class="far fa-trash-alt"></i></button>
<div class="modal fade" id="myModal{{ $loop->index }}" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Confirm Delete</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this?</p>
</div>
<div class="modal-footer">
Confirm
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</td>
</tr>
#endforeach
with for loop, multiple modals are created but for all the modals generated with for loop, you can not use same id 'myModal' . id should be unique.

cannot delete last data on table using laravel destroy function

I create a laravel destroy function and i use a modal to delete data. but i cannot delete all data in my database. last one cannot be deleted. i think it cause in view.
this is my modal popup in view
<div class="modal fade" id="delete-form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Account</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="delete-form" action="{{ route('account.destroy', [$account->id]) }}" method="post">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
<p>Are you sure you want to delete this data? </p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
And my destroy controller
public function destroy(Account $account)
{
$findAccount = Account::find($account->id);
if($findAccount->delete()){
return redirect()->route('account.index')->with('success', 'Account details delete successfully!');
}
return back()->withInput()->with('error', 'Account details could not be deleted.');
}
i cannot delete all data in table. they keep one data and it cannot be deleted.
Try this:
#extends('template.app')
#section('content')
<div class="col-md-9">
<!-- table content -->
<div class="card">
<div class="card-header main-color-bg">
<h3 class="card-title">Account Details</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<input class="form-control rounded-corner" type="text" placeholder="Search Member Here" style="margin-bottom: 20px;">
</div>
</div>
<!-- success message -->
#include('inc.message')
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Account Number</th>
<th scope="col">Type</th>
<th scope="col">Amount</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
#if(is_empty($accounts))
<tr><td>NO DATA</td></tr>
#else
#foreach($accounts as $account)
<tr>
<th scope="row">{{$account->id}}</th>
<td>{{$account->acc_no}}</td>
<td>{{$account->acc_type}}</td>
<td>{{$account->amount}}</td>
<td><a class="btn btn btn-secondary" href="{{route('account.edit', $account->id)}}"><span class="fa fa-pencil"></span> Edit</a> <a class="btn btn btn-danger"
data-toggle="modal" data-target="#delete-form"><span class="fa fa-trash-o"></span> Delete</a></td>
</tr>
#endforeach
#endif
</tbody>
</table>
<!-- pagination -->
<nav id="pagination">
<ul class="pagination justify-content-center">
<li class="page-item disabled">
<span class="page-link"><span class="fa fa-arrow-circle-left"></span></span>
</li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item active">
<span class="page-link">
2
<span class="sr-only">(current)</span>
</span>
</li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item">
<a class="page-link" href="#"><span class="fa fa-arrow-circle-right"></span></a>
</li>
</ul>
</nav>
<!-- end pagination -->
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal"><span class="fa fa-plus"></span> Add Account</button>
</div>
</div>
<!-- end table content -->
</div>
<!-- delete modal -->
<!-- Modal -->
<div class="modal fade" id="delete-form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Account</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="delete-form" action="{{ route('account.destroy', [$account->id]) }}" method="delete">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
<p>Are you sure you want to delete this data? </p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- end delete modal-->
<!-- modal popup -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" style="background-color: #309fe2; color: #fff;">
<h5 class="modal-title" id="exampleModalLabel">Account Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{!! Form::open(['url' => '/account', 'id' => 'eventForm', 'data-toggle' => 'validator', 'role' => 'form']) !!}
{{ csrf_field() }}
<div class="modal-body">
<!-- modal form -->
<div class="form-group">
<label for="acc_no">Account Number</label>
<input type="number" class="form-control" name="acc_no" id="acc_no" maxlength="20" placeholder="Enter your account number" required>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" maxlength="30" name="name" id="name" placeholder="Enter your name" required>
</div>
<div class="form-group">
<label for="id_no">Identity No</label>
<input type="number" class="form-control" maxlength="9" name="id_no" id="id_no" placeholder="Enter your identity no" required>
</div>
<div class="form-group">
<label for="bank_id">Bank</label>
<select class="form-control" name="bank_id">
#if(!empty($banks))
#foreach($banks as $bank)
<option value="{{ $bank->id }}">{{ $bank->name }}</option>
#endforeach
#endif
</select>
</div>
<div class="form-group">
<label for="acc_type">Account Type</label>
<select class="form-control" id="acc_type" name="acc_type">
<option value="Saving">Saving</option>
<option value="Current">Current</option>
<option value="Deposite">Deposite</option>
</select>
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="text" pattern="[0-9.]" class="form-control" maxlength="15" name="amount" id="amount" placeholder="Enter your amount" required>
</div>
<!-- end modal form -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><span class="fa fa-times-circle"></span> Close</button>
<button type="submit" class="btn btn-primary pull-right"><span class="fa fa-money"></span> Save changes</button>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
<!-- end modal -->
#endsection
On your delete form change method from POST to DELETE.
I think may be Account::find($account->id); did not get the right Account object. use findOrFail to have another shot:
public function destroy(Account $account)
{
$findAccount = Account::findOrFail($account->id);
...
}
this is my table view. where i put if statement
#foreach($accounts as $account)
<tr>
<th scope="row">{{$account->id}}</th>
<td>{{$account->acc_no}}</td>
<td>{{$account->acc_type}}</td>
<td>{{$account->amount}}</td>
<td><a class="btn btn btn-secondary" href="{{route('account.edit', $account->id)}}"><span class="fa fa-pencil"></span> Edit</a> <a class="btn btn btn-danger"
data-toggle="modal" data-target="#delete-form"><span class="fa fa-trash-o"></span> Delete</a></td>
</tr>
#endforeach
#extends('template.app')
#section('content')
<div class="col-md-9">
<!-- table content -->
<div class="card">
<div class="card-header main-color-bg">
<h3 class="card-title">Account Details</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<input class="form-control rounded-corner" type="text" placeholder="Search Member Here" style="margin-bottom: 20px;">
</div>
</div>
<!-- success message -->
#include('inc.message')
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Account Number</th>
<th scope="col">Type</th>
<th scope="col">Amount</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
#if(!empty($accounts))
#foreach($accounts as $account)
<tr>
<th scope="row">{{$account->id}}</th>
<td>{{$account->acc_no}}</td>
<td>{{$account->acc_type}}</td>
<td>{{$account->amount}}</td>
<td><a class="btn btn btn-secondary" href="{{route('account.edit', $account->id)}}"><span class="fa fa-pencil"></span> Edit</a> <a class="btn btn btn-danger"
data-toggle="modal" data-target="#delete-form"><span class="fa fa-trash-o"></span> Delete</a></td>
</tr>
#endforeach
#endif
</tbody>
</table>
<!-- pagination -->
<nav id="pagination">
<ul class="pagination justify-content-center">
<li class="page-item disabled">
<span class="page-link"><span class="fa fa-arrow-circle-left"></span></span>
</li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item active">
<span class="page-link">
2
<span class="sr-only">(current)</span>
</span>
</li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item">
<a class="page-link" href="#"><span class="fa fa-arrow-circle-right"></span></a>
</li>
</ul>
</nav>
<!-- end pagination -->
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal"><span class="fa fa-plus"></span> Add Account</button>
</div>
</div>
<!-- end table content -->
</div>
<!-- delete modal -->
<!-- Modal -->
<div class="modal fade" id="delete-form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Account</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="delete-form" action="{{ route('account.destroy', [$account->id]) }}" method="delete">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
<p>Are you sure you want to delete this data? </p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- end delete modal-->
<!-- modal popup -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" style="background-color: #309fe2; color: #fff;">
<h5 class="modal-title" id="exampleModalLabel">Account Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{!! Form::open(['url' => '/account', 'id' => 'eventForm', 'data-toggle' => 'validator', 'role' => 'form']) !!}
{{ csrf_field() }}
<div class="modal-body">
<!-- modal form -->
<div class="form-group">
<label for="acc_no">Account Number</label>
<input type="number" class="form-control" name="acc_no" id="acc_no" maxlength="20" placeholder="Enter your account number" required>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" maxlength="30" name="name" id="name" placeholder="Enter your name" required>
</div>
<div class="form-group">
<label for="id_no">Identity No</label>
<input type="number" class="form-control" maxlength="9" name="id_no" id="id_no" placeholder="Enter your identity no" required>
</div>
<div class="form-group">
<label for="bank_id">Bank</label>
<select class="form-control" name="bank_id">
#if(!empty($banks))
#foreach($banks as $bank)
<option value="{{ $bank->id }}">{{ $bank->name }}</option>
#endforeach
#endif
</select>
</div>
<div class="form-group">
<label for="acc_type">Account Type</label>
<select class="form-control" id="acc_type" name="acc_type">
<option value="Saving">Saving</option>
<option value="Current">Current</option>
<option value="Deposite">Deposite</option>
</select>
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="text" pattern="[0-9.]" class="form-control" maxlength="15" name="amount" id="amount" placeholder="Enter your amount" required>
</div>
<!-- end modal form -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><span class="fa fa-times-circle"></span> Close</button>
<button type="submit" class="btn btn-primary pull-right"><span class="fa fa-money"></span> Save changes</button>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
<!-- end modal -->
#endsection

Pass data to modal in the same page, Laravel

So i have this index page which has the list of task and inside the table is the title of task and the actions, what i'm trying to do is when i click the task title the modal will pop whereas in the modal is its body and content of the said task.. here is the code
controller
public function index()
{
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
return view('posts.index')->with('posts', $posts);
}
index page
#extends('layout.app')
#section('content')
<div class="container">
<div class="col-md-6" style="float:left;">
<h2>Todo List</h2>
<div class="table-hover">
<table class="table">
<thead class="thead-default">
<tr>
<th>Title</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(count($todos) > 0)
#foreach($todos as $todo)
<tr>
<th>{{$todo->title}}</th>
<td>{{$todo->status}}</td>
<td><a class="btn btn-sample btn-sm">Edit</a> <a class="btn btn-sample2 btn-sm">Delete<a></td>
</tr>
</tbody>
#endforeach
#else
<th><h2>Nothing to show</h2></th>
#endif
</table>
</div>
</div>
the modal below the same page
<div class="modal fade" id="exampleModal3" tabindex="-1" role="dialog" aria-labelledby="exampleModal3Label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModal3Label">View task</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h3>{{$todo->title}}</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
There are two methods to achieve this, either you repeat your modal with unique id with tr or records, or you create one modal, and onClick bring data from ajax or javascript, and fill it within modal.
I'm explaining you the first and easiest one
#extends('layout.app')
#section('content')
<div class="container">
<div class="col-md-6" style="float:left;">
<h2>Todo List</h2>
<div class="table-hover">
<table class="table">
<thead class="thead-default">
<tr>
<th>Title</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(count($todos) > 0)
#foreach($todos as $todo)
<tr>
<th>{{$todo->title}}
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModal3Label{{ $loop->iteration }}">View task</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h3>{{$todo->title}}</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</th>
<td>{{$todo->status}}</td>
<td><a class="btn btn-sample btn-sm">Edit</a> <a class="btn btn-sample2 btn-sm">Delete<a></td>
</tr>
</tbody>
#endforeach
#else
<th><h2>Nothing to show</h2></th>
#endif
</table>
</div>
</div>

modal and while loop

I have been scratching my head about this for days and i cant seem to understand why this isnt working,
i have a modal which pops up when selecting the accept button, this modal should populate the users name and telephone number.
the problem is that when you select the accept it only populate the first persons details and no the rest of the people in the table, i dont understand why if anyone can point out why i would be much obliged
here is my code (sorry its abit messy at the moment):
<?php
require_once ("includes/session_check.php");
require_once ("includes/db_connect.php");
//MySqli Select Query
$results = $mysqli->query("SELECT id, username, phone, requester,goodorbad,confirmed,denied FROM request WHERE goodorbad=1 AND confirmed=0 AND denied=0 Order by ID ASC");
echo '
<!-- **********************************************************************************************************************************************************
MAIN CONTENT
*********************************************************************************************************************************************************** -->
<!--main content start-->
<section id="main-content">
<section class="wrapper site-min-height">
<h3><i class="fa fa-angle-right"></i> Non Authenticated Requests</h3>
<div class="row mt">
<div class="col-lg-12">
<p>Non Authenticated Requests here.</p>';
echo '<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<table class="table table-striped table-advance table-hover">
<h4><i class="fa fa-angle-right"></i>Non Authenticated Requests</h4>
<hr> <thead>
<tr>
<th><i class="fa fa-bullhorn"></i>Username</th>
<th><i class="fa fa-phone"></i>Phone number update request</th>
<th><i class="fa fa-bookmark"></i>Requester</th>
<th><i class=" fa fa-edit"></i> Status</th>
<th><i class=" fa fa-edit"></i> Accept/Decline</th>
<th></th>
</tr>
</thead>';
while($row = $results->fetch_object()) {
//$row->id
//$row->confirmed
//$row->denied
$status = $row->confirmed;
echo '
<!-- Modal for Confirm -->
<form action="#" method="POST">
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<input type="hidden" name="username_request" value="'.$row->username.'"/>
<input type="hidden" name="telephone_request" value="'.$row->phone.'"/>
<h4 class="modal-title">Are you sure ?</h4>
</div>
<div class="modal-body">
<br>
<p>You are about to update </p>
'.$row->username.'
<br>
<p> with the following telephone number </p>
'.$row->phone.'
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default"
type="button">Cancel</button>
<button data-dismiss="modal" class="btn btn-default"
type="button">Submit</button>
</div>
</div>
</div>
</div>
</form>
<!-- modal -->
<tbody>
<tr>
<td>'.$row->username.'</td>
<td>'.$row->phone.'</td>
<td>'.$row->requester.' </td>';
if($status == 1){
echo ' <td><span class="label label-success">Completed</span></td>
<td></td>
<td></td>
';
}else{
}
echo '<td><span class="label label-info label-mini">Pending</span></td>
<td>
Accept?
<a data-toggle="modal" href="#myModal"> <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
</a>
<td>
Decline?
<button class="btn btn-danger btn-xs"><i class="fa fa-minus-circle"></i></button>
</td>
</td>
</tr>
</tbody>
';
}
echo '
</table>
</div><!-- /content-panel -->
</div><!-- /col-md-12 -->
</div><!-- /row -->';
// close connection
$mysqli->close();
?>
<html>
</html>
You need to add unique id to every model.
Try
<?php
require_once ("includes/session_check.php");
require_once ("includes/db_connect.php");
//MySqli Select Query
$results = $mysqli->query("SELECT id, username, phone, requester,goodorbad,confirmed,denied FROM request WHERE goodorbad=1 AND confirmed=0 AND denied=0 Order by ID ASC");
echo '
<!-- **********************************************************************************************************************************************************
MAIN CONTENT
*********************************************************************************************************************************************************** -->
<!--main content start-->
<section id="main-content">
<section class="wrapper site-min-height">
<h3><i class="fa fa-angle-right"></i> Non Authenticated Requests</h3>
<div class="row mt">
<div class="col-lg-12">
<p>Non Authenticated Requests here.</p>';
echo '<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<table class="table table-striped table-advance table-hover">
<h4><i class="fa fa-angle-right"></i>Non Authenticated Requests</h4>
<hr> <thead>
<tr>
<th><i class="fa fa-bullhorn"></i>Username</th>
<th><i class="fa fa-phone"></i>Phone number update request</th>
<th><i class="fa fa-bookmark"></i>Requester</th>
<th><i class=" fa fa-edit"></i> Status</th>
<th><i class=" fa fa-edit"></i> Accept/Decline</th>
<th></th>
</tr>
</thead>';
$i = 1;
while($row = $results->fetch_object())
{
//$row->id
//$row->confirmed
//$row->denied
$status = $row->confirmed;
echo '
<!-- Modal for Confirm -->
<form action="#" method="POST">
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal'.$i.'" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<input type="hidden" name="username_request" value="'.$row->username.'"/>
<input type="hidden" name="telephone_request" value="'.$row->phone.'"/>
<h4 class="modal-title">Are you sure ?</h4>
</div>
<div class="modal-body">
<br>
<p>You are about to update </p>
'.$row->username.'
<br>
<p> with the following telephone number </p>
'.$row->phone.'
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default"
type="button">Cancel</button>
<button data-dismiss="modal" class="btn btn-default"
type="button">Submit</button>
</div>
</div>
</div>
</div>
</form>
<!-- modal -->
<tbody>
<tr>
<td>'.$row->username.'</td>
<td>'.$row->phone.'</td>
<td>'.$row->requester.' </td>';
if($status == 1)
{
echo ' <td><span class="label label-success">Completed</span></td>
<td></td>
<td></td>
';
}
echo '<td><span class="label label-info label-mini">Pending</span></td>
<td>
Accept?
<a data-toggle="modal" href="#myModal'.$i.'"> <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
</a>
<td>
Decline?
<button class="btn btn-danger btn-xs"><i class="fa fa-minus-circle"></i></button>
</td>
</td>
</tr>
</tbody>
';
$i++;
}
echo '
</table>
</div><!-- /content-panel -->
</div><!-- /col-md-12 -->
</div><!-- /row -->';
// close connection
$mysqli->close();
?>
<html>
</html>
You will have to change 2 lines. The reason of error is you are calling the same id again and again. So you will have to change id dynamically with loop. You will have to change your 2 lines like these:
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal'.$i.'" class="modal fade">
and
<a data-toggle="modal" href="#myModal'.$i.'"> <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button></a>
Final code is:
<?php
require_once ("includes/session_check.php");
require_once ("includes/db_connect.php");
//MySqli Select Query
$results = $mysqli->query("SELECT id, username, phone, requester,goodorbad,confirmed,denied FROM request WHERE goodorbad=1 AND confirmed=0 AND denied=0 Order by ID ASC");
echo '
<!-- **********************************************************************************************************************************************************
MAIN CONTENT
*********************************************************************************************************************************************************** -->
<!--main content start-->
<section id="main-content">
<section class="wrapper site-min-height">
<h3><i class="fa fa-angle-right"></i> Non Authenticated Requests</h3>
<div class="row mt">
<div class="col-lg-12">
<p>Non Authenticated Requests here.</p>';
echo '<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<table class="table table-striped table-advance table-hover">
<h4><i class="fa fa-angle-right"></i>Non Authenticated Requests</h4>
<hr> <thead>
<tr>
<th><i class="fa fa-bullhorn"></i>Username</th>
<th><i class="fa fa-phone"></i>Phone number update request</th>
<th><i class="fa fa-bookmark"></i>Requester</th>
<th><i class=" fa fa-edit"></i> Status</th>
<th><i class=" fa fa-edit"></i> Accept/Decline</th>
<th></th>
</tr>
</thead>';
$i=0;
while($row = $results->fetch_object()) {
$i++;
//$row->id
//$row->confirmed
//$row->denied
$status = $row->confirmed;
echo '
<!-- Modal for Confirm -->
<form action="#" method="POST">
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal'.$i.'" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<input type="hidden" name="username_request" value="'.$row->username.'"/>
<input type="hidden" name="telephone_request" value="'.$row->phone.'"/>
<h4 class="modal-title">Are you sure ?</h4>
</div>
<div class="modal-body">
<br>
<p>You are about to update </p>
'.$row->username.'
<br>
<p> with the following telephone number </p>
'.$row->phone.'
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default"
type="button">Cancel</button>
<button data-dismiss="modal" class="btn btn-default"
type="button">Submit</button>
</div>
</div>
</div>
</div>
</form>
<!-- modal -->
<tbody>
<tr>
<td>'.$row->username.'</td>
<td>'.$row->phone.'</td>
<td>'.$row->requester.' </td>';
if($status == 1){
echo ' <td><span class="label label-success">Completed</span></td>
<td></td>
<td></td>
';
}else{
}
echo '<td><span class="label label-info label-mini">Pending</span></td>
<td>
Accept?
<a data-toggle="modal" href="#myModal'.$i.'"> <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
</a>
<td>
Decline?
<button class="btn btn-danger btn-xs"><i class="fa fa-minus-circle"></i></button>
</td>
</td>
</tr>
</tbody>
';
}
echo '
</table>
</div><!-- /content-panel -->
</div><!-- /col-md-12 -->
</div><!-- /row -->';
// close connection
$mysqli->close();
?>
<html>
</html>

Categories