Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

table with detail and delete options 1

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
I have a table with selected fields (see code below). I want to add two more columns (Detail and Delete) with "clickable" links/buttons; 'detail' would show complete information (all fields) and 'delete' would change status from A to C (so user won't see it in the table next time)
'projects' table has unique ID field which can be used at WHERE condition to run queries.
Any ideas (or website with some tutorial) please?

...
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM projects WHERE USERID='$userid' AND STATUS='A'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2" width="750">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Project</font></th>
<th><font face="Arial, Helvetica, sans-serif">Model</font></th>
<th><font face="Arial, Helvetica, sans-serif">Width [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Span [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total Length [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date, Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"project");
$f2=substr(mysql_result($result,$i,"model"),0,4);
$f3=substr(mysql_result($result,$i,"model"),4,1);
$f4=mysql_result($result,$i,"span");
$f5=mysql_result($result,$i,"totlength");
$f6=mysql_result($result,$i,"timestmp");
$f7=mysql_result($result,$i,"gtotal");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
</tr>

<?php
$i++;
}
?>
...
 
Adding the extra columns should be straight forward.

for the Detail part, you can use a regular link that contains the id for the row you wish to show, and then use that in your Details page to retrieve the data from the table and display it?

Something like:

Code:
$f8=mysql_result($result,$i,"project_id");
...
<td><font face="Arial, Helvetica, sans-serif"><a href="details.php?id_row=<?php echo $f8; ?>">Details</a</font></td>


Then in your details.php page you can receive that value from the URL string in the GE variable as:

$row_id=$_GET['id_row'];

With that you can perform a query and retrieve all the information you need and display it. 


The Delete button should work pretty much the same except you perform a delete query with the received Id instead of a SELECT query. 


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thank you; i updated my code (see below).
can i somehow execute query delete (or rather update) right away (with prompt for confirmation) after user clicks 'delete' from this site instead of going to delete.php?

@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM projects WHERE USERID='$userid' AND STATUS='A'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2" width="750">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Project</font></th>
<th><font face="Arial, Helvetica, sans-serif">Model</font></th>
<th><font face="Arial, Helvetica, sans-serif">Width [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Span [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total Length [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date, Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total</font></th>
<th><font face="Arial, Helvetica, sans-serif">Detail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Delete</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"project");
$f2=substr(mysql_result($result,$i,"model"),0,4);
$f3=substr(mysql_result($result,$i,"model"),4,1);
$f4=mysql_result($result,$i,"span");
$f5=mysql_result($result,$i,"totlength");
$f6=mysql_result($result,$i,"timestmp");
$f7=mysql_result($result,$i,"gtotal");
$f8=mysql_result($result,$i,"id");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="details.php?id_row=<?php echo $f8; ?>">Details</a</font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="delete.php?id_row=<?php echo $f8; ?>">Delete</a</font></td>
</tr>

<?php
$i++;
}
?>
 
Not with PHP alone. If you want an automatic deletion, you'll need to use Ajax to have it interact with the DB without having to load a new page.

You could of course point the delete link to the same page that displays your results and using an If statement to check for the existence of the variable perform the deletion before displaying the rows again.

Code:
<td><font face="Arial, Helvetica, sans-serif"><a href="delete.php?id_row=<?php echo $f8; ?>[red]&action=delete[/red]">Delete</a</font></td>

Code:
if(isset($_GET['id_row'])&&isset($_GET['action'] && $_GET['action']=='delete')){

[blue]deletion code goes here[/blue]
}
[green]rest of display page code goes here[/green]



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks.

so if my current table/page is myquotas.php can i modify the code like this?

<td><font face="Arial, Helvetica, sans-serif"><a href="myquotas.php?id_row=<?php echo $f8; ?>&action=delete">Delete</a</font></td>

if(isset($_GET['id_row'])&&isset($_GET['action'] && $_GET['action']=='delete')){
mysql_query("UPDATE projects SET Status="C" WHERE ID='$id_row';", $connection);
}

(and then reload page with javaScript?)
 
Pretty Much. Though there's no need to reload. Once the delete has taken place and as long as its located above where you run the query that displays the table.
It should re display the table with the changes.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I got this error message:

Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in myquotas.php on line 190

line 190
if (isset($_GET['id_row'])&&isset($_GET['action'] && $_GET['action']=='delete')) {
mysql_query("UPDATE projects SET Status='C' WHERE ID='$id_row';", $connection);
}

whole code:

<?php
include('../global/includes/_db_info.php');
mysql_connect($dbserver,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$connection = mysql_connect($dbserver,$username,$password);

if (isset($_GET['id_row'])&&isset($_GET['action'] && $_GET['action']=='delete')) {
mysql_query("UPDATE projects SET Status='C' WHERE ID='$id_row';", $connection);
}

$userid=$_SESSION["uid"];

@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM projects WHERE USERID='$userid' AND STATUS='A'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2" width="750">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Project</font></th>
<th><font face="Arial, Helvetica, sans-serif">Model</font></th>
<th><font face="Arial, Helvetica, sans-serif">Width [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Span [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total Length [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date, Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total</font></th>
<th><font face="Arial, Helvetica, sans-serif">Detail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Delete</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"project");
$f2=substr(mysql_result($result,$i,"model"),0,4);
$f3=substr(mysql_result($result,$i,"model"),4,1);
$f4=mysql_result($result,$i,"span");
$f5=mysql_result($result,$i,"totlength");
$f6=mysql_result($result,$i,"timestmp");
$f7=mysql_result($result,$i,"gtotal");
$f8=mysql_result($result,$i,"id");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="details.php?id_row=<?php echo $f8; ?>">Details</a</font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="myquotas.php?id_row=<?php echo $f8; ?>&action=delete">Delete</a</font></td>
</tr>

<?php
$i++;
}
?>
</body>
</html>
 
OK, here is the working code:

<?php
include('../global/includes/_db_info.php');
mysql_connect($dbserver,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$connection = mysql_connect($dbserver,$username,$password);

if (isset($_GET['id_row']) && (isset($_GET['action']) && ($_GET['action']=='delete'))) {
mysql_query("UPDATE projects SET Status='C' WHERE ID='$id_row';", $connection);
}

$userid=$_SESSION["uid"];

@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM projects WHERE USERID='$userid' AND STATUS='A'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2" width="750">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Project</font></th>
<th><font face="Arial, Helvetica, sans-serif">Model</font></th>
<th><font face="Arial, Helvetica, sans-serif">Width [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Span [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total Length [ft]</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date, Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Total</font></th>
<th><font face="Arial, Helvetica, sans-serif">Detail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Remove</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"project");
$f2=substr(mysql_result($result,$i,"model"),0,4);
$f3=substr(mysql_result($result,$i,"model"),4,1);
$f4=mysql_result($result,$i,"span");
$f5=mysql_result($result,$i,"totlength");
$f6=mysql_result($result,$i,"timestmp");
$f7=number_format(mysql_result($result,$i,"gtotal"),2);
$f8=mysql_result($result,$i,"id");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="details.php?id_row=<?php echo $f8; ?>">Details</a</font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="myquotas.php?id_row=<?php echo $f8; ?>&action=delete">Remove</a</font></td>
</tr>

<?php
$i++;
}
?>

thanks for your help!
 
Sorry bout that I missed the closing ) for the second isset. Glad you found the error and were able to fix it.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top