Cannot sort column on clicking column header in php - php

I have written code to sort column by clicking header column.
I have created array of column name to be sorted then created select query with $sort and $order. In query string it passes the columname and ASC or DESC but problem it takes me to blank page and it if I pass name of page then also it shows blank page. Can anyone help me to find error.
<?php
function list_users()
{
$y = 0;
$sortDefault = 'id';
// select array for columns
$sortColumns = array('id','first_name','last_name');
// select query with sort and orderby
$sort = isset($_GET['sort']) && in_array($_GET['sort'], $sortColumns) ? $_GET['sort'] : $sortDefault;
$order = (isset($_GET['order']) && strcasecmp($_GET['order'], 'DESC') == 0) ? 'DESC' : 'ASC';
$sql = "select * from contacts ORDER BY $sort $order";
$result = mysql_query($sql) or die ("Can't run query because ". mysql_error());
echo "<form method='post'>
<table width='50' align='right' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2' align='center' style='font-size:18px; font-weight:bold;'>"; ?>
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
}
echo "<a href='logout.php' id='logout'>Logout</a></td>
</tr>
</table>
<table width='400' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr><td colspan='2' align='center' style='font-size:18px; font-weight:bold;'>Displayed Data</td></tr>
<tr><td colspan='2'></td></tr>
<tr><td colspan='2'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new contact</a></td></tr>
<tr><td colspan='2'> </td></tr>
</table>
<br/>
<br/>";
if (mysql_num_rows($result)){
(($y % 2) == 0) ? $bgcolor = "#8FBC8F" : $bgcolor=" #9ACD32";
echo "<table width='400' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr style='background-color:$bgcolor;' align=center>
<td><input type='checkbox' id='all' name='mainchk' /></td>";?>
<td><a href='?sort=name&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Name</a></td>
<td><a href='?sort=last_name&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>LastName</td>
<td>Status</td>
<td>Action</td>
<?php "</tr>";
while($rows = mysql_fetch_array($result)){
$name = $rows['first_name'];
$lname = $rows['last_name'];
$status = $rows['contact_status'];
$id = $rows['id'];
($status == 0) ? $status = "Available to contact" : $status = "Do not contact at present.";
echo"<tr align=center>
<td><input type='checkbox' value='$id' name='data[]' id='data'></td>
<td>$name</td>
<td>$lname</td>
<td>$status</td>
<td><a href='".$_SERVER['PHP_SELF']."?action=delete&id=$id' class='confirmation'><img src='delete.png' height='16px' width='16px'></a> <a href='".$_SERVER['PHP_SELF']."?action=edit&id=$id'><img src='write.png' height='16px' width='16px'></a></td>
<tr>";
$y++;
}
echo "</table>";
}else{
echo "<tr><td colspan='2' align='center'><b>No data found.</b></td></tr>";
}
echo"<div align='center'><input name='delete' type='submit' value='Delete' id='delete'></a></form>";
}
?>
This is what I get in url::::
/mainpage.php?sort=first_name&order= DESC and blank page is displayed.
I got to my problem is that as I am displaying html table by function list_users() which is called in following way::
if ((empty($_POST))&&(empty($_GET)))
{
list_users();
die();
}
If I remove if condition then it problems to my other function add contact form is displayed below list. If only list_user() is called it sorts column.

Solution:
As I called displayed result in a function earlier so now I directly displayed result set rather than any function so the sort function which I have written for column header is working now properly.And my other function are also working properly.
echo "<form method='post'>
<table width='50' align='right' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2' align='center'>"; ?>
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
}
echo "<a href='logout.php' id='logout'>Logout</a></td>
</tr>
</table>
<table width='500' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr><td colspan='6' align='center' style='font-size:18px; font-weight:bold;'>Displayed Data</td></tr>
<tr><td colspan='6'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new contact</a></td></tr>";
$y = 0;
$sortDefault = 'id';
// select array
$sortColumns = array('id','first_name','last_name');
// select query with sort
$sort = isset($_GET['sort']) && in_array($_GET['sort'], $sortColumns) ? $_GET['sort'] : $sortDefault;
$order = (isset($_GET['order']) && strcasecmp($_GET['order'], 'DESC') == 0) ? 'DESC' : 'ASC';
$sql = "select * from contacts ORDER BY $sort $order";
$result = mysql_query($sql) or die ("Can't run query because ". mysql_error());
if (mysql_num_rows($result)){
(($y % 2) == 0) ? $bgcolor = "#8FBC8F" : $bgcolor=" #9ACD32";
echo "
<tr style='background-color:#5CB3FF' align=center>";?>
<!-- <td><input type='checkbox' id='all' name='mainchk' /></td>-->
<td>Chk</td>
<td><a href='?sort=first_name&order=<?php echo $order =='DESC' ? 'ASC' : 'DESC' ?>'>Name</a></td>
<td><a href='?sort=last_name&order=<?php echo $order =='DESC' ? 'ASC' : 'DESC' ?>'>LastName</td>
<td><a href='?sort=email&order=<?php echo $order =='DESC' ? 'ASC' : 'DESC' ?>'>Email</td>
<td>Status</td>
<td id="actid">Action</td>
<?php "</tr>";
while($rows = mysql_fetch_assoc($result)){
$name = $rows['first_name'];
$lname = $rows['last_name'];
$email = $rows['email'];
$status = $rows['contact_status'];
$id = $rows['id'];
($status == 0) ? $status = "Available to contact" : $status = "Do not contact at present.";
echo"<tr align=center style='background-color:#C0C0C0'>
<td><input type='checkbox' value='$id' name='data[]' id='data'></td>
<td>$name</td>
<td>$lname</td>
<td>$email</td>
<td>$status</td>
<td><a href='".$_SERVER['PHP_SELF']."?action=delete&id=$id' class='confirmation'><img src='delete.png' height='16px' width='16px'></a>
<a href='".$_SERVER['PHP_SELF']."?action=edit&id=$id'><img src='write.png' height='16px' width='16px'></a></td>
<tr>";
$y++;
}
echo"</table>";
}
else
{
echo "<tr><td colspan='2' align='center'><b>No data found.</b></td></tr>";
}
echo"<div align='center' width='50' ><a href='#' id='all' name='mainchk'>CheckAll/UnCheckAll</a>
<input type='image' src='delete.png' alt='Submit' width='16px' height='16px' id='delete'>
</div></form>";

Related

Sorting By Checking the Option Button PHP MYSQL

I want to let the user sort the table base on what the user wants.
I have two options for this, sort by name or sort by exam.
code
echo "<table border=1 align=center><tr class=style2><td><input type=radio name=sort value='byname'>Sort By Name<td><input type=radio name=sort value='byexam'>Sort By Exam";
$sort = $_POST['name'];
if ($sort == "byname"){
$sort=mysql_query("select * from mst_adminresult order by login ASC",$cn) or die(mysql_error());
while($row=mysql_fetch_row($sort))
echo "<table border=1 align=center><tr class=style2><td>Student Name <td> Test<br> Question <td> Score";
echo "<tr class=style8><td>$row[1] <td align=center> $row[2] <td align=center> $row[3]/20";
echo "</table>";
}else{
$sort=mysql_query("select * from mst_adminresult order by test_id ASC",$cn) or die(mysql_error());
while($row=mysql_fetch_row($sort))
echo "<table border=1 align=center><tr class=style2><td>Exam<td width=300>Student Name<td> Score";
echo "<tr class=style8><td>$row[1] <td align=center> $row[2] <td align=center> $row[3]/20";
echo "</table>";
}
echo "<table border=1 align=center><tr class=style2><td width=300>Student Name <td> Test<br> Question <td> Score";
while($row=mysql_fetch_row($rs))
{
echo "<tr class=style8><td>$row[1] <td align=center> $row[2] <td align=center> $row[3]/20";
}
echo "</table>";
the problem I am encountering is it doesn't function properly. The default arrangement of the data is listed by the latest exam taker to the last. Now what I am aiming for is, if the user checks the by the name option, it will sort by name. for the by exam, it will be listed by exam.
output:
Your input radio button name="sort" so you need to use $_POST['sort'].
You can use input value as database field name so that we can easily use it in query without if...else condition.
echo "<table border=1 align=center><tr class=style2>
<tr><td><input type=radio name='sort' value='byname'>Sort By Name</td>
<td><input type=radio name='sort' value='byexam'>Sort By Exam</td></tr></table>";
$sort = isset($_POST['sort']) ? $_POST['sort'] : "test_id"; // change of $_POST['name'] to $_POST['sort']
$sortQuery = mysql_query("select * from mst_adminresult order by ".$sort." ASC",$cn) or die(mysql_error());
while($row=mysql_fetch_row($sortQuery))
{
echo "<table border=1 align=center><tr class=style2><td>Student Name</td><td>Test<br> Question</td><td>Score</td></tr>";
echo "<tr class=style8><td>$row[1]</td> <td align=center> $row[2]</td> <td align=center> ". ($row[3]/20) ."</td></tr> ";
echo "</table>";
}
echo "<table border=1 align=center><tr class=style2><td width=300>Student Name</td><td> Test<br> Question </td><td> Score</td></tr>";
while($row=mysql_fetch_row($rs))
{
echo "<tr class=style8><td>$row[1]</td> <td align=center> $row[2]</td> <td align=center> ". ($row[3]/20) ."</td></tr> ";
}
echo "</table>";

Select an sql field from html field and show it in another page

I have displayed sql table in html table, made a hyperlink near all fields, when i click it the whole field details should be shows in other page(ie; i show only 2 fields of sql in the table and want to show rest in another page).
admin.php
<?php
$con= mysql_connect("localhost","root","");
mysql_select_db("main",$con);
echo"<form action=\"post\" class=\"form-horizontal\" role=\"form\">";
echo "<table width='700' height='150' onclick='myFun(event)'>";
echo" <tr>
<td width='100' align='center'></td>
<td width='100' align='center'><b><u>NAME</u></b></td>
<td width='100' align='left'><b><u>E-MAIL</u></b></td>
</tr>
";
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
echo"</form> ";
?>
viewadmin.php
<?php
$name = $_GET['name'];
$result=mysql_query("SELECT NAME,DOB,MOB,EMAIL, FROM admin WHERE NAME = $name");
if (false === $result) {
echo mysql_error();
}
else {
$row=mysql_fetch_row($result);
}
echo" <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>Name</td>
<td align='left'>".$row['NAME']."</td>
</tr>
<tr>
<td align='left'>E-mail</td>
<td align='left'>".$row['EMAIL']."</td>
</tr>
<tr>
<td align='left'>D.O.B</td>
<td align='left'>".$row['DOB']."</td>
</tr>
<tr>
<td align='left'>Mobile</td>
<td align='left'>".$row['MOBILE']."</td>
</tr>
<tr>
<td align='left'>Photo</td>
<td ><img src='uploads/grumpy.jpg' height='200' width='200'></td>
</tr>
</table>";
echo"</form> ";
?>
do something like this:
admin.php
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result)) {
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
and in viewadmin.php
$name = $_GET['name'];
$result=mysql_query("SELECT * FROM admin WHERE name = $name");
$row=mysql_fetch_row($result);
echo " <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>...";
first rename the html page by php page, then you can pass the primary key or any key of the row from first page to admin page with the help of GET.
for eg:
first.php
<?php
$result=mysql_query("select ID,NAME,EMAIL from admin order by AID"); while($row=mysql_fetch_array($result)){
?><a hre='admin.php?id="$id=<?php $row[0] ?>"'></a>
<?php
}
?>
and in the admin.php page
you can access the value like
echo $_GET['id'];
stop using MySQL and use MySQLi, this code should work
<?php
$db_connect = mysqli_connect('localhost', 'root', 'pass', 'database');
if (mysqli_connect_errno($db_connect)) {
die('Some error occurred during connection to the database');
}
$name = mysqli_real_escape_string($db_connect,$_REQUEST['name']);
if($stmt = mysqli_prepare($db_connect, 'SELECT * FROM admin WHERE name = ?')){
mysqli_stmt_bind_param($stmt, 's', $name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) !== 0){
$row = mysqli_fetch_assoc($result);
echo "<form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>..."
}
else{
echo 'not found';
}
}
else{
trigger_error('error:' . mysqli_errno($db_connect) . mysqli_error($db_connect));
}
?>

Sorting mysql results with pagination in php

Good morning everyone!
I've been working on a little project and I thought I was done. So, I have a bunch of pages, but I have one where there's a MYSQLi query with over 100k results, I figured it would be nice not to display all 100k+ results, so I added pagination to the site. The problem I am having is, whenever someone sorts the page and clicks next page, the sort goes away.
As I was writing this, I kind of sort of figured it out, but here's my code:
$sort = "id";
if(isset($_GET['sort'])) {
switch ($_GET['sort'] ) {
case 0:
$sort = 'id';
break;
case 1:
$sort = 'priority DESC';
break;
case 2:
$sort = 't_name';
break;
case 3:
$sort = 'loc';
break;
case 4:
$sort = 'w_req';
break;
case 5:
$sort = 'tel';
break;
case 6:
$sort = 'maint_user';
break;
}
}
// pagination
$total_results = mysqli_num_rows(mysqli_query($mysqli,"SELECT w_status FROM w_o WHERE (w_status = 'active' OR w_status = 'open') ORDER BY $sort"));
if(!isset($page_number))
$page_number = (int)$_GET['page'] <= 0 ? 1 : (int)$_GET['page']; // grab the page number
$perpage = 15; // number of elements perpage
if($page_number > ceil($total_results/$perpage))
$page_number = ceil($total_results/$perpage);
$start = ($page_number - 1) * $perpage;
$result = mysqli_query($mysqli,"SELECT * FROM w_o WHERE (w_status = 'active' OR w_status = 'open') ORDER BY $sort LIMIT $start, $perpage");
I think my issue is that $sort= "id"; and every time I click on next, the $sort gets reset to = "id" So I am thinking, maybe include the $sort in the pagination code? I appreciate any help I can get. Thank you :)
Here's the html:
<form action="re_assign_a.php" method="post" name="view_order">
<table border="0" width="100%">
<tr>
<td align="left">Previous</td>
<td align="right">Next</td>
</tr>
</table>
<br>
<table border='2' style='width: 100%; margin: auto; border-width: 1px'>
<tr>
<th><span title="More">Order #</span></th>
<th><span title="More">Priority</span></th>
<th><span title="More">Tenant Name</span></th>
<th><span title="More">Apartment</span></th>
<th><span title="More">Work Requested</span></th>
<th><span title="More">Resident Phone #</span></th>
<th><span title="More">Assigned to</span></th>
<th>Check to Re-Assign</th>
</tr>
<?php
$i = 0;
while($row = mysqli_fetch_array($result))
{
$test_id = $row['id'];
echo "<tr class='table_work'>";
echo "<td align='center'><a href='one_order.php?id=$test_id'>" . $row['id'] . "</td>";
echo
"<td align='center'><a href='one_order.php?id=$test_id'>";
if ($row['priority'] == '1') { echo "<div class='priority1'> </div>"; } elseif ($row['priority'] == '2') { echo "<div class='priority2'> </div>"; } elseif ($row['priority'] == '3') { echo "<div class='priority3'> </div>"; };
echo "</a></td>
<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo1 ($row['t_name']); echo "</a></td>
<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo1 ($row['loc']); echo "</a></td>
<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo($row['w_req']); echo "</a></td>";
echo "<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo2($row['tel']); echo "</a></td>";
echo "<td align='center'><a href='one_order.php?id=$test_id'>" . $row['maint_user'] . "</a></td>";
echo "<td align='center'><input name='assign[$i]' type='checkbox' value='" . $row['id'] . "' ></td>";
++$i;
}
?>
</table><br>
<table border="0" width="100%">
<tr>
<td align="left">Previous</td>
<td align="right">Next</td>
</tr>
</table>
You just need to adjust your links for going to the pages to include the sort order as a GET parameter. Store the GET parameter as a variable defaulted to zero
$sortOrder = $_GET['sort'] ? : 0;
<td align="left">Previous</td>
<td align="right">Next</td>
Now when the next or previous page link is clicked, the page number and the sort order are passed to the server.
You have to extend the URLs in your HTML. That's a quick & dirty solution! You have to define the variables in the code correctly.
Next page URL
<td align="left">Previous</td>
<td align="right">Next</td>
And the sort URL
<th><span title="More">Order #</span></th>

how to give color to table tr?

Hello i have a table with some fields like
here i want make colors for table entire rows..means if ASR value is 75 to 100 should get one color and 50 to 75 should get another color and below 50 should get another color.
and here is my php code
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".(($attenedcalls/$row['totalcalls'])*100)."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
thanks in advance
You can try like this
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
$color = '';
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
$asr = (($attenedcalls/$row['totalcalls'])*100);
if($asr >= 75 && $asr <=100 ){
$color = 'red';
}else if($asr >= 50 && $asr < 75){
$color = 'cyan';
}else if($asr < 50){
$color = 'blue';
}
}
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr style='background-color : ".$color."'>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".$asr."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
[...]
while ($row = mysql_fetch_assoc($result)) {
$asrVal=(($attenedcalls/$row['totalcalls'])*100);
if($asrVal>=50 && $asrVal <=75) $class="from50to75";
if($asrVal>=75 && $asrVal <=100) $class="from75to100";
if($asrVal<50) $class="below50";
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr class='$class'>
[...]
then add:
<style>
tr.from50to75 td{background-color:red;}
tr.from75to100 td{background-color:green;}
tr.below50 td{background-color:blue;}
</style>
Modify your while loop so that you compute the ASR value before emitting the <tr> tag. Use that value to select a class according to the classification you have set up, and emit a tag of the form <tr class=foo> where foo is the class name you have selected. Then it’s just a matter of writing CSS rules for the classes, using class selectors like tr.foo.
(Provided that you have not set color on the td cells. If you have, you need to use selectors like tr.foo td to override such settings.)

hyperlink on every row of mysql data

<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if ($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th>
<th >VIEW</th>
<th >ID</th>
<th >DATE</th>
<th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td>
<input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id />
</td>
<td>
$id
</td>
<td>
view
</td>
<td>
$date
</td>
<td>
$ponumber
</td>
</tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete'
value='Delete Selected Items'/></p></form>";}
?>
i have a sort of an online order form which enable the sales rep to input sales order,
i have done the insert and delete using the code above now i want every row to be a hyperlink so that when they click view it will display only row that has been clicked, in my code above if you click :view" all the detail will display, how can i display only the row that i will click will display the detail of the record!
you need to pass the id in the url and you need to read it if it's there.
e.g.
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
/* Edit 1 */
if (!empty($_GET['id'])) {
$query .= " WHERE id = " . mysql_real_escape_string($_GET['id']);
}
/* Edit 1 end */
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th><th >VIEW</th><th >ID</th><th >DATE</th><th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id /></td>
<td>$id</td>
<td>";
/* Edit 2 */
echo "<a href='view.php?id=$id'>view</a>";
/* Edit 2 End */
echo "</td>
<td>$date</td>
<td>$ponumber</td></tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";}
?>
A style suggestion:
Don't do/stop doing this:
echo "<form method='post' action='delete.php'>";
echo ...
while
Where what you are echoing is a static string. Instead, do:
?>
<form method='post' action='delete.php'>
...
<?php
while
it's simply easier to read and maintain.

Categories