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
}
}
Related
I would like to ask on how to make my table to appear as following:
But I only manage to get my table to appear like this:
As you can see, the table is separated according to the different year or semester of when they stored their stuff. It is basically a history storage of that particular person. Unfortunately, I don't know how to make the generated table to attach together for the one with same year and semester instead of separating it. Even the numbering is affected. Below is the code that I have so far:
<?php
include("connect.php");
include("header.php");
if(isset($_GET['invstuview_bag'], $_GET['invstuview_name']))
{
$get_id = $_GET['invstuview_bag'];
$get_name = $_GET['invstuview_name'];
?>
<br/>
<ul class="breadcrumb">
<li class="breadcrumb-item">View History</li>
<li class="breadcrumb-item-active">Baggage Detail History</a></li>
</ul>
<div id="cssword">Baggage Detail History For <?php echo $get_name; ?>(<?php echo $get_id; ?>)</div>
<div class="container" style="width:70%;">
<div class="table-responsive">
<?php
$sql_join = "SELECT student.*,location.*, inventory.*, baggage.*
FROM
student,location, inventory, baggage
WHERE
student.student_id = inventory.invstu_id AND
baggage.baggage_id = inventory.invbag_id AND
location.location_id = inventory.invloc_id AND
invstu_id = '$get_id'
ORDER BY inventory_id";
$result_join= mysqli_query($connect,$sql_join);
$prev_year = "";
$prev_sem = 0;
$get_year = "";
$get_sem = 0;
while($row_join=mysqli_fetch_assoc($result_join))
{
$counter = 1;
$prev_year = $row_join["invstu_year"];
$prev_sem = $row_join["invstu_sem"];
//if the data is of same year and semester
if(($prev_year!="") && ($prev_sem!=0) && ($prev_year == $get_year) && ($prev_sem == $get_sem))
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
//if data is not of same year or semester
else
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
</br></br>
Room: <?php echo $row_join["invstu_room"]; ?><br/>
Year: <?php echo $row_join["invstu_year"]; ?><br/>
Semester: <?php echo $row_join["invstu_sem"]; ?><br/>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
}
?>
</div>
<div align="right">
<button type="button" name="back" class="btn btn-success" id="back" style="width:200px; display:inline-block; margin-top:2%; margin-bottom:1%; background-color: #4CAF50" onclick="window.location.href='view.php'">Back</button>
</div>
</div>
<?php
}
?>
Any ideas is highly appreciated.
Move these out of the while loop:
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
echo'</table>';
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>
Var dump result
I am fetching data in MySQL into an HTML table:
<div id="toggleDiv" class="">
<div class="box-body" id="toggleDiv_2">
<div class="row">
<div class="col-md-6">
<?php
$select_patient_info_cash =
"SELECT * FROM patient_info WHERE id_logged = :id_logged".
" AND patient_id = :patient_id AND payment_type = :pt";
$select_patient_info_cash_stmt =
$conn->prepare($select_patient_info_cash);
$select_patient_info_cash_stmt->bindValue(":id_logged", $id_logged);
$select_patient_info_cash_stmt->bindValue(":patient_id", $patient_id);
$select_patient_info_cash_stmt->bindValue(":pt", "cash");
$select_patient_info_cash_stmt->execute();
$select_patient_info_cash_stmt->fetch(PDO::FETCH_ASSOC);
$select_patient_info_cash_stmt_count =
$select_patient_info_cash_stmt->rowCount();
if($select_patient_info_cash_stmt_count > 0){ ?>
<table style="text-align:center"
class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Project Description</th>
<th>Project Cost</th>
<th>Date Of Pay</th>
</tr>
</thead>
<?php foreach ($select_patient_info_cash_stmt as $cash) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
</table>
<?php } else { ?>
<?php } ?>
</div><!-- /.col -->
Now I test it for a user that have data in patient info where payment_type != cash, and the <thead> didn't show up. But when I test it where payment_type=cash the <thead> shows up but no data are echoed into line.
It should show me 2 new lines after <thead> and I can't figure out why data are not displayed on the page
I think you miss ->fetch() from your prepared statement. According to PHP docs:
<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
?>
Therefore you need to modify your code to something like:
<?php while ($cash = $select_patient_info_cash_stmt->fetch()) { ?>
<tr>
<td><?php echo $cash['project'] ?></td>
<td><?php echo $cash['project_cost'] ?></td>
<td><?php echo $cash['date_now'] ?></td>
</tr>
<?php } ?>
I also suggest that you should use a MVC framework or some sort of template engines. Mixing PHP and HTML is a very bad practice.
References: http://php.net/manual/en/pdo.prepared-statements.php
how can I pass values/ variables between tabs in html using php
I have 3 tabs those are fetched from database. each tab has separate id.
<div class="tab-nav">
<?php while ($row1 = mysql_fetch_array($result1)) : ?>
<?php $row1['name']; ?>
<?php endwhile ?>
</div>
tabs
<div class="tab-holder">
<div class="tab">
<table class="table table-striped">
<thead>
<tr>
<th width="34%">Job Title</th>
<th width="15%">Experience</th>
<th width="29%">Location</th>
<th width="8%">Salary</th>
<th width="7%"></th>
</tr>
</thead>
<tbody>
<?php
while($row = mysql_fetch_array($result)) {
$cat = $row['cat'];
if ($cat =='6') {
$jobtitle = $row['vVacjobtitle'];
?>
<tr>
<td>
<span class="pill medium green"><?php echo $row['vVacjobtitle'] ?></span>
</td>
<td><?php echo $row['experience'] ?></td>
<td><?php echo $row['vVacLocation'] ?></td>
<td><?php echo $row['salary'] ?></td>
<td>
Apply
</td>
</tr>
<?php
}
} ?>
</tbody>
</table>
</div>
how can i get id when clicking the tab
if ($cat == 6)
i want to place id in the place of 6
Thanks in advance .. :)
Since you're passing the value of id as a GET parameter, so you can access it by $_GET['id'] in your PHP code
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/