making a print button - php

I am trying to make a button that would print my database in a contact list format. Where they would be grouped under the Departments that the contacts are added to on the php forms. Like this: (BELOW IS MY CODE FOR THE CONTACT PAGE)
ADMIN
George George#snakdnasd 0282738292392
8432
IT DEPARTMENT
Tyler tyler#askdnasdksan 7492823829292 8321
I have looked around and have tried the usual commands like window.print and just print() but it just prints the actual php page.
<?php
require_once"connection.php";
$contacts = array();
$all_contacts = "select * from contacts where contact_status = '1'";
$sql_all_contacts = $conn->query($all_contacts);
$total_contacts = $sql_all_contacts->num_rows;
while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
$contacts[] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
<?php include"includes/head.inc"; ?>
</head>
<body>
<div class="wrapper">
<!-- header section -->
<?php include"includes/header.inc"; ?>
<!-- content section -->
<div class="content">
<div class="floatl"><h1><?php echo $total_contacts ?> Contact(s) Total</h1></div>
<a class="floatr" href="insert_contact.php"><input class="cancel_contact_button" type="button" value="New Contact"></a>
<div class="clear"></div>
<hr class="pageTitle">
<table border ="1" style="width:100%" id="contactsTable" class="display">
<thead>
<tr align="left">
<th>Name:</th>
<th>Email:</th>
<th>Department:</th>
<th>Extension:</th>
<th>Cellphone:</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact) {?>
<tr>
<td><?php echo $contact["name"];?></td>
<td><?php echo $contact["email"]; ?></td>
<td><?php echo $contact["department"]; ?></td>
<td><?php echo $contact["extension"]; ?></td>
<td><?php echo $contact["cellphone"]; ?></td>
<td><i class="fa fa-pencil"></i> | <i class="fa fa-trash-o"></i></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</body>
</html>

Related

Responsive table won't react to PHP, but it reacts to HTML

I put PHP codes inside my HTML table. This makes table don't work again. It search status will not work, and show entries won't change, beside that, after what I get 11 datas inside the table, it will show only 10 in it and won't create page two.
But, If I put manually with HTML, it works fine. What's the problem? If I create manually inside HTML 's it works fine.
include_once 'info.php';
$query = $config -> prepare("SELECT `banID`, `user_banned`, `ban_reason`, `ban_time`, `user_banner`, `ban_timestamp` FROM `samp_ban` ORDER BY `banID` DESC LIMIT 10");
if($query -> execute())
{
$query_results = $query->fetchAll();
} foreach( $query_results as $query_result ) {
if($query_result["ban_time"] == 0) { $query_result["ban_time"] = "Permanent"; }}
?>
<div class="contentpanel">
<ol class="breadcrumb breadcrumb-quirk">
<li><i class="fa fa-home mr5"></i> Home</li>
<li>Non Grata List</li>
</ol>
<div class="panel">
<div class="panel-heading">
<h4 class="panel-title">Non GrataLIST</h4>
<p>Here's the non grata list.</p>
</div>
<div class="panel-body">
<div class="table-responsive">
<table id="dataTable1" class="table table-bordered table-striped-col">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>REASON</th>
<th>TIME</th>
<th>ADMIN</th>
<th>DATA</th>
</tr>
</thead>
<?php foreach( $query_results as $query_result ) {
if($query_result["ban_time"] == 0) { $query_result["ban_time"] = "Permanent"; } ?>
<tbody>
<tr>
<td><?php echo $query_result["banID"]; ?></td>
<td><?php echo $query_result["user_banned"]; ?></td>
<td><?php echo $query_result["ban_reason"]; ?></td>
<td><?php echo $query_result["ban_time"]; ?> </td>
<td><?php echo $query_result["user_banner"]; ?></td>
<td><?php echo $query_result["ban_timestamp"]; ?></td>
</tr><?php } ?>
</tbody>
</table>
</div>
</div>
</div><!-- panel -->
</div><!-- contentpanel -->
The only thing I can see that might be problematic with this code is that you open the table body tag in the foreach loop, but close it outside of the loop. So I would suggest opening you table body tag before the foreach loop:
<tbody>
<?php
foreach( $query_results as $query_result ) {
if($query_result["ban_time"] == 0) { $query_result["ban_time"] = "Permanent"; } ?>
<tr>
<td><?php echo $query_result["banID"]; ?></td>
<td><?php echo $query_result["user_banned"]; ?></td>
<td><?php echo $query_result["ban_reason"]; ?></td>
<td><?php echo $query_result["ban_time"]; ?> </td>
<td><?php echo $query_result["user_banner"]; ?></td>
<td><?php echo $query_result["ban_timestamp"]; ?></td>
</tr>
<?php } ?>
</tbody>

Grouping table with data headers

Hi everyone im attempting to group my data table to display the users listed by department eg:
IT DEPARTMENT
Name Phone Email Extension
Bob 99393 ksand#sda 8484
but mine is displayed as this:
Name Phone Email Extension Department
Bob 99393 ksand#sda 8484 IT
I did some reading on datatable grouping but coulodnt find anything that explains how it works and how to implement it.
My code is below. If someone could point me to a link to help that would be greatly appreciated, thank you.
<?php
require_once"connection.php";
$contacts = array();
$all_contacts = "select * from contacts where contact_status = '1'";
$sql_all_contacts = $conn->query($all_contacts);
$total_contacts = $sql_all_contacts->num_rows;
while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
$contacts[] = $row;
}
?>
<html>
<head>
<?php include"includes/head.inc"; ?>
</head>
<body>
<div class="wrapper">
<!-- header section -->
<?php include"includes/header.inc"; ?>
<!-- content section -->
<div class="content">
<div class="floatl"><h1><?php echo $total_contacts ?> Contact(s) Total</h1></div>
<a class="floatr" href="insert_contact.php"><input class="cancel_contact_button" type="button" value="New Contact"></a> $
<div class="clear"></div>
<hr class="pageTitle">
<table border ="1" style="width:100%" id="contactsTable" class="display">
<thead>
<tr align="left">
<th>Name:</th>
<th>Email:</th>
<th>Department:</th>
<th>Extension:</th>
<th>Cellphone:</th>
<th>Actions</th>
</tr>
</thead>
<tr align="left">
<th>Name:</th>
<th>Email:</th>
<th>Department:</th>
<th>Extension:</th>
<th>Cellphone:</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact) {?>
<tr>
<td><?php echo $contact["name"];?></td>
<td><?php echo $contact["email"]; ?></td>
<td><?php echo $contact["department"]; ?></td>
<td><?php echo $contact["extension"]; ?></td>
<td><?php echo $contact["cellphone"]; ?></td>
<td><a href="update_contact.php?id=<?php echo $contact["contact_id"]; ?>"><i class="fa fa-p$
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Since you're already doing a while loop to add the db rows into an array, you could use the Department as the key. This would group the contacts by department, and you could run nested foreach loops.
while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
$contacts[$row['Department']][] = $row;
}
The foreach loops:
foreach ($contacts as $department => $contactGroup) {
echo $department;
foreach($contactGroup as $contact) {
// output your contact here
}
}

pdf download button not working in php

I have got an ebooks table in the database where path of pdf is defined.What I want is, when a user clicks on the download button, pdf get download.But the download button is not working and I'm also not getting any error from Php.Please help! Thank you.
Code:
<div id="data">
<?php
if (isset($_POST['submit'])) {
$title = $_POST['title'];
$name = $_POST['name'];
$count = 1;
$query = "SELECT * FROM ebooks WHERE $title LIKE '%".$name."%'";
$display = $obj->run_query($query);
?>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr align="center">
<td>No.</td>
<td>Title</td>
<td>Author</td>
<td>Publication</td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php
foreach ($display as $key) { ?>
<tr align="center">
<td><?php echo $count++;?></td>
<td><?php echo $key['ebook_title'];?></td>
<td><?php echo $key['ebook_author'];?></td>
<td><?php echo $key['ebook_publication'];?></td>
<td><a style="color:white;" href="/admin/ebooks/<?php echo $key['ebook_url'];?>" class="btn btn-round btn-primary">Download</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php } ?>
</div>
</div>
You have to add download before ending of anchor tag try this :
<a style="color:white;" href="/admin/ebooks/<?php echo $key['ebook_url'];?>" class="btn btn-round btn-primary" download>Download</a>

Slide Up/Down does not work properly

Hi I want to display some search results using tables. In my code the basic details are displayed in a one table and if we want to see more details you have to click view more option. The hide details are also be displayed in a separate table.But I have a problem in my code. If I have more than one search result in my database the details regards to first person are displayed as I want. that means it I want to see more details i want to click on view more and it slide down the hide table. But the rest of search result displays the hidden tables as well. and also if i click the view more option of the rest, it again slide down the hidden table of very first person's table. here I have attached my code. Could you please help me to solve this issue?
<body>
<div class="container"> <!-- container -->
<div class="row" id="main-content">
<div class="span8">
<div class="well"> <!-- -start of well class -->
<?php
dbConnect();
$district = $_POST['district'];
$category = $_POST['catID'];
$subject = $_POST['subID'];
//get the category name
$get_cat_name = mysql_query("SELECT catName FROM tutor_category WHERE catID='{$category}' ");
while($row = mysql_fetch_assoc($get_cat_name ))
{
$cat_name = $row['catName'];
}
//get the subject name
$get_sub_name = mysql_query(" SELECT subName FROM tutor_subjects WHERE catID='{$category}' AND subID='{$subject}'");
while($row = mysql_fetch_assoc($get_sub_name ))
{
$sub_name = $row['subName'];
}
?>
<!-- ****************** Heading Table *******************-->
<table class="table table-bordered">
<tr>
<th> <?php echo $district." District - ". $cat_name ." - ". $sub_name ?> </th>
</tr>
</table>
<!-- ****************** end of heading table *******************-->
<?php
//get tutor IDs
$get_tutor_id = mysql_query(" SELECT DISTINCT tutorID FROM tutor_register_subjects WHERE district='{$district}' AND catID='{$category}' AND subID='{$subject}' ");
while($row = mysql_fetch_assoc($get_tutor_id)) // first
{
$tutor_id = $row['tutorID'];
$query = mysql_query(" SELECT * FROM tutor WHERE tutorID='{$tutor_id}' ");
while($row = mysql_fetch_assoc($query))
{ // second
$fname = $row['firstName'];
$lname = $row['lastName'];
$nic = $row['nic'];
$gender = $row['gender'];
$education = $row['education'];
$address = $row['address'];
$profession= $row['profession'];
$email = $row['email'];
$cnum = $row['contactNum'];
$avatar = $row['avatar'];
} // second
?>
<div class="control-group">
<!-- basic details -->
<table class="table table-bordered">
<tr>
<td width="120" rowspan="4"><?php echo "<img src='uploads/".$avatar."' height='120' width='100'>"?></td>
<th width="120">Name</th>
<td><?php echo $fname." ". $lname?></td>
</tr>
<tr>
<th>NIC</th>
<td><?php echo $nic ?></td>
</tr>
<tr>
<th>Gender</th>
<td><?php echo $gender ?></td>
</tr>
<tr>
<td colspan="2"><a class="more">View More</a> <a class="less">View Less</a></td>
</tr>
</table>
</div>
<!-- end of basic details -->
<!-- more details-->
<div class="control-group" id="more">
<table class="table table-bordered">
<tr>
<th>Contact Number</th>
<td><?php echo $cnum ?></td>
</tr>
<tr>
<th>Email</th>
<td><?php echo $email ?></td>
</tr>
<tr>
<th>Address</th>
<td><?php echo $address ?></td>
</tr>
<tr>
<th>Education</th>
<td><?php echo $education ?></td>
</tr>
<tr>
<th>Work Place</th>
<td><?php echo $profession ?></td>
</tr>
</table>
</div>
<!-- end of more details-->
<legend></legend>
<?php
} // first
?>
</div> <!-- -start of well class -->
</div> <!-- end span12 class -->
</div> <!-- end of row class -->
</div> <!-- container -->
<!-- view more script-->
<script src="../js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#more").hide();
$(".less").click(function(){
$("#more").slideUp(1000);
});
$(".more").click(function(){
$("#more").slideDown(1000);
});
});
</script>
</body>
What's happening is that $("#more") selects every div with ID more, and changes it
Give each .less and .more something which is unique to the current user:
<div class="less" data-id="<?php echo $person_id; ?>">Show less</div>
<div class="more" data-id="<?php echo $person_id; ?>">Show more</div>
Then in your #more, where the actual data is, do something about the same
<div id="more-<?php echo $person_id; ?>">More information here</div>
Now in your jQuery, select the correct id:
$(".less").click(function(){
$("#more-"+ $(this).attr('data-id')).slideUp(1000);
});
$(".more").click(function(){
$("#more-"+ $(this).attr('data-id')).slideDown(1000);
});
What you are effictively doing now is telling jQuery to select div with e.g. id="more-15" in case you clicked on the .more with attribute data-id="15", thus selecting the correct div :)
Note: You don't have to use divs to be able to do this. This would also solve invalid HTML because you got tons of elements with the same id's
See: http://ejohn.org/blog/html-5-data-attributes/

PHP: Form echo's id in source but shows first id in link for all rows

I'm having an issue with a pop up form that has a field where the user needs to enter some info regarding a row of data.
The stuff he/she enters needs to be inserted into the table which I will do a Update query... But my issue is that the form echo's the id from the database in the source code but it doesn't show when I hover over the button.
After I press the button it only executes the query for the first id. And the others show only the 2nd and so forth until they are removed one by one. I don't know what I'm doing wrong:
<?php include_once('classes/profile.class.php');?>
<?php include_once('header.php');?>
<h1>
<?php _e('Traffic Fines Adjudication'); ?>
</h1>
<br>
<div class="tabs-left">
<?php
$result = "SELECT * FROM login_fines_adjudicated WHERE active = 0 ORDER BY date_issued";
$stmt = $generic->query($result);
//this function will take the above query and create an array
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
//do nothing
//with the array created above, I can create variables (left) with the outputted array (right)
$id = $row['id'];
$date = $row['date_added_db'];
$date_issued = $row['date_issued'];
$time_arrive = $row['time_arrived'];
$time_depart = $row['time_departed'];
$ref = $row['reference_code'];
$reason = $row['violation_reason'];
$location = $row['location'];
$bay = $row['bay_number'];
$licence = $row['licence'];
$punit = $row['parking_unit'];
$value = $row['value'];
$operator = $row['operator'];
$operator_id = $row['operator_id'];
//If date field is empty display nothing! Or sentence...
if(!empty($date))
{
?>
<table class="table">
<thead>
<tr>
<th>Photo</th>
<th>Date Added</th>
<th>Date Issued</th>
<th>Time Arrived</th>
<th>Time Departed</th>
<th>Reference Code</th>
<th>Violation Category</th>
<th>Location</th>
<th>Bay Number</th>
<th>Licence Plate</th>
<th>Parking Unit</th>
<th>Amount</th>
<th>Operator Name</th>
<th>Operator ID</th>
<th>Action</th>
</tr>
</thead>
<tr>
<td><img class="photo" src="photos/no-available.jpg" /></td>
<td><?php echo $date; ?><br /></td>
<td><?php echo $date_issued; ?></td>
<td><?php echo $time_arrive; ?></td>
<td><?php echo $time_depart; ?></td>
<td><?php echo $ref; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $location; ?></td>
<td><?php echo $bay; ?></td>
<td><?php echo $licence; ?></td>
<td><?php echo $punit; ?></td>
<td><?php echo $value; ?></td>
<td><?php echo $operator; ?></td>
<td><?php echo $operator_id; ?></td>
<td>
<form action="approve.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<p>Approve</button></p>
</form>
<div id="reject-form" class="modal hide fade">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><?php _e('Reject Reason'); ?></h3>
</div>
<div class="modal-body">
<div id="message"></div>
<form action="reject.php" method="post" name="rejectform" id="rejectform" class="form-stacked rejectform normal-label">
<div class="controlgroup rejectcenter">
<div class="control">
<input id="reject" name="reject" type="text"/>
</div>
</div>
<input type="hidden" name="delete_id" value="<?php echo $id; ?>" />
</form>
</div>
<div class="modal-footer">
<?php _e('Submit'); ?></button>
<p class="pull-left"><?php _e('Please give a short reason for rejecting.'); ?></p>
</div>
</div>
</form>
<p><a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1><button url="#" class="btn btn-primary">Reject</button></a></p>
</td>
</tr>
</table>
<?php
}
}
?>
</div>
<?php include ('footer.php'); ?>
Any help would be appreciated!
The problem is that you have many elements with the same id attribute. Genarally, it is a really bad practice to have more than elements with the same id, not to mention it does not conform to the HTML Spec, according to which "id = [...] This name must be unique in a document.". Therefore, you are strongly advised to fix all IDs mking sure it is unique (e.g. appending the current entry's ID).
What causes the specific problem you described in your question is this:
You define for every table a pop-up dialog (div), with id reject-form.
Then, you implement the "reject"-button as a link with href="#reject-form".
So, when you click on any "reject"-button, the user is taken to the first element found in the DOM with id="reject-form" (which is always the dialog for the first entry).
In order to fix that, you can append each entry's ID in the id attributes in two locations: the pop-up dialog and the "reject"-button:
// Replace that:
<div id="reject-form" class="modal hide fade">
// with this:
<div id="reject-form-<?php echo $id; ?>" class="modal hide fade">
// Replace that:
<a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1>
// with this:
<a data-toggle="modal" href="#reject-form-<?php echo $id; ?>" id="rejectlink" tabindex=-1>

Categories