Add Variable from PHP foreach to bootstrap modal - javascript

I have a button within a php foreach statement which loads a modal. How can i pass the particular id ($animalid) to the modal? I have it kind of working but the modal will load the same id in each modal popup. See my php foreach code below and also part of the modal.
$pdo2 = Database::connect();
$sql2 = 'SELECT * FROM animals WHERE riderid = '.$data[id].' AND hp != "Choose One"';
foreach ($pdo2->query($sql2) as $row) {
echo '<tr>';
echo '<td>'. $row['hp'] . '</td>';
echo '<td>'. $row['hpname'] . '</td>';
echo '<td>'. $row['hpage'] . '</td>';
echo '<td>'. $row['hpcolour'] . '</td>';
echo '<td>'. $row['hpmicro'] . '</td>';
echo '<td>';
echo '<button class="btn btn-default btn-xs" id="float-right" data-toggle="modal" data-target=".bs-example-modal-lg-2"><span class="glyphicon glyphicon-pencil"></span> Update</button>';
echo ' ';
echo '<a class="btn btn-default btn-xs" href="#"><span class="glyphicon glyphicon-trash"></span> Delete</a>';
echo '</td>';
echo '</tr>';
$animalid = $row['id'];
}
Database::disconnect();
?>
Modal Code
<div class="modal-body">
<form name="editanimal" id="editanimal" class="form-horizontal" action="updateanimal.php" method="post">
<span class="form-break">
<?php
// Get Animal id
//$animalid = $data['id'];
echo $animalid;
?>

$animalid is in the loop. If you want to use that value, you can store the data to other array and use it. Hope you get some inspiration.
PHP:
<?php
$pdo2 = Database::connect();
$sql2 = 'SELECT * FROM animals WHERE riderid = '.$data[id].' AND hp != "Choose One"';
$list = array();
foreach ($pdo2->query($sql2) as $row) {
$item = array();
$content = '<tr>';
$content .= '<td>'. $row['hp'] . '</td>';
$content .= '<td>'. $row['hpname'] . '</td>';
$content .= '<td>'. $row['hpage'] . '</td>';
$content .= '<td>'. $row['hpcolour'] . '</td>';
$content .= '<td>'. $row['hpmicro'] . '</td>';
$content .= '<td>';
$content .= '<button class="btn btn-default btn-xs" id="float-right" data-toggle="modal" data-target=".bs-example-modal-lg-2"><span class="glyphicon glyphicon-pencil"></span> Update</button>';
$content .= ' ';
$content .= '<a class="btn btn-default btn-xs" href="#"><span class="glyphicon glyphicon-trash"></span> Delete</a>';
$content .= '</td>';
$content .= '</tr>';
$item['id'] = $row['id'];
$item['content'] = $content;
$list[] = $item;
}
Database::disconnect();
?>
HTML:
<div class="modal-body">
<form name="editanimal" id="editanimal" class="form-horizontal" action="updateanimal.php" method="post">
<span class="form-break"></span>
<?php foreach($list as $item){ ?>
<?php echo $item['id'];?>
<?php } ?>
or..
<table>
<?php foreach($list as $item){ ?>
<?php echo $item['content'];?>
<?php } ?>
</table>
<!-- ... -->
</form>
</div>

$animalid is being over written each time you iterate through the foreach loop. You would have to build an array to hold all of the animalids, and pass that to your modal or print out the id as you are iterating through the loop.

Related

How to hide column if roles not 'admin'

How to hide the Read, Update and Delete button column from other user. Only user with 'admin' roles can see the button. Every users has been assign either 'admin' or 'user' roles in database.
can hide the button from other user which not admin
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>";
echo '<span class="fa fa-eye"></span>';
echo '<span class="fa fa-pencil"></span>';
echo '<span class="fa fa-trash"></span>';
echo "</td>";
echo "</tr>";
if you already save the user role in the database, you can check the condition using php like below
while($row = mysqli_fetch_array($result)){
$userRole = $row['role'];
$traw = "<tr>";
$traw .="<td>" . $row['id'] . "</td>";
$traw .="<td>" . $row['name'] . "</td>";
$traw .="<td>" . $row['address'] . "</td>";
$traw .="<td>" . $row['salary'] . "</td>";
$traw .="<td >";
if($userRole == 'admin') {
$traw .='<span class="fa fa-eye"></span>':'';
$traw .='<span class="fa fa-pencil"></span>';
$traw .='<span class="fa fa-trash"></span>';
}
$traw .= "</td>";
$traw .="</tr>";
echo $traw;

pagination not appearing in php

i have a table in php whose data comes from sql database, so i have kept a pagination for the table because the data is long
<?php
require_once "config.php";
$perpage = 5;
if(isset($_GET['page']) & !empty($_GET['page'])){
$curpage = $_GET['page'];
}else{
$curpage = 1;
}
$start = ($curpage * $perpage) - $perpage;
$sql = "SELECT * FROM `registers` LIMIT $start, $perpage";
$PageSql = "SELECT * FROM `registers`";
$pageres = mysqli_query($link, $PageSql);
$totalres = mysqli_num_rows($pageres);
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table'>";
echo "<thead>";
echo "<tr>";
echo "<th class='serial'>ID</th>";
echo "<th>Name</th>";
echo "<th>Phone Number</th>";
echo "<th>Email</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
?>
<nav aria-label="Page navigation">
<ul class="pagination">
<?php if($curpage != $startpage){ ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $startpage ?>" tabindex="-1" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">First</span>
</a>
</li>
<?php } ?>
<?php if($curpage >= 2){ ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $previouspage ?>"><?php echo $previouspage ?></a></li>
<?php } ?>
<li class="page-item active"><a class="page-link" href="?page=<?php echo $curpage ?>"><?php echo $curpage ?></a></li>
<?php if($curpage != $endpage){ ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $nextpage ?>"><?php echo $nextpage ?></a></li>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $endpage ?>" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Last</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
<?php }?>
the data is correctly added into the database, but the problem is the pagination is nowhere seen in the page. when i just put the html it was coming,but when i added the php code inside it,its is not coming. can anyone tell me what could be the problem please?
Move <nav></nav> code inside
if(mysqli_num_rows($result) > 0){ }
Before or after your table.
Issue is that you now show it only if your if statement fails: if($result = mysqli_query($link, $sql)){ } else { }
Tip:
To Prevent SQL Injection i suggest:
PHP MySQLi Prepared Statements Tutorial
PHP PDO Prepared Statements Tutorial

JQuery: How to pass PHP data from div to div

As for my HTML code is
<div class="right">
<h2>Product Detail</h2>
<div id="righttop"></div>
<button id="Add">Add</button>
</div>
<div class="rightbot">
<h2>Shopping Car</h2>
<div id="rightbot"></div>
</div>
And the PHP is
mysqli_select_db($con,"products");
$sql="SELECT product_id, product_name, unit_price, unit_quantity, in_stock FROM products WHERE product_id = '".$productid."'";
$result = mysqli_query($con,$sql);
echo "<table>";
echo "<th>Product Name</th>";
echo "<th>Unit Price</th>";
echo "<th>Unit Quantity</th>";
echo "<th>In Stock</th>";
while($row = mysqli_fetch_array($result)) {
echo "<tr id='detail'>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['unit_price'] . "</td>";
echo "<td>" . $row['unit_quantity'] . "</td>";
echo "<td>" . $row['in_stock'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo $_POST['detail'];
mysqli_close($con);
?>
div#righttop is changed by another button.
How can I pass the current content from div#righttop to div#rightbot?
If you meant to transfer the contents of righttop to rightbot, here is a way. On click of the button, before changing anything in righttop, do this:
$('#rightbot').html($('#righttop').html());

How can I declare my Modal in the Javascript code

Actually i want to set the Modal on the box-click so i put the onclick tag in the box tag but after that my modal is not appear...
Actually i passed the unique-Id for the individual box and that's work's fine but the Modal is not appear.....So please help me
<?php
include("connect.php");
$query="SELECT * FROM `orders`";
$filter_Result=mysqli_query($con,$query);
$count=0;
while($row = mysqli_fetch_array($filter_Result)){
$count++;
$boxid = $count;
echo "<div class='block'>";
echo "<div class='boxed' id=$boxid onclick='myFunction($boxid)'>";
"<button id='$boxid'>Open Modal</button>";
"<div id='$boxid' class='modal'>";
"<div class='modal-content'>";
"<span class='close'>x</span>";
"<p>Some text in the Modal..</p>";
"</div>";
"</div>";
echo "<div id='container'>";
echo "<td>" . $row['id'] . "</td>" ."<br>";
echo "<td>" . $row['status'] . "</td>"."<br>";
echo "<td>" . $row['created_date'] . "</td>"."<br>";
echo "<td>" . $row['uid'] . "</td>"."<br>"."<br>"."<br>"."<br>";
echo "</div>";
echo "</div>";
echo "</div>";
}
?>
<script>
function myFunction(id) {
//modal = document.getElementById($id);
//window.alert(modal);
// Get the button that opens the modal
//var btn = document.getElementById("myBtn");
window.alert(id);
document.getElementById(id).innerHTML = Date();
}
</script>
Replace this code:
<?php
include("connect.php");
$query="SELECT * FROM `orders`";
$filter_Result=mysqli_query($con,$query);
$count=0;
while($row = mysqli_fetch_array($filter_Result)){
$count++;
$boxid = $count;
echo "<div class='block'>";
echo "<div class='boxed'>";
"<button onclick='myFunction($boxid)'>Open Modal</button>";
"<div id='$boxid' class='modal'>";
"<div class='modal-content'>";
"<span class='close'>x</span>";
"<p>Some text in the Modal..</p>";
"</div>";
"</div>";
echo "<div id='container'>";
echo "<td>" . $row['id'] . "</td>" ."<br>";
echo "<td>" . $row['status'] . "</td>"."<br>";
echo "<td>" . $row['created_date'] . "</td>"."<br>";
echo "<td>" . $row['uid'] . "</td>"."<br>"."<br>"."<br>"."<br>";
echo "</div>";
echo "</div>";
echo "</div>";
}
?>
<script>
function myFunction(id) {
modal = document.getElementById($id);
window.alert(modal);
}
</script>
Use like this, this is an bootstrap model popup
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<?php
include("connect.php");
$query="SELECT * FROM `orders`";
$filter_Result=mysqli_query($con,$query);
$count=0;
while($row = mysqli_fetch_array($filter_Result)){
$count++;
$boxid = $count;
echo '<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal'.$boxid.'">Open Modal</button>
<div class="modal fade" id="myModal'.$boxid.'" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Details</h4>
</div>
<div class="modal-body">
<tr width="100%">
<td>'. $row['id'].'</td>
<td>'. $row['status'].'</td>
<td>'. $row['created_date'].'</td>
<td>'. $row['uid'].'</td>
</tr>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>';
}
?>
</div>

Javascript print function

I need help trying to print certain fields from the table using javascript. Mainly each ticket number that is generated with the name and phone number of the person and another function to just print the address of the person that purchased the tickets. I have been racking my brain for a couple of days and think I just need some fresh eyes to help me out. Here s the code I have been working with, I will admit that I am somewhat of a newb! i just a little help. Thanks.
<div class="span16">
<div style="float:right;">
Printer 3 Checked
<!--Printer 2 Checked-->
</div>
<h1>Orders</h1>
<hr/>
<?php echo $this->element('pager'); ?>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>Order</th>
<th>Raffle</th>
<!--<th>Transaction</th>-->
<th style="white-space:nowrap;">Tickets</th>
<th>Total</th>
<th>Date</th>
<th>Status</th>
<th>Name</th>
<th style="white-space:nowrap;width:200px;">Address</th>
<th style="white-space:nowrap;width:100px;">Print</th>
<th style="white-space:wrap;width:100px;">Address Label</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
$t = 0;
foreach ($orders as $order):
$rowclass = 'orders';
if ($i++ % 2 == 0) {
$rowclass .= ' orders-even';
} else {
$rowclass .= ' orders-odd';
}
$id = $order['Order']['id'];
//$transaction_number = $order['Order']['transaction_number'];
$quantity = $order['Order']['quantity'];
$total = '$'.number_format($order['Order']['total'], 2, '.', ',');
$date = '<div class="date">'.date('m/d/y', strtotime($order['Order']['modified'])).' </div>';
$date .= '<div class="time">'.date('h:i a', strtotime($order['Order']['modified'])).' </div>';
$status = '<div class="status status-'.strtolower($order['Order']['status']).'">'.$order['Order']['status'].' </div>';
$name = '<div class="name">'.$order['Order']['first_name'].' '.$order['Order']['last_name'].' </div>';
$name .= '<div class="email">'.$order['Order']['email'].' </div>';
$phone = '<div class="phone">'.$order['Order']['phone'].' </div>';
if (!empty($phone)) {
$name .= '<span class="phone">'.$phone.'</span>';
}
$address = '<div class="address">'.$order['Order']['address'].'</div>';
if (!empty($order['Order']['address2'])) {
$address .= '<div class="address">'.$order['Order']['address2'].'</div>';
}
$csz = $order['Order']['city'];
if (!empty($order['Order']['state'])) {
$csz .= ' '.$order['Order']['state'];
}
if (!empty($order['Order']['zip'])) {
$csz .= ', '.$order['Order']['zip'];
}
$address .= '<div class="csz">'.$csz.'</div>';
if (empty($order['Order']['address2'])) {
$address .= '<div class=""></div>';
}
$raffle = $order['Raffle']['name'];
if (!empty($order['Order']['printed'])) {
$printed = '<a href="/orders/view/'.$order['Order']['id'].'/printer1" class="btn btn-printed">';
$printed .= 'Tickets';
$printed .= '</a>';
} else {
$printed = '<a href="/orders/view/'.$order['Order']['id'].'/printer1" class="btn">';
$printed .= 'Tickets';
$printed .= '</a>';
}
/* settings for address labels */
if (!empty($order['Order']['printed2'])) {
$printed2 = '<a href="/orders/address/'.$order['Order']['id'].'" class="btn btn-printed">';
$printed2 .= 'Address';
$printed2 .= '</a>';
} else {
$printed2 = '<a href="/orders/address/'.$order['Order']['id'].'/" class="btn">';
$printed2 .= 'Address';
$printed2 .= '</a>';
}
?>
<tr class="<?php echo $rowclass; ?>" rel="tickets-<?php echo $order['Order']['id']; ?>">
<?php echo '<td align="center"><input type="checkbox" name="data[Order][][id]" id="OrderId'.$order['Order']['id'].'" value="'.$order['Order']['id'].'"></td>'; ?>
<?php echo '<td>'.$raffle.'</td>'; ?>
<?php //echo '<td>'.$transaction_number.'</td>'; ?>
<?php echo '<td nowrap>'; ?>
<?php foreach ($order['Ticket'] as $ticket): ?>
<?php echo $ticket['reference_number']; ?>
<script>
function printTickets()
{
window.print()
}
</script>
<input type="button" value="Print" onclick="printMe()"><br />
<?php endforeach; ?>
<?php echo '</td>'; ?>
<?php echo '<td>'.$total.'</td>'; ?>
<?php echo '<td>'.$date.'</td>'; ?>
<?php echo '<td>'.$status.'</td>'; ?>
<?php echo '<td>'.$name.'</td>'; ?>
<?php echo '<td>'.$address.'</td>'; ?>
<?php echo '<td>'.$printed.'</td>'; ?>
<?php echo '<td>'.$printed2.'
</td>';?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo $this->element('pager'); ?>
<?php $this->start('sidebox.orders');?>
<div class="well">
<?php echo $this->Html->link(__('New Order', true), array('action' => 'add'), array('class' => 'btn primary')); ?>
<?php echo $this->Html->link(__('List Orders', true), array('controller' => 'orders', 'action' => 'index'), array('class' => 'btn')); ?>
<?php echo $this->Html->link(__('New Order', true), array('controller' => 'orders', 'action' => 'add'), array('class' => 'btn')); ?>
<?php echo $this->Html->link(__('List Tickets', true), array('controller' => 'tickets', 'action' => 'index'), array('class' => 'btn')); ?>
<?php echo $this->Html->link(__('New Ticket', true), array('controller' => 'tickets', 'action' => 'add'), array('class' => 'btn')); ?>
</div>
<?php $this->end(); ?>
<?php $this->append('sidebox', $this->fetch('sidebox.orders')); ?>
</div>
<script>
$('.tickets').hide();
$(function(){
$('.tickets-link').click(function(e){
e.preventDefault();
$('.'+$(this).attr('rel')).slideToggle();
});
$('.btn-printed, .btn-multiprint').addClass('disabled');
$(':checkbox').change(function(){
if ($(':checked').length) {
$('.btn-multiprint').removeClass('disabled');
} else {
$('.btn-multiprint').addClass('disabled');
}
});
$('.btn-multiprint').click(function(e){
e.preventDefault();
var ids = [];
$(':checked').each(function(){
ids.push($(this).attr('value'));
});
window.location = $(this).attr('href')+ids.join(',')+'/'+$(this).attr('id').replace('multiprint','printer');
});
});
</script>
I may be missing something and maybe you have thought of this, but i feel I had to do something similar years back... why not use a print stylesheet to hide that which you don't want printed and show what you do? You could try dynamically loading a different print stylesheet if different clicks have different actions. How to load up CSS files using Javascript?

Categories