PHP table data filtering - php

I want to filter data in the table use dropdown menu (e.g. filter by name so i can see all data with name 'Jane'). I don't want to move to another page (use ajax or anything else if can). Any idea what must i do ?
This is the dropdown menu and table code :
<!-- Dropdown menu -->
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
<table>
<!-- Table heading -->
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<!-- Table body -->
<tbody>
<?php
$i=1;
foreach ($dataProvider as $data){
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $data->name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->join; ?></td>
<td><?php echo $data->last_login; ?></td>
</div>
</tr>
<?php $i++; } ?>
</tbody>
<!-- // Table body END -->
</table>
Thanks for any advice.
Regards

You can use jQuery to achieve this effect quite easily. Make two files:
1) One that includes the table.
2) One that has the select tag that will reload the first file upon change of the <select> tag.
Let's call the first file select.php
<script>
// Load the div with the contents of the table.php file with no GET parameter
$('div').load('table.php');
$('select').change(function() {
var name = $(this).val();
var data = 'name='+ name;
// Make sure that the table's contents don't change if the first option tag
// is selected.
if(name != '') {
$('div').load('table.php', data);
}
});
</script>
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
File two can be called table.php
$sql = "SELECT * FROM table WHERE name = '".$name."'";
$query = mysql_query($sql)or die(mysql_error());
$num = mysql_num_rows($query);
$i = 0;
while($row = mysql_fetch_array($query)) {
// Save your info as variables
$name[$i] = $row['name'];
$email[$i] = $row['email'];
$join[$i] = $row['join'];
$login[$i] = $row['last_login'];
}
?>
<div>
<table>
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num;$i++) {
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $name[$i]; ?></td>
<td><?php echo $email[$i]; ?></td>
<td><?php echo $join[$i]; ?></td>
<td><?php echo $login[$i]; ?></td>
</div>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Upon change of the select tag, the second file will be loaded with the value of the selected option tag sent to that file as a GET variable. You might wanna take the SQL part with a grain of salt as I'm not sure how you're fetching your relevant data.

Related

How can I make the data that is displayed on the table change with value chosen in a select box?

Based on the user that is chosen within the combo box, I want the table that is displaying user data from the database to only show the data corresponding to the user selected in the combo box.
I mainly tried using an array to store values but I couldn't get that working.
Combo Box that displays the name to pick
<select>
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded">Find</button>
</select>
</form>
Table that shows the data from the database.
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
//Fetch Data form database
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
I'm wondering if by using the form and doing a function that on pressing the Find button it looks up the user and displays only it's data. Thanks
Check my code here, there are some thing you must add, like a WHERE statement to your query, when fetching data to only show results with the selected name in the form
<!-- Create a form with a select for all the names -->
<form method="POST" action="">
<select name="name">
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded" name="find_info">Find</button>
</select>
</form>
<?php
if(isset($_POST['find_info'])){ //If find button is pressed, show this:
?>
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
$nameSelected = strip_tags(mysqli_real_escape_string(htmlspecialchars($_POST['name'])));
// Use WHERE in your mysqli query to fetch data where name in database is equal to $nameSelected
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>

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
}
}

how to pass values between tabs in a html page using 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

get unique ID's of rows in php foreach loop for JQuery

Good Afternoon,
I have 3 foreach loops on my page, the first gets the teams, the second gets each person in the team, and the third gets each unique reference to a task that the agent has completed. I have collected this data and it is being displayed fine. I now want to add some JQuery to it so it will hide the agents and references unless the relevant team or agent is clicked on.
So if I load the page everything will be hidden apart from the teams, when I click on a team it will show the agents, when I click on an agent it will show the references.
I am having trouble assigning unique ID's to each row and finding those in the JQuery script.
Here is my code...
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $aTeamData['iBusinessStreamId'];?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId]?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $aPropertyData['iUserId'];?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I have given each of the rows a unique class by adding in the unique identifier from the database. I just dont know how to find these within the Jquery script.
EDIT:
Maybe not explained myself properly, I would like help with how to set up this script but obviously a lot better.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick',function(){
$('.Agent').toggle('show');
$('.Property').toggle('show');
});
});
</script>
But in this case it will show/hide all of the rows as they will all have the same id's, whereas now I have added on the unique id on the end with the php code in the class, I dont know how to call those as they all are called different classes.
So if I click on TeamClick1, it shows the actual rows for that team (Agent1), and not all of them. but obviously I cant type out all of the unique id's I just dont know how to get them from the php in JQuery.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick(UNIQUE ID)',function(){
$('.Agent(UNIQUE ID)').toggle('show');
$('.Property(UNIQUE ID)').toggle('show');
});
});
Hope this makes sense.
I am hasty but it is like this
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $iBusinessStreamId;?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId; ?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId2 => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $iUserId2 ;?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I'm putting the identifiers in classes, but if you want put in IDs you can, is the same thing, like this id="<?php ... ?>"

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/

Categories