Passing data to modal codeigniter framework - php

Currently i have a table that is generated via DB and i want to view the details of the selected value on the data table. I separated the modal with view/users/modal. but there's an error
Message: Trying to get property 'user_id' of non-object
please help me to resolve this
I am running Codeigniter 3.1.10
Xampp V3.2.3
Data Table
<?php echo $this->load->view('users/Modal/view_modal'); ?>
<tbody>
<?php if(!empty($value)): ?>
<?php foreach($value as $row): ?>
<tr>
<td align="center"><?php echo $row->user_id; ?></td>
<td align="center"><?php echo $row->firstname; ?></td>
<td align="center"><?php echo $row->lastname; ?></td>
<td align="center"><?php echo $row->email; ?></td>
<td align="center">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#info_modal<?php echo $row->user_id; ?>">
<i class="far fa-eye"></i>
</button>
<a href="<?php echo base_url('users/view_edit_form/'.$row->user_id); ?>" class="btn btn-success">
<i class="fas fa-user-edit"></i>
</a>
<a href="" class="btn btn-danger">
<i class="fas fa-ban"></i>
</a>
</td>
<?php endforeach; ?>
My Modal
<div class="modal fade" id="info_modal<?php echo $row->user_id; ?> " 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"><?php echo $row->firstname; ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</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>
</div>
</div>
I expect the output will display more information of the user but the actual output is an error message Message: Trying to get property 'user_id' of non-object

try to check what $row contains.
If -> don't work, try using $row['user_id']

Related

Bootstrap modal doesn't get user ID

I have a problem with a bootstrap modal. I'm using PHP.
In an admin panel there is a table with the list of the users and the possibility to edit or delete a user's profile. For the delete I want to create a modal for the confirm of the delete but, when I click on "confirm" button inside the modal, the modal gets by default the user ID of the first user in the table and not the user ID of the selected user.
Here is the code:
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php var_dump($utente['idUser']); ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
If I make a var_dump of $utente['idUser'] before the modal, it gets the right user ID. If I make it inside the modal it gets by default the first ID, as I said.
Notice that every modal trigger button has a data-target attribute to define which modal will be opened.
In your case, the button of every row you used to triggered the modal have the same data-target, which is #confirmDelete. These modals behind also has the same id called #confirmDelete, so every time you hit the modal trigger button (all had the same data-target) then eventually it will shows up the very first modal element.
For a better understanding, compare my code to yours and see the differences.
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php echo $utente['idUser']; ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
In the above code, I gave every pair of modal elements (modal trigger button and modal ID) a unique data-target value and a unique element id.
...
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>">
...
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
...
Now each pair of modal elements have their own ids and they should be working the way you wanted.

Passing a PHP echo value to a modal box

I'm creating a web back end for a restaurant. There is an option to block/unblock restaurant owners. I have put an "if" condition to appear button (a tag) according to the status of the restaurant owner. Within this "a" tag I want to pass the value (id) to a modal box.
I put some code in the "data-target" of "a" tag, as well as in the modal id. The value didn't pass and also didn't open the modal.
<tbody>
<?php
global $con;
$sql = "SELECT * FROM `fd_owner_details`";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$kk=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td><?php echo '<img src= "'.$row['image'].'">'; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php
if ($row['status']==1){
echo '<i class="fa fa-ban" aria-hidden="true"></i>';
}
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
--------------------------------------------------------------------------------
<div class="modal fade" id="blockRestaurantOwner?id=<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
Where Should I change, to get the "id" to the modal ?
Just copy the whole script and try to past inside a php file and try to run it. This can be modified as expected at your end using while loop.
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<?php
$array[] = array('id'=>1, 'fname'=>'hari','lname'=>'ltest','address_line1'=>'no:9','address_line1'=>'rajan street','contact_no1'=>'92222','contact_no2'=>'899782','status'=>1);
$array[] = array('id'=>2, 'fname'=>'vvvv','lname'=>'ltest2','address_line1'=>'no:92','address_line1'=>'siv street','contact_no1'=>'522','contact_no2'=>'2922','status'=>0);
?>
<table border=1>
<tbody>
<?php foreach($array as $row){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php if ($row['status']==1){ ?>
<a data-toggle="modal" href="#blockRestaurantOwner<?=$row['id']?>" class="btn btn-warning btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="Block"><i class="fa fa-ban" aria-hidden="true"></i></a>
<div id="blockRestaurantOwner<?php echo $row['id']; ?>" class="modal fade" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
<?php }
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
</table>
you can set onclick listener to block button and pass owner id to that function like below
<tbody>
<?php
global $con;
$sql = "SELECT * FROM `fd_owner_details`";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$kk=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']. " " .$row['lname'] ; ?></td>
<td><?php echo $row['address_line1']. ", " .$row['address_line2'] ; ?></td>
<td><?php echo $row['contact_no1'].", ".$row['contact_no2']; ?></td>
<td><?php echo '<img src= "'.$row['image'].'">'; ?></td>
<td>
<?php
if ($row['status']==1){
echo "<span class='label mb-2 mb-xl-0 label-dark'>Active</span>";
}
else {
echo "<span class='label mb-2 mb-xl-0 label-light'>Disabled</span>";
}
?>
</td>
<td>
<span>
<i class="fa fa-pencil" aria-hidden="true"></i>
<?php
if ($row['status']==1){
echo '<i class="fa fa-ban" aria-hidden="true"></i>';
}
else {
echo "<a href='#' data-toggle='modal' data-target='#unblockRestaurantOwner' class='btn btn-warning btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Unblock'><i class='fa fa-check' aria-hidden='true'></i></a>";
}
?>
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</td>
</tr>
<?php } ?>
</tbody>
<div class="modal fade" id="blockRestaurantOwner" tabindex="-1" role="dialog" aria-labelledby="blockRestaurantOwnerTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">× </span>
</button>
</div>
<div class="modal-body">
<p id="main-content">Are you sure you want to block this restaurant owner ???</p>
<p id="content">You will not be able to recover this action !!!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No, Cancel it </button>
<button type="button" class="btn btn-warning">Yes, Block</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var target_owner_id="";
function set_target_id(id){
target_owner_id = id;
console.log(target_owner_id);
}
</script>

Displaying data from database to modal CodeIgniter

I'm making an Computer Laboratory Monitoring System using CI framework.
I want to display the detail of the selected item in my list but i got an error says :
Message: Trying to get property 'invent_id' of non-object
Here's my code view code
<?php if(!empty($value)): ?>
<?php foreach($value as $post ): ?>
<tr>
<td data-field="id"><?php echo $post->invent_id;?></td>
<td data-field="id"><?php echo $post->name;?></td>
<td data-field="id"><?php echo $post->type;?></td>
<td data-field="id"><?php echo $post->stock;?></td>
<td>
<button name="view" data-toggle="modal" data-target="#exampleModal<?php echo $post->invent_id; ?>" class="btn btn-info view_data"> <span class="glyphicon glyphicon-eye-open">
</span></button>
<span class="glyphicon glyphicon-edit"></span>
<span class="glyphicon glyphicon-ban-circle"></span></td>
<?php $this->load->view('Modal/view_inventory_detail', $post); ?>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td>No Records Found</td>
</tr>
<?php endif; ?>
</tr>
</tbody>
</table>
My controller
function inventory_list(){
$value['value'] = $this->Admin_Model->get_inventory();
$this->load->view('HeadtoFoot/header');
$this->load->view('Admin/inventory/list',$value);
$this->load->View('HeadtoFoot/footer');
}
And My Modal
<div class="modal fade" id="exampleModal<?php echo $post->invent_id; ?>" 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">Item Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- Details Goes Here -->
</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>
</div>
</div>
The Trying to get property of non-object error in CI generally happens when running a query. I would guess the error is happening in the call to the model:
$this->Admin_Model->get_inventory()

How to pass GET value to bootstrap modal

i have a bootstrap modal and am trying to pass a unique value through GET to the modal but the modal keeps getting the same unique value instead of the value of the table row i clicked. Been struggling with this for days and cant seem to figure out why it's not working. The code is below
<?php
$sql = "SELECT * FROM houses";
$q=$conn->query($sql);
while ($row = mysqli_fetch_array($q)) {
?>
<tr>
<td><?php echo $row['nickname']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['address']; ?></td>
<td>
<div class="btn-group">
<a class="btn btn-success" href="view_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_check_alt2"></i></a>
<a class="btn btn-primary" href="edit_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_plus_alt2"></i></a>
<a class="btn btn-danger" data-toggle="modal" data-target="#myDelete"><i class="icon_close_alt2"></i></a>
</div>
<div id="myDelete" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Property</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to pull out this property from your list of Properties, as all data regarding this property will be lost permanently <?php echo $row['nickname']; ?></p>
</div>
<div class="modal-footer">
<a class="btn btn-success" href="view_property.php?house=<?php echo $row['nickname']; ?>">Delete</a>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</td>
HTML ids should be unique, you're using the same id for every iteration.
Try using:
id="myDelete<?php echo $row['id'] ?>"
data-target="#myDelete<?php echo $row['id'] ?>"
Hope this helps!

How can I output child view with dynamically generated captureTo for addChild() in controller

Giving the following manual:
enter link description here
It is quite easy to have a child view and echo it when I know captureTo used in the controller (e.g. "<?php echo $this->article ?>"), but can I do similar when I generate view models dynamically, and assign them to dynamically generated captureTo for addChild() function:
foreach ($studentEvaluations as $studEval) {
$studEvalId = $studEval->getEvalId();
$formViewModel = $this->buildStudentEvaluationViewModel($studEval);
$viewModel->addChild($formViewModel, $studEvalId);
}
I tried the following, but it does not work:
<?php foreach ($this->viewModel()->getCurrent()->getIterator() as $studId => $studEval) : ?>
<tr>
<td><?php echo $this->escapeHtml($studEval->fname); ?></td>
<td><?php echo $this->escapeHtml($studEval->lname); ?></td>
<td><?php echo $this->escapeHtml($studEval->formName); ?></td>
<td><?php echo $this->escapeHtml($studEval->supdated); ?></td>
<td><?php echo $this->escapeHtml($studEval->screated); ?></td>
<td>
<button class="btn btn-primary btn-lg"
data-toggle="modal"
data-target="#myModal<?php echo $studId; ?>"
data-loading-text="Loading..."> Edit
</button>
<!-- Modal -->
<div class="modal fade" id="myModal<?php echo $studId; ?>" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<p>
<!-- HERE ->>>>>>>>> --> <?php echo $studEval ?>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php endforeach; ?>
EDIT 1
This is an error when I try to output the view in the this way:
Catchable fatal error: Object of class Zend\View\Model\ViewModel could not be converted to string in C:\dev\projects\OnlineFieldEvaluation\module\OnlineFieldEvaluation\view\online-field-evaluation\online-field-evaluation\test3.phtml on line 62
I found how to do it. It was just my luck of knowledge in PHP:
<?php foreach ($this->viewModel()->getCurrent()->getChildren() as $studEval) : ?>
<tr>
<td><?php echo $this->escapeHtml($studEval->fname); ?></td>
<td><?php echo $this->escapeHtml($studEval->lname); ?></td>
<td><?php echo $this->escapeHtml($studEval->formName); ?></td>
<td><?php echo $this->escapeHtml($studEval->supdated); ?></td>
<td><?php echo $this->escapeHtml($studEval->screated); ?></td>
<td>
<button class="btn btn-primary btn-lg"
data-toggle="modal"
data-target="#myModal<?php echo $studEval->captureTo(); ?>"
data-loading-text="Loading...">
Edit <?php echo $studEval->captureTo(); ?>
</button>
<!-- Modal -->
<div class="modal hide fade" id="myModal<?php echo $studEval->captureTo(); ?>" tabindex="-1"
role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<p>
<?php
echo $this->{$studEval->captureTo()};
?>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php endforeach; ?>

Categories