how to output specific information in a modal from a database - javascript

I am trying to display more information about the user in my table. The goal is to see more information about the user in a modal, than you can see in the table overview. Unfortunately, I don't manage to display only the information of the respective user in the modal. I already have the following code which doesn't work like i want. At the moment, the modal stays emtpy.
Greetings
admin.php
<?php
include('connection.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/delete_/i',$key_post) ) {
$id_to_delete = (integer) str_replace('delete_','', $key_post);
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
}
}
include('read.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/viewDetail_/i',$key_post) ) {
$id_to_view = (integer) str_replace('viewDetail_','', $key_post);
$viewQuery = "SELECT * FROM card WHERE id = " . $id_to_view;
$statement = $pdo->prepare($viewQuery);
$statement->execute();
$viewResult = $statement->fetchAll();
}
}
}
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td>
<input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
<input type="submit" name="viewDetail_<?php echo $row['ID']; ?>" value="modal" data-target="modal1" class="modal-trigger">
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
<br>
<div class="row">
<button class="btn waves-effect waves-light" type="submit" name="action">Absenden
<i class="material-icons right">send</i>
</button>
</div>
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Modal Header</h4>
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
</tr>
<?php
foreach ($viewResult as $row){ ?>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</div>
<div class="modal-footer">
Zurück
</div>
</div>
read.php
<?php
include('connection.php');
$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
foreach ($statement->fetchAll() as $row) {
$id = $row['ID'];
$imagePath = $row["ImagePath"];
$sender = $row["Sender"];
$senderEmail = $row["SenderEmail"];
$receiver = $row["Receiver"];
$receiverEmail = $row["ReceiverEmail"];
$subject = $row["Subject"];
$text = $row["Text"];
$sendDate = $row["SendDate"];
$dispatched = $row["Dispatched"];
$category = $row['Category'];
}
}
?>

Related

jquery or php if X is greater than Y

I'm making a website that displays a basic crud that shows Companyname, Stock, Minumum, Maximum.
I would like to display an alert or a message somewhere if the Stock is greater than the maximum. How could I go about doing this? Here is my display page code.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Company Name</th>
<th>Stock</th>
<th>Minumum</th>
<th>Maximum</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['companyname']; ?></td>
<td><?php echo $row['stock']; ?></td>
<td><?php echo $row['minumum']; ?></td>
<td><?php echo $row['maximum']; ?></td>
<td><a class="btn btn-info" href="update1.php?id=<?php echo $row['id']; ?>">Edit</a> <a class="btn btn-danger" href="delete1.php?id=<?php echo $row['id']; ?>">Delete</a></td>
</tr>
You can put an additional message in the Stock column with an if statement.
<td><?php echo $row['stock']; ?></td>
with
<td><?php echo $row['stock']; if ($row['stock'] > $row['maximum']) { echo " > maximum"; } ?></td>

How to change each html table row accordian

I want to create a dynamic table which gets its all its entries coming from database. I know how to make a simple table accordian by this Twitter Bootstrap Use collapse.js on table cells [Almost Done] .
If I apply that technique it turns table rows into accordian but no matter which row i click it only expands first row.
Here is the code of my table getting data from database.
<div class="container">
<h2>All the Suggestions of HR</h2>
<div class="table-responsive">
<table id="myTable" width="100%" class="table table-striped display responsive nowrap table-hover">
<thead>
<tr>
<th scope="col">Status</th>
<th scope="col">Date</th>
<th scope="col">Time</th>
<th scope="col">Subject</th>
<th scope="col">Message</th>
<th scope="col">department</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM hr;";
$result = mysqli_query($conn , $sql);
$nr = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)) :
$id = $row['hr_id'];
?>
<tr>
<?php $timestamp = $row['timeSent'];
$date = date('d-m-Y',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));
$status = $row['isAnswered'];
?>
<td><?php if ($status == 'true') {
echo "Answerd";
}
else{
echo "Pending";
} ?></td>
<td><?php echo $date;?></td>
<td><?php echo $time;?></td>
<td><?php echo $row['subject'];?></td>
<td><?php echo $row['message'];?></td>
<td><?php
$cat = $row['category_id'];
if($cat == 1){
echo "Suggestion";
}
elseif ($cat == 2) {
echo "Claim";
}
else{
echo "Help";
}?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
Whenever a row is clicked there is a content i want to show which is different for every row.
<?php
while($row = mysqli_fetch_assoc($result)) :
$id = $row['hr_id'];
?>
<tr data-toggle="collapse" data-target="#accordian_<?php echo $id; ?>" class="accordion-toggle">
<?php $timestamp = $row['timeSent'];
$date = date('d-m-Y',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));
$status = $row['isAnswered'];
?>
<td><?php echo $status == 'true' ? 'Answerd' : 'Pending'; ?></td>
<td><?php echo $date;?></td>
<td><?php echo $time;?></td>
<td><?php echo $row['subject'];?></td>
<td><?php echo $row['message'];?></td>
<td><?php
$cat = $row['category_id'];
if($cat == 1){
echo "Suggestion";
}
elseif ($cat == 2) {
echo "Claim";
}
else{
echo "Help";
}?></td>
</tr>
<tr >
<td colspan="6" class="hiddenRow">
<div class="accordian-body collapse" id="accordian_<?php echo $id; ?>"> Your Text here</div>
</td>
</tr>
<?php endwhile; ?>

PHP - Cart is not loading on the page

I have a website with a cart, the index file is as below:
<?php
session_start();
include_once 'product-action.php';
include_once 'cart.php';
$stmt = $conn->prepare("SELECT * FROM entertainment ORDER BY id ASC");
$stmt->execute();
$entertainment = $stmt->get_result();
echo '<table style="border: 1px solid #ddd;"><tr>';
if (!empty($entertainment)) {
foreach($entertainment as $product){
?>
<td style="border: 1px solid #ddd;">
<form method="post" action=""Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location?action=add&id=<?php echo $product['id']; ?>">
<div class="product-image"><img src="<?php echo $product['image']; ?>" width="200" height="200"></div>
<div><strong><?php echo $product["title"]; ?></strong></div>
<div class="product-price"><?php echo "$".$product["offer_price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" /></div>
</form>
</td>
<?php
}
}
echo '</tr></table>';
?>
The other 2 pages are as follows:
cart.php
<?php
if(!empty($_SESSION["cart_item"])){
$item_total = 0;
?>
<input type="button" value="Empty Cart">
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["product_title"]; ?></strong></td>
<td><?php echo $item["quantity"]; ?></td>
<td><?php echo "$".$item["price"]; ?></td>
<td>Remove Item</td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="3" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
<?php
if(!empty($_SESSION["cart_item"])){
$item_total = 0;
?>
<input type="button" value="Empty Cart">
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["title"]; ?></strong></td>
<td><?php echo $item["quantity"]; ?></td>
<td><?php echo "$".$item["offer_price"]; ?></td>
<td>Remove Item</td>
</tr>
<?php
$item_total += ($item["offer_price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="3" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
The product_action.php
<?php
if(!empty($_GET["action"])) {
$productId = isset($_GET['id']) ? htmlspecialchars($_GET['id']) : '';
$quantity = isset($_POST['quantity']) ? htmlspecialchars($_POST['quantity']) : '';
switch($_GET["action"]) {
case "add":
if(!empty($quantity)) {
$stmt = $conn->prepare("SELECT * FROM entertainment where id= ?");
$stmt->bind_param('i',$productId);
$stmt->execute();
$productDetails = $stmt->get_result()->fetch_object();
$itemArray = array($productDetails->id=>array('product_title'=>$productDetails->title, 'id'=>$productDetails->id, 'quantity'=>$quantity, 'price'=>$productDetails->offer_price));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productDetails->id,array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productDetails->id == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $quantity;
}
}
} else {
$_SESSION["cart_item"] = $_SESSION["cart_item"] + $itemArray;
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productId == $v['id'])
unset($_SESSION["cart_item"][$k]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
I am completely new to PHP. I tried searching alot but didn't find what I wanted. now the problem is while loading the page, the page is not displaying, its still loading. can anyone tell me how to fix it. any help would be great.

Filtering rows of php generated table using a select dropdown

I have a table that looks like this Table
The table rows are filled from a database. Here is the following code.
<select name="tipo" id="tipoProveedor" data-col="6" class="form-control">
<option value="todos">Todos</option>
<?php
$tipo_values = array(
'B'=>'Bienes',
'S'=>'Servicios',
);
foreach($tipo_values as $value => $display_text)
{
$selected = ($value == $this->input->post('tipo')) ? ' selected="selected"' : "";
echo '<option value="'.$value.'" '.$selected.'>'.$display_text.'</option>';
}
?>
</select>
<table id="table" class="table table-striped">
<thead>
<tr>
<th>Razón Social</th>
<th>Dirección</th>
<th>Contacto</th>
<th>Teléfono Fijo</th>
<th>Teléfono Móvil</th>
<th>Correo Electrónico</th>
<th>Tipo</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach($listaproveedor as $p){ ?>
<tr class="<?php echo $p['tipo']; ?>">
<td><?php echo $p['razonSocial']; ?></td>
<td><?php echo $p['direccion']; ?></td>
<td><?php echo $p['nombre1']; ?></td>
<td><?php echo $p['telefonoFijo1']; ?></td>
<td><?php echo $p['telefonoMovil1']; ?></td>
<td><?php echo $p['correoElectronico1']; ?></td>
<td><?php echo $p['tipo']; ?></td>
<td>
<a title="Editar" href="<?php echo site_url('proveedor/edit/'.$p['id']); ?>" class="btn btn-info btn-xs"><span class="fa fa-pencil"></span></a>
<a title="Eliminar" href="<?php echo site_url('proveedor/remove/'.$p['id']); ?>" class="btn btn-danger btn-xs"><span class="fa fa-trash"></span></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Above the table there is a select dropdown to filter the rows.
It has 3 options
Todos (All)
Bienes
Servicios
The filtering should be applied to the column Tipo (the one circled in red in the picture).
Option "Todos" should show all the rows
Option "Bienes" should show all the rows with the letter B in "Tipo" column.
Option "Servicios" should show all the rows with the letter S in "Tipo" column
EDIT: I edited the html code for the table and added the following script.
Now it works!
$('.familiaOcultar').addClass('collapse');
$('#tipoProveedor').change(function(){
$('.familiaOcultar').collapse('hide');
$('.familiaOcultar.B').hide();
var val = $(this).val();
if (val == "todos"){
$('.B td').show();
$('.S td').show();
$('.familiaOcultar.B').hide();
} else if (val == 'B'){
$('.B td').show();
$('.S td').hide();
$('.familiaOcultar.B').show();
} else {
$('.B td').hide();
$('.S td').show();
$('.familiaOcultar.B').hide();
}
});

jQuery table td click not working properly

I have php code which generates a table. Inside td I have inserted img. When user click on td then it changes img inside it. But it is not working properly. Only even rows td element changes image while odd rows doesn't. Below is my code:
<div class="row" id="atten_list">
<div class="col-sm-offs-3 col-md1-6">
<table class="table table-bordered">
<thead>
<tr>
<td><?php echo get_phrase('roll');?></td>
<td><?php echo get_phrase('name');?></td>
<td><?php echo get_phrase('1');?></td>
<td><?php echo get_phrase('2');?></td>
<td><?php echo get_phrase('3');?></td>
<td><?php echo get_phrase('4');?></td>
<td><?php echo get_phrase('5');?></td>
<td><?php echo get_phrase('6');?></td>
<td><?php echo get_phrase('7');?></td>
<td><?php echo get_phrase('8');?></td>
<td><?php echo get_phrase('9');?></td>
<td><?php echo get_phrase('10');?></td>
<td><?php echo get_phrase('11');?></td>
<td><?php echo get_phrase('12');?></td>
<td><?php echo get_phrase('13');?></td>
<td><?php echo get_phrase('14');?></td>
<td><?php echo get_phrase('15');?></td>
<td><?php echo get_phrase('16');?></td>
<td><?php echo get_phrase('17');?></td>
<td><?php echo get_phrase('18');?></td>
<td><?php echo get_phrase('19');?></td>
<td><?php echo get_phrase('20');?></td>
<td><?php echo get_phrase('21');?></td>
<td><?php echo get_phrase('22');?></td>
<td><?php echo get_phrase('23');?></td>
<td><?php echo get_phrase('24');?></td>
<td><?php echo get_phrase('25');?></td>
<td><?php echo get_phrase('26');?></td>
<td><?php echo get_phrase('27');?></td>
<td><?php echo get_phrase('28');?></td>
<td><?php echo get_phrase('29');?></td>
<td><?php echo get_phrase('30');?></td>
<td><?php echo get_phrase('31');?></td>
</tr>
</thead>
<tbody>
<?php
$students = $this->db->get_where('student' , array('class_id'=>$class_id))->result_array();
foreach($students as $row):
?>
<tr class="gradeA" id="adata">
<td><?php echo $row['roll'];?></td>
<td><?php echo $row['name'];?></td>
<?php
for($i=1; $i<=31; $i++){ ?><?php
$datea = $i;
$full_date = $year.'-'.$month.'-'.$datea;
//inserting blank data for students attendance if unavailable
$verify_data = array( 'student_id' => $row['student_id'],
'date' => $full_date);
$query = $this->db->get_where('attendance' , $verify_data);
if($query->num_rows() < 1){
$this->db->insert('attendance' , $verify_data);}
//showing the attendance status editing option
$attendance = $this->db->get_where('attendance' , $verify_data)->row();
$status = $attendance->status;
$id = $attendance->attendance_id;
?>
<?php if ($status == 1):?>
<td align="center" id="status" title="<?php echo $id; ?>">
<img src="<?php echo base_url("/assets/images/present.png"); ?>" alt="StackOverflow" title="StackOverflow is the best!" />
</td>
<?php endif;?>
<?php if ($status == 2):?>
<td align="center" id="status" title="<?php echo $id; ?>">
<img src="<?php echo base_url("/assets/images/absent.png"); ?>" alt="StackOverflow" title="StackOverflow is the best!" />
</td>
<?php endif; ?><?php }?><?php $this->db->where('class_id',$class_id);
$this->db->from('student');
$nofs = $this->db->count_all_results(); ?>
<!-- Script for changing image on td click -->
<script>
$("table tbody tr#adata td#status").click(function() {
var img = $(this).find("img")[0];
if(img.src == '<?php echo base_url("/assets/images/present.png"); ?>'){
img.src = img.src.replace('<?php echo base_url("/assets/images/present.png"); ?>', '<?php echo base_url("/assets/images/absent.png"); ?>');
}
else if(img.src == '<?php echo base_url("/assets/images/absent.png"); ?>'){ img.src = img.src.replace('<?php echo base_url("/assets/images/absent.png"); ?>', '<?php echo base_url("/assets/images/present.png"); ?>');
}else{}
}); </script>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
</div>
Try this syntax ,
$('table').on('click','td',function(){
//Your code
});
Also use 'class' since 'ID' attrib should be unique

Categories

Resources