How do add span on my table echo? - php

Hello i just new to coding but i don't know how to add span on my table on my echo every-time keep moving the table i need help how to do syntax on my code
here is my original code for my table
<td><span id="name<?php echo $row['id']; ?>"><?php echo $row['name']; ?></td>
<td><span id="email<?php echo $row['id']; ?>"><?php echo $row['email']; ?></td>
<td><span id="username<?php echo $row['id']; ?>"><?php echo $row['username']; ?></td>
<td><span id="level<?php echo $row['id']; ?>"><?php echo $row['password']; ?></td>
this my other table for my edit but everytime i press the button it does not display any value in my edit
<td>'.$row["name"].'</td>
<td>'.$row["username"].'</td>
<td>'.$row["email"].'</td>
<td>'.$row["level"].'</td>
<td><a style="cursor:pointer;" class="btn btn-info edit" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-edit" aria-hidden="true" ></span></a> || <a style="cursor:pointer;" class="btn btn-danger delete" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
what im trying to get how do i put id on this echo output to direct it to my button showing the value of id return all the value on that id.
while($row = mysqli_fetch_array($result)) {
$output .= '
<tr>
<td>'.$row["name"].'</td> <-- how do i add <span>,put id on this rows
<td>'.$row["username"].'</td> or how to add id on this table
<td>'.$row["email"].'</td>
<td>'.$row["level"].'</td>
<td>
<a style="cursor:pointer;" class="btn btn-info edit" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-edit" aria-hidden="true" ></span></a> || <a style="cursor:pointer;" class="btn btn-danger delete" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
</td>
</tr>';
}
echo $output;
}

Here's your $output with <span>s in them:
$output .= '
<tr>
<td><span id="name'.$row["id"].'">'.$row["name"].'</span></td>
<td><span id="username'.$row["id"].'">'.$row["username"].'</span></td>
<td><span id="email'.$row["id"].'">'.$row["email"].'</span></td>
<td><span id="level'.$row["id"].'">'.$row["level"].'</span></td>
<td>
<a style="cursor:pointer;" class="btn btn-info edit" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-edit" aria-hidden="true" ></span></a> || <a style="cursor:pointer;" class="btn btn-danger delete" data-id="'.$row['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
</td>
</tr>';

Related

Problems displaying actions in a listing table

I have a problem in the column referring to the column Actions of my listings table. The error is as follows.
Parse error: syntax error, unexpected token "<" in C:\xampp\htdocs\mangas\view\form_select_manga.php on line 42`
This error is caused by:
<a class="btn btn-info" title="Edit" href="edit_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-edit"></i> Edit</a>
<a class="btn btn-danger" title="Delete" href="delete_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-trash"></i> Delete</a>
Only the two action buttons have this problem, I have difficulties to adapt the table without causing an error.
<div class="container" style="margin-top: 40px;">
<h4 class="text-center">Manga list</h4>
<br>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
<th>Volumes</th>
<th>Discont</th>
<th>Collection value</th>
<th>Registration date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
require_once "../model/MangaDAO.php";
$manga = new MangaDAO();
foreach ($manga->selectAllMangas() as $value){
echo '<tr>';
echo '<td title="'.$value['id_manga'].'">'.$value['id_manga'].'</td>';
echo '<td title="'.$value['title'].'">'.$value['title'].'</td>';
echo '<td title="'.$value['publisher'].'">'.$value['publisher'].'</td>';
echo '<td title="'.$value['volumes'].'">'.$value['volumes'].'</td>';
echo '<td title="R$'.$value['discont'].'">R$'.$value['discont'].'</td>';
echo '<td title="R$'.$value['value'].'">R$'.$value['value'].'</td>';
echo '<td title="'.date('d/m/Y H:i:s', strtotime($value['registration_date'])).'">'.
date('d/m/Y H:i:s', strtotime($value['registration_date'])).'</td>';
<a class="btn btn-info" title="Edit" href="edit_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-edit"></i> Edit</a>
<a class="btn btn-danger" title="Delete" href="delete_manga.php?id_manga=<?php echo $id_manga ?>" role="button"><i class="fa fa-trash"></i> Delete</a>
echo '</tr>'; echo '</p>';
}
?>
</tbody>
</table>
</div>
<?php
require_once "../model/MangaDAO.php";
$manga = new MangaDAO();
foreach ($manga->readAllMangas() as $value){
echo '<tr>';
echo '<td title="'.$value['id_manga'].'">'.$value['id_manga'].'</td>';
echo '<td title="'.$value['title].'">'.$value['title'].'</td>';
echo '<td title="'.$value['publisher'].'">'.$value['publisher'].'</td>';
echo '<td title="'.$value['volumes'].'">'.$value['volumes'].'</td>';
echo '<td title="R$'.$value['discount'].'">R$'.$value['discount'].'</td>';
echo '<td title="R$'.$value['value'].'">R$'.$value['value'].'</td>';
echo '<td title="'.date('d/m/Y H:i:s', strtotime($value['registering_date'])).'">'.
date('d/m/Y H:i:s', strtotime($value['registering_date'])).'</td>';
echo '<td>';
echo '<a class="btn btn-info" title="Update '.$value['title'].'"'.'href="/mangas/view/form_update_manga.php?id_manga='.$value['id_manga'].'"role="button" ><i class="fa fa-edit"></i> Update</a>';
echo " ";
echo '<a class="btn btn-danger" title="Delete '.$value['title'].'"'.'href="/mangas/controller/delete_manga.php?id_manga='.$value['id_manga'].'"role="button"><i class="fa fa-trash"></i> Delete</a>';
echo '</td>';
echo '</tr>';
}
?>

Ajax table doesn't show anything

I'm creating a table using ajax and php but there's one problem, the table isn't showing in my div. I'm really new to ajax so I don't really fully understand it yet.
Here's my div:
<div class="body" id="live-data">
</div>
Here's the ajax code:
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
});
And here's fetch.php:
<?php
include('../global/db.php');
$output = '';
$sql ="SELECT * FROM students WHERE status = '0' AND stud_grade = '$level_id' ORDER BY date_enrolled DESC";
$result = mysqli_query($db, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>';
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
';
}
}
else {
$output .= '
<tr>
<td colspan="12">Data not Found</td>
</tr>';
}
$output .= '
</tbody>
</table>
</div>';
echo $output;
?>
It would great if anyone could help because I just don't know why it doesn't work
Edit:
I've changed the code so it returns the data in the console tab and here's what shows up:
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
</tbody>
</table>
</div>
So it clearly returns the correct data but it just doesn't show up in the live-data div
Maybe take a look in your html where is your div. Your div id says "live-data" and in the ajax code you mentioned to fetch data for div id "#live_data" instead of "#live-data".
Maybe changing them for same name can solve your problem. I would use for thr div id and in the ajax same id names like "#liveData".
Example (same code, just edited to the right IDs, compare with your original):
Your HTML div
<div class="body" id="liveData">
</div>
Your ajax code
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#liveData').html(data);
}
});
}
fetch_data();
});
Just append the correct Id of the div tag in sucess function

How to update database from Menu?

<div class="container">
<h3 class="text-center">Hotel/Admin Joint Panel</h3>
<table class="table table-bordered">
<tr>
<th>Food_ID</th>
<th>Cuisines</th>
<th >Description</th>
<th >Price</th>
<th >Image</th>
<th >Date</th>
<th width="50px">Action</th>
</tr>
<?php
include_once 'mainclass.php';
$confirmThread = new menuDatabase();
$sql = "SELECT * FROM gallery";
$users = mysqli_query($confirmThread->getCon(),$sql);
while($user = $users->fetch_assoc()){
?>
<tr id="<?php echo $user['idGallery'] ?>" >
<td ><?php echo $user['idGallery'] ?></td>
<td><?php echo $user['titleGallery'] ?></td>
<td><?php echo $user['descGallery'] ?></td>
<td><?php echo $user['price'] ?></td>
<!-- <td><?php echo ("data:image/jpeg;base64,".base64_encode($user['image'] )) ?></td> -->
<td><?php echo '<img src="data:image/jpeg;base64,'.base64_encode($user['image'] ).'">'
?></td>
<td width="100px"><?php echo $user['samaye'] ?></td>
<td>
<button class="btn btn-danger btn-sm remove">Delete</button>
<button style="margin-top: 2px" id="createOnly" class="btn btn-danger"> Create Menu</button>
<button style="margin-top: 2px" id="updateOnly" id="hi" class="btn btn-danger btn-sm "> <a class="update" href="upload.php">Update</a></button>
</td>
</tr>
<?php } ?>
</table>
</div> <!-- container / end -->
I wanted to update the database using an update button which is in Menu.php. What I wanted is, when I clicked on the update button(in menu.php) it should grab the food_id(primary key) and remember this for Upload.php, from where I am doing real updating by getting all new values.
You can change the following statement to
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<button style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm"> <a class = "update" href = "upload.php">Update</a></button>
</td>
this. This will act as a form (Only for the update button) and you can get these values to your update.php. The id that you need is hidden. From update.php you can do what you need to.
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<form method = "post" action = "upload.php">
<input type = "hidden" name="food_id" value="<?php echo $user['idGallery']; ?>"/>
<button type = "submit" style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm">Update</button>
</form>
</td>
Or else you can do this by using Query Parameter like this,
<td>
<button class = "btn btn-danger btn-sm remove">Delete</button>
<button style = "margin-top: 2px" id = "createOnly" class = "btn btn-danger"> Create Menu</button>
<button style = "margin-top: 2px" id = "updateOnly" id = "hi" class = "btn btn-danger btn-sm"> <a class = "update" href = "upload.php?foodID=<?php echo $user['idGallery']; ?>">Update</a></button>
</td>
and from your update.php you can get the result like this,
$foodID = $_GET["foodID"];
Hope this helps you!

How to write PHP code inside a variable output?

I have a user table where I have to display a delete button and I need to make sure the current user cannot delete themselves.
In order to do that, I wrote some code as shown below, but it doesn't work.
The user table appears with the delete button in all the users, but I need to make sure the current user cannot delete their own user account.
while($row = mysqli_fetch_array($result)) {
$output .='<tr>
<td>'.$row["user_id"].'</td>
<td>'.$row["fullname"].' </td>
<td>'.$row["user_role"].'</td>
<td>'.$row["username"].'</td>
<td>'.$row["password"].'</td>
<td>'.$row["branch_id"].'</td>
<td>'.$row["registered_date"].'</td>
<td>
<button id="'.$row["user_id"].'" class="btn btn-warning btn-xs edit_data"><i class="fa fa-edit "></i></button>
if('.$_SESSION['username'].'!='.$row["username"].') {
<a id="'.$row["user_id"].'" class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a> } </td>
</tr>';
}
$output .= '</table>';
echo $output;
You can not execute PHP in a string, you need to split your string creation.
while ($row = mysqli_fetch_array($result)) {
$output .='<tr>
<td>'.$row["user_id"].'</td>
<td>'.$row["fullname"].' </td>
<td>'.$row["user_role"].'</td>
<td>'.$row["username"].'</td>
<td>'.$row["password"].'</td>
<td>'.$row["branch_id"].'</td>
<td>'.$row["registered_date"].'</td>
<td>
<button id="'.$row["user_id"].'" class="btn btn-warning btn-xs edit_data"><i class="fa fa-edit "></i></button>';
if ($_SESSION['username'] != $row["username"]) {
$output .= '<a id="'.$row["user_id"].'" class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a>';
}
$output .= '</td></tr>';
}
$output .= '</table>';
echo $output;
Since you output your variable right after the loop - you can do the following
<table>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?=$row['user_id']; ?></td>
<td><?=$row['fullname']; ?></td>
<td><?=$row['user_role']; ?></td>
<td><?=$row['username']; ?></td>
<td><?=$row['password']; ?></td>
<td><?=$row['branch_id']; ?></td>
<td><?=$row['registered_date']; ?></td>
<td>
<?php
if($_SESSION['username'] != $row['username'])
{
?>
<a id="<?=$row['user_id']; ?>" class="btn btn-danger btn-xs delete"><i class="fa fa-times"></i></a>
<?php
}
?>
</td>
</tr>
<?php
}
?>
</table>

how to use div tag inside <td> in php code

I want to know how should we use div tags inside a table in PHP code. This is my code. Please tell me how to use it properly.
<tbody>
<?php
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<tr>
<td>{$row['id']}</td>
<td><img width='90px' height='90px' src='imageView.php?id=".$row["id"]."' /> </td>
<td>{$row['item_name']}</td>
<td>{$row['description']}</td>
<td>{$row['quantity']}</td>
<td>echo '<div class="col-md-1 col-sm-3 col-xs-6 c-cart-qty">
<div class="c-input-group c-spinner">
<input type="text" class="form-control c-item-1" value="1">
<div class="c-input-group-btn-vertical">
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-up"></i>
</button>
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-down"></i>
</button>
</div>
</div>
</div>
</td>';
<td> <button>submit</button> </td>
</tr>";
}?>
</tbody>
I have tried using inverted comma's and braces but still, it shows error. What needs to be done here?
You can write the div inside table. Don't put all table inside php tag. remove html outside the <?php ?> php code. after that whenever you want to echo any value just used the php.
Customize your code like below,
<table>
<tbody>
<?php
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><img width='90px' height='90px' src='imageView.php?id=<?php echo $row["id"]; ?>' /> </td>
<td><?php echo $row['item_name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td>
<div class="col-md-1 col-sm-3 col-xs-6 c-cart-qty">
<div class="c-input-group c-spinner">
<input type="text" class="form-control c-item-1" value="1">
<div class="c-input-group-btn-vertical">
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-up"></i>
</button>
<button class="btn btn-default" type="button" data_input="c-item-1">
<i class="fa fa-caret-down"></i>
</button>
</div>
</div>
</div>
</td>
<td> <button>submit</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
Within a double quote you can use a single quote
<tbody>
<?php
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<tr>
<td>{$row['id']}</td>
<td><img width='90px' height='90px' src='imageView.php?id=".$row["id"]."' /> </td>
<td>{$row['item_name']}</td>
<td>{$row['description']}</td>
<td>{$row['quantity']}</td>
<td><div class='col-md-1 col-sm-3 col-xs-6 c-cart-qty'>
<div class='c-input-group c-spinner'>
<input type='text' class='form-control c-item-1' value='1'>
<div class='c-input-group-btn-vertical'>
<button class='btn btn-default' type='button' data_input='c-item-1'>
<i class='fa fa-caret-up'></i>
</button>
<button class='btn btn-default' type='button' data_input='c-item-1'>
<i class='fa fa-caret-down'></i>
</button>
</div>
</div>
</div>
</td>;
<td> <button>submit</button> </td>
</tr>";
}?>
</tbody>

Categories