SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Nov 4, 2009, 10:15 #1
- Join Date
- May 2007
- Posts
- 21
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Navigating in a table and delete row
Hi!
This is what I want to do:
At the moment I have a table and I can navigate to a page where I can delete rows.
What I want is that on the page where you can delete rows, I want to have two images (two arrows, one up and one down) which can navigate in the table. When going to the page where you can delete rows, the buttom row should initially be selected. Then you can navigate through the rows and delete the selected row, using the "Delete selected row".
If this could help
Delete function:
PHP Code:<?php
/*
DELETE.PHP
Deletes a specific entry from the 'players' table
*/
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
$id = $_GET['id'];
// delete the entry
$result = mysql_query("DELETE FROM players WHERE id=$id")
or die(mysql_error());
// redirect back to the view page
header("Location: view.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: view.php");
}
?>
PHP Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW.PHP
Displays all data from 'players' table
*/
// connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM players")
or die(mysql_error());
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
echo '<p><a href="delete_all.php?lastname=' . 10 . '">Delete All</a></p>';
?>
<p><a href="new.php">Add a new record</a></p>
</body>
</html>
Best regards,
Johan
-
Nov 4, 2009, 10:25 #2
- Join Date
- Oct 2009
- Posts
- 1,852
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Why do you need such complicated desidn?
Yes, it can be done with both PHP and Javascript (preferred)
But why don't you make it as simple, as just button "Delete" on each row?
One click action. No arrows, no moves.
-
Nov 4, 2009, 10:30 #3
- Join Date
- May 2007
- Posts
- 21
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I´m going to do a user test on an old system, so at the moment I have to do a replication. Would be nice to have some help=) Thank you very much for your reply!
Edit: It should use PHP
-
Nov 4, 2009, 10:38 #4
- Join Date
- Oct 2009
- Posts
- 1,852
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
*shrug*
This can be easily done with PHP.
As simple as schoolbook's pagination example.
Just put a link on the arrow which carries something like highlight=$hid
where $hid is id for line to be highlightd after pressing arrow.
When output your table, just compare $row['id'] to $_GET['highlight'] and if matched, change coloe of the row.
I didn't get the point of such an interface anyway.
-
Nov 4, 2009, 10:48 #5
- Join Date
- May 2007
- Posts
- 21
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I get what you´re saying. Because the user can add and delete as he wants, the id of the rows will always be different from time to time.
So at first the highlighted row when going to the page should be the latest id, then the biggest.
Do you know what code to use?
I don´t understand how it works to link the arrow. Because it it starts to highlight the buttom row ID(max) then when clicking on the upper arrow, it should link to ID(max)-1 and then, newt tim it should link to ID(max)-2... etc.
Would be nice to have some help with coding, I´m kind of a noob..
Thanks for your reply!!
-
Nov 4, 2009, 11:03 #6
- Join Date
- Oct 2009
- Posts
- 1,852
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Never do arithmetical operations to id. It is not a number. Consider it is string with no meaning. Cause it is.
If id changed so fast, pass relative position on the table. set it to table height at first and now you can do that math, +1 -1 stuff.
When output table do an increment on counter varibale and compare it with passed value.
Bookmarks