Grouping Data under Headers - php

Hi guys i currently have a address book that displays the information like this:
Extension Name Department Email Cellphone
1 jurgen home 1# 1
2 dawn work 2# 2
3 mike away 3# 33
Is there a way to display the information by Department? such as this: Where it shows the users under each department that they fall under, basically grouping them? be gentle :) im new to coding :D
Extension Name Email Cellphone
ALPINE DEALER PRINCIPAL
7813 Garth 123#alpinemotors.co.za 2
7898 Sam 3653#alpinemotors.co.za 4
AFTER-SALES DIRECTOR
7709 Bruce 6342#alpinemotors.co.za 3
Heres my code for the view.php page (on a side note i made a search bar but dont know how to code it to search the names field actively, so as they type in the search bar, the tables filter without having to press enter)
default:
$sql ="SELECT * FROM address ORDER BY name ASC";
$data = mysql_query($sql);
?>
<h2>Alpine VW Extension List</h2>
<table class="tableStyleClass">
<tr>
<th width="100">Extension</th>
<th width="100">Name</th>
<th width="200">Department</th>
<th width="200">Email</th>
<th width="200">Cellphone</th>
<th width="200" colspan="2">Admin</th>
</tr>
<td colspan="5" align="right"><?php if($disable!=1){?><div align="right <a href="<?=$_SERVER['PHP_SELF'];?>?mode=add"?mode=add>Add Contact</a><?php }else{?>Contact Book is Full<?php } ?></div></td>
<?php
$rowColor = 0;
while($info = mysql_fetch_array( $data )){
if($rowColor==0){
?>
<tr class="oddClassStyle">
<?php
$rowColor =1;
}elseif($rowColor==1){
?>
<tr class="evenClassStyle">
<?php
$rowColor = 0;
}
?>
<td><?=$info['Ext'];?></td>
<td><?=$info['name'];?></td>
<td><?=$info['department'];?></td>
<td><?=$info['email'];?></td>
<td><?=$info['phone'];?></td>
<td><a href="<?=$_SERVER['PHP_SELF'];?>?id=<?=$info['id'];?>&name=<?=$info['name'];?>&phone=<?=$info['phone'];?>&email=<?=$info['email']; >&mode=edit" >Edit </a></td>
<td>Remove</td>
</tr>
<?php
}
?>
</table>
<?php
break;
(THE ABOVE CODE WAS FIXED FROM ANSWERS BELOW)
Ok now that i have that sorted my edit button doesnt seem to work. it supposed to display the current info and then once edited, you click the edit button and it changes the information for that user.
Here is the code i was using previously:
case 'edit':
?>
<h2>Editing: <?=$_GET['name'];?></h2>
<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>?mode=edited" method="post">
<table width="399" class="tableStyleClassTwo">
<tr><td width="87">Name:</td>
<td width="551"><div align="left">
<input type="text" value="<?=$_GET['name'];?>" name="name" />
</div></td></tr>
<tr><td>Phone:</td><td><div align="left">
<input type="text" value="<?=$_GET['phone'];?>" name="phone" />
</div></td></tr>
<tr><td>Email:</td><td><div align="left">
<input type="text" value="<?=$_GET['email'];?>" name="email" />
</div></td></tr>
<tr><td>Extension:</td><td><div align="left">
<input type="text" value="<?=$_GET['Ext'];?>" name="Ext" />
</div></td></tr>
<tr><td colspan="2" align="center">Back |<input name="Submit" type="submit" value="Save Changes" /></td></tr>
<input type="hidden" name="mode" value="edited">
<input type="hidden" name="id" value="<?=$_GET['id'];?>">
</table>
</form>
<?php
break;
case 'edited':
$name = $_POST['name'];
$phone = $_POST['phone'];
$department = $_POST['department'];
$email = $_POST['email'];
$Ext = $_POST ['Ext'];
$id = $_POST['id'];
$sql = "UPDATE address SET name = '" . $name ."', phone = '" . $phone . "', email = '" . $email . "', Ext = '" . $Ext . "' WHERE id = '" . $id . "'";
mysql_query($sql);
header('location: ' . $_SERVER['PHP_SELF']);
break;

Try this query -
$sql = "SELECT t1.Ext, t1.name, t1.email, t1.cellphone FROM address t1 join address t2 on t1.id=t2.id ORDER BY t2.department";
EDIT:
<?php
$sql = "SELECT t1.Ext, t1.name, t1.email, t1.cellphone FROM address t1 join address t2 on t1.id=t2.id ORDER BY t2.department";
$data = mysql_query($sql);
?>
<h2>Alpine VW Extension List</h2>
<table class="tableStyleClass">
<tr>
<th width="100">Extension</th>
<th width="100">Name</th>
<th width="200">Email</th>
<th width="200">Cellphone</th>
<th width="200" colspan="2">Admin</th>
</tr>
<td colspan="5" align="right"><?php if($disable!=1){?><div align="right <a href="<?=$_SERVER['PHP_SELF'];?>?mode=add"?mode=add>Add Contact</a><?php }else{?>Contact Book is Full<?php } ?></div></td>
<?php
$rowColor = 0;
$deptName = "";
while($info = mysql_fetch_array( $data )){
if($deptName === "" || $deptName != $info['department']) {
$deptName = $info['department'];
echo "<tr> <th colspan=6> $deptName </td> </th> </tr>\n";
}
if($rowColor==0){
?>
<tr class="oddClassStyle">
<?php
$rowColor =1;
}elseif($rowColor==1){
?>
<tr class="evenClassStyle">
<?php
$rowColor = 0;
}
?>
<td><?=$info['Ext'];?></td>
<td><?=$info['name'];?></td>
<td><?=$info['email'];?></td>
<td><?=$info['phone'];?></td>
<td><a href="<?=$_SERVER['PHP_SELF'];?>?id=<?=$info['id'];?>&name=<?=$info['name'];?>&phone=<?=$info['phone'];?>&email=<?=$info['email']; >&mode=edit" >Edit </a></td>
<td>Remove</td>
</tr>
<?php
}
?>
</table>
<?php
break;
?>

Here's another way:
$depts = [];
$res = mysql_query("SELECT * FROM address ORDER BY department, name");
// build the appropriate data structure that organizes contacts into different departments
// by looping through each contact, check if department exist (if not create the department) and add contact to the department
while ($row = mysql_fetch_assoc($res)) {
if (!isset($depts[$row['department']])) {
$depts[$row['department']] = [];
}
$depts[$row['department']][] = $row;
}
?>
<h2>Alpine VW Extension List</h2>
<table class="tableStyleClass">
<tr>
<th width="100">Extension</th>
<th width="100">Name</th>
<th width="200">Email</th>
<th width="200">Cellphone</th>
<th width="200" colspan="2">Admin</th>
</tr>
<tr>
<td colspan="6">
<?php if ($disable != 1) : ?>
Add Contact
<?php else : ?>
<span>Contact Book is Full</span>
<?php endif ?>
</td>
</tr>
<?php foreach ($depts as $dname => $contacts) : ?>
<tr>
<td colspan="6"><?= $dname ?></td>
</tr>
<?php foreach ($contacts as $contact) : ?>
<tr>
<td><?= $contact['ext'] ?></td>
<td><?= $contact['name'] ?></td>
<td><?= $contact['email'] ?></td>
<td><?= $contact['phone'] ?></td>
<td><a href="<?= $_SERVER['PHP_SELF'] ?>?id=<?= $contact['id'] ?>&mode=edit" >Edit </a></td>
<td>Remove</td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
</table>
On another note, stop using mysql_* functions. There's a big red warning that says it is deprecated for good reasons. Use mysqli or better PDO instead!
And make better use of CSS (and use less attributes such as align="right"). You can use :nth-child selector (particularly with :even and :odd)to do your alternating styles for your table.

Related

Display records based on same id but remove the similarity on sql

maybe the subject is duplicate or have the similarity with others, but i haven't found what i'm looking for.
I have a Sales Order table on sql, and I am using odbc to connect
id_sales_order date customer product qty
001 2017-12-12 ABC laptop 1
001 2017-12-12 ABC printer 1
001 2017-12-12 ABC mouse 2
002 2017-12-15 Hercules pc 1
002 2017-12-15 Hercules hdd 1
and I have a code
<body>
<?php
$konek = odbc_connect("otosurabaya","mike","mike") or die("Error Connect to Database");
$sql = "SELECT id_sales_order, customer, product, date,qty FROM Kreasi_Cabang.dbo.QNota_SalesOrder where sales='" .$_SESSION['username']. "' group by id_sales_order, customer, product, date, qty" ;
$hasil = odbc_exec($konek, $sql) or die ("Error Execute [".$sql."]");
$noUrut = 1;
?>
<?php
while($result = odbc_fetch_array($hasil))
{
?>
<div class="panel panel-success">
<div class="panel-heading" >
<h3 class="panel-title" align="center"><strong> <?php echo $_SESSION['username']; ?> </strong> </h3>
</div>
<div class="panel-body">
<table width="100%" border="1">
<tr>
<th width="131" scope="row">Customer</th>
<td width="44">:</td>
<td width="800" align="left"><?php echo $result['customer'];?></td>
</tr>
<tr>
<th scope="row">SO ID</th>
<td>:</td>
<td align="left"><?php echo $result['id_sales_order'];?></td>
</tr>
<tr>
<th scope="row">Date
<td align="left"><?php echo $result['date'];?></td>
</tr>
<tr>
<th height="49" scope="row">Product</th>
<td> </td>
<td align="left">QTY</td>
</tr>
<tr>
<th scope="row"><?php echo $result['product'];?></th>
<td> </td>
<td align="left"><?php echo $result['qty'];?></td>
</tr>
<?php }?>
</table>
</p>
</div>
<hr />
<?php
odbc_close($konek);
?>
</body>
The above code, it display record only per row, I want to remove the same value and keep the others.
I have search and try many syntax, please need your expertise
I want to display the record as below
Customer : ABC
Date : 2017-12-12
ID SO : 001
Product QTY
Laptop 1
Printer 1
Mouse 1
You need to put the if condition inside the while loop. And need to put the flag so that your customer name and other details should be print once and uniquely.
And product details will be print multiple times. Here is the code for that:
<?php
$customer_name = '';
$sale_date = '';
$sales_id = '';
$flag = true;
?>
<div class="panel panel-success">
<div class="panel-heading" >
<h3 class="panel-title" align="center"><strong> <?php echo $_SESSION['username']; ?> </strong> </h3>
</div>
<div class="panel-body">
<table width="100%" border="1">
<?php
while($result = odbc_fetch_array($hasil))
{
?>
<?php if($customer_name != $result['customer'] && $sale_date != $result['date'] && $sales_id != $result['id_sales_order']) { ?>
<tr>
<th width="131" scope="row">Customer</th>
<td width="44">:</td>
<td width="800" align="left"><?php echo $result['customer'];?></td>
</tr>
<tr>
<th scope="row">SO ID</th>
<td>:</td>
<td align="left"><?php echo $result['id_sales_order'];?></td>
</tr>
<tr>
<th scope="row">Date
<td align="left"><?php echo $result['date'];?></td>
</tr>
<?php
$customer_name = $result['customer'];
$sale_date = $result['date'];
$sales_id = $result['id_sales_order'];
$flag = true;
} else { $flag = false; }
if($flag) {
?>
<tr>
<th height="49" scope="row">Product</th>
<td> </td>
<td align="left">QTY</td>
</tr>
<?php } ?>
<tr>
<th scope="row"><?php echo $result['product'];?></th>
<td> </td>
<td align="left"><?php echo $result['qty'];?></td>
</tr>
<?php } ?>
You can change in the if condition && with || if you want to print the customer details in case of any detail is not matching. Like if date is different or if sales id is different.
Note: I put the if condition on the basis of your data shown in question. you can change it as per your requirement.

How to collect the id once I click on image linked update page in php?

This is my whole html and php code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Student Details</h2>
<div id="EditStudent">
<img src="sawiro/EditImage.jpg" alt="Edit Student Image">
</div>
<table style="width: 100%" border ="0" bordercolor="green" bgcolor="white" cellspacing="20" cellpadding="0">
<thead>
<tr>
<?php
$Carrier = 0;
function clearInput($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(isset($_GET['SelectedId'])){
$Carrier = clearInput($_GET['SelectedId']);
}
$cnx = new PDO('mysql:host=localhost;dbname=transportsystem;charset=utf8', 'root', '');
$result = $cnx->query("SELECT * FROM person
INNER JOIN student ON person.PerID = student.StPersonID
WHERE PerID = $Carrier");
while($row = $result->fetch()){
$Name = $row["Name"];
$Gender = $row["Gender"];
$Telephone = $row["Telephone"];
$Class = $row["Class"];
echo '<th colspan="8" style="font-size:40px;">'.$row["Name"].'</th>';
}
?>
</tr>
</thead>
<tr style="color: #000;">
<th> Student ID:</th>
<td><?php echo $Carrier;?></td>
</tr>
<tr>
<th> Full Name:</th>
<td><?php echo $Name;?></td>
</tr>
<tr>
<th> Gender:</th>
<td><?php echo $Gender;?></td>
</tr>
<tr>
<th> Telephone:</th>
<td><?php echo $Telephone;?></td>
</tr>
<tr>
<th> Class:</th>
<td><?php echo $Class;?></td>
</tr>
</table>
</body>
</html>
I want to add Selectionid, as you can see on it
?SelectionId=$Carrier;
where my id variable is $Carrier becuase of this code
if(isset($_GET['SelectedId']))
{$Carrier = clearInput($_GET['SelectedId']);}
I want once I click on <img src="sawiro/EditImage.jpg" alt="Edit Student Image"> to give me url UpdateStudent.php?SelectionId=8 for example, to use this id for setting data purpose.
Please help me to collect the id from the target php to use in to another php to update that particular row.
I think you meant it like this:
<a href="UpdateStudent.php?SelectionId=<?php echo $Carrier ;?>">
<img src="sawiro/EditImage.jpg" alt="Edit Student Image"/>
</a>

I want to delete multiple images from source folder through PHP but the code for deleting the multiple images from the folder is not working

<?php include("header.php"); ?>
<?php
if (#$_POST['delete']=="Delete"){
$count=count($_POST['delbx']);
for($i=0;$i<$count;$i++){
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resulty = mysqli_query($conn, $delete) or die(mysql_error());
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$path="admin/".$rowdy['a_image'];
echo $path;
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
} ?>
<div class="table-responsive">
<table class="table">
<caption>All Users</caption>
<?php
$sql = "SELECT a_id, a_name, a_phone, a_password, a_role, a_mail, a_image FROM admin";
$result = $conn->query($sql);
if ($result->num_rows > 0) {?>
<thead>
<tr>
<th><form action="view_user.php" method="post"><input name="delete" type="submit" id="delete" value="Delete"></th><th>S. No.</th> <th>Name</th> <th>Phone No.</th> <th>Mail Id</th> <th>Role</th> <th>Password</th> <th>Image</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()) { ?>
<tbody>
<tr>
<th scope="row">
<?php echo $row["a_id"]; ?>
</th>
<td align="center" bgcolor="#FFFFFF">
<input name="delbx[]" type="checkbox" id="delbx[]" value="<?php echo $row["a_id"]; ?>" />
</td>
<td>
<?php echo $row["a_name"]; ?>
</td>
<td>
<?php echo $row["a_phone"]; ?>
</td>
<td>
<?php echo $row["a_mail"]; ?>
</td>
<td>
<?php echo $row["a_role"]; ?>
</td>
<td>
<?php echo $row["a_password"]; ?>
</td>
<td>
<img src="admin/<?php echo $row["a_image"]; ?>" width="60" height="40">
</td>
<th>
Edit
</th>
</tr>
</tbody>
<?php
}
} else {
echo "0 results";
}?>
</table>
</form>
</div>
<?php include("footer.php"); ?>
The code I mention is not deleting the multiple images from the source folder but deleting the multiple data from database whereas I am trying to delete images from the source folder along with data please help thanks in advance
One of the problem is you are deleting the row and trying to select image column from the deleted row.. dont use user supplied variables directly in your query
your code should be
for($i=0;$i<$count;$i++){
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
if(mysqli_query($conn, $delete)){
$path="admin/".$rowdy['a_image'];
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
}

Removing access from user levels

sorry for my code but this is only a practice code, i m trying to remove user access links, i displayed all the links that corresponds to the selected user access here's the code and database
Select user levels
<p class="left"><b>User Level Information</b></p><br>
<table class="left">
<tr>
<td id="TD3"><B>User Level ID</B></td>
<td id="TD3"><B>User Level Name</B></td>
</tr>
<?php
$employee = mysql_query("SELECT * FROM cms_userlevels ORDER BY `cms_userlevels`.`user_level` ASC ");
while($row1 = mysql_fetch_array($employee)){
?>
<tr>
<td id="TD3"><?php echo $row1['user_level']; ?></td>
<td id="TD3"><?php echo "<a href='manageuserlevels.php?id=".$row1['user_level']."'>"; ?> <?php echo $row1['user_type']; ?> </a></td>
</tr>
<?php } ?>
</table>
Selected User level
if(isset($_POST['Update']))
{
if(isset($_POST['delete']))
{
$delete1= mysql_query("DELETE FROM cms_userlevels where PROGRAM_ID = '$userId'");
header('location:manageuserlevel.php');
}
else
{
$update = mysql_query("UPDATE cms_program set
PROGRAM_TITLE='$_POST[progti]',
PROGRAM_DESCRIPTION='$_POST[progde]'
where
PROGRAM_ID='$userId'");
}
}
?>
<?php
$userId = $_GET['id'];
$sql = mysql_query("select * from cms_userlevels where user_level='$userId'");
$row2 = mysql_fetch_array($sql);
echo "
<center>
<table class='left'>
<form method='post' action='managemanageuserlevels.php?id=". $userId. "'>
<tr>
<td id='TD4'>User Level ID </td>
<td id='TD4'><input type='text' name='usertype' value='".$row2['user_type']."'></td>
</tr>
<tr>
<td colspan='2'><center><input type='submit' name='submit' value='Update'></td>
</tr>
</form>
</table>";
?>
<table class="left">
<tr>
<td>Remove Access:</td></tr>
<?php
$haslevel = mysql_query("select * from cms_userlevels, cms_level_page_matches, cms_pages
WHERE cms_userlevels.user_level = '$userId'
AND cms_level_page_matches.page_id = cms_pages.id");
while($getlevel = mysql_fetch_array($haslevel)){
$showhaslevel1 = $getlevel['id'];
$showhaslevel2 = $getlevel['page_title'];
?>
<tr><td>
<input type="checkbox" name="checkbox[]" value="<? echo $showhaslevel1; ?>"><?php echo $showhaslevel2; ?></td></tr>
<?php } ?>
the tables used
cms_userlevels
user_level user_type
1 Admin
2 Student
user_pages
id page name
1 page1.php
2 page2.php
user_level_page_matches
id user_level page_id
1 1 1
2 2 2

How to transfer multiple rows output to another php file to process data?

<form name="myForm" action="doControlAcc.php" method="post" onsubmit="return validateForm(this);">
<table cellpadding="0" cellspacing="0" border="20" class="display" id="acc" width="200">
<thead>
<tr><h2>Accessories summary</h2></tr>
<tr>
<th>Type</th>
<th>Currently - Ex Factory</th>
<th>Minimum Required Stock</th>
<th>Update by</th>
<th>Last update</th>
<th>Re-Stock Amount</th>
<th>Stock Out</th>
</tr>
</thead>
<tbody>
<?php
$accessories = array()
$query = "SELECT description, current_stock, min_required_stock, c_ex_factory, last_update from Accessories”;
$result= mysqli_query($link, $query) or die(mysqli_error($link));
$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){
$order = $row['c_ex_factory'] - $row['min_required_stock'];
if ($order < 0){
$order = $row['min_required_stock']-$row['c_ex_factory'];
$color = "#FF0000";
}else{
$order = 0;
$color = "#00FF00";
}
?>
<tr>
<td name="accessories[]" id="accessories" value=”<?php echo $row['description'] ?>” <?php echo in_array($row['description', $ accessories) > <?php echo $row['description']; ?> </td>
<td align="center" name="accessories[]" id="accessories" value=”<?php echo $row[''c_ex_factory '] ?>” <?php echo in_array($row[''c_ex_factory ', $ accessories) > <?php echo $row['c_ex_factory']; ?> </td>
<td align="center" name="accessories[]" id="accessories" value=”<?php echo $row[''min_required_stock '] ?>” <?php echo in_array($row[''min_required_stock ', $ accessories) > <?php echo $row['min_required_stock']; ?> </td>
<td align="center" name="accessories[]" id="accessories" value=”<?php echo $row[' name '] ?>” <?php echo in_array($row[' name ', $ accessories) > <?php echo $row['name']; ?> </td>
<td align="center" name="accessories[]"id="accessories" value=”<?php echo $row['description'] ?>” <?php echo in_array($row['description', $ accessories) > <?php echo $row['up_when']; ?> </td>
<td align="center" name="accessories[]" bgcolor="<?php echo $color; ?>" id="accessories" <?php echo in_array($row[' re_stock ', $ accessories) > <input type="text" size="5" name="re_stock" value=" <?php echo $order ?>"/> </td>
<td align="center" name="accessories[]"id="accessories" <?php echo in_array($row[' out_stock ', $ accessories) > <input type="text" size="5" name="out_stock" value ="0"/></td>
</tr>
<?php
}
?>
<tr>
<td> </td>
<td> </td>
<td></td> <td></td> <td></td>
<td><button data-theme="b" id="submit" type="submit">Send order</button></td>
</tr>
</tbody>
</table>
</form>
Hello,
I'm new to web programming.
The code above will fetch the data from database to create a table in PHP, then it will take the user inputs for stock-out and restock and send those to doControlAcc.php file.
The problem I'm facing is I need to create multiple input lines so that users can enter stock-out and re-stock for different products. And I don't know how to pass these multiple lines to doControlAcc.php file after the form is filled. (i.e how can I access accessories array in doControlAcc.php?).
THank you for your help.
This may not be the best way, but you can always use session variables. I can't quite understand the program you are writing, so I can't say whether or not sessions would be safe enough, but if you wanted to use sessions, do something like this:
At the beginning of your file:
<?php
session_start();
?>
And later, when the variable is all set:
$_SESSION['accessories'] = $accessories;
To access it in another code file, you must start the session as shown above in each code file. Then, just do this;
$accessories = $_SESSION['accessories'];

Categories