Jump to content

[SOLVED] table display


php_novice2007

Recommended Posts

Hi,

 

I've got a mysql table called users with attributes "id", "name", "group", "last login". I've got a page where I first displays a HTML table containing all the users information followed by a form where each id is displayed and a check box is next to it.

 

Currently I've just used a "select * from users" statement to extract all the users (ordered by id) and display their information on the HTML table. I've also saved the ids in a PHP array so that I can go through it and generate the form after displaying the table.

 

Now I want to improve the display of the table. I want to add a drop down list on the page with options "id", "name", "group", and "last login" where depending on which one the user selects, the table will display the user information sorted by the option chosen. 

 

I'm not sure how to go about doing that.. I suppose I'll have to call a javascript function whenever the drop down option changed, and somehow redisplay the table. But how? I have to send another query to the database but I don't want to leave the page, and I need to keep at least the id sorted since I need it to generate the form.. Should I somehow form javascript objects (no idea how I would do that) and whenever the list changed write code to reorder the objects and then display the information?

 

Thanks for any help~!

Link to comment
Share on other sites

if you want to just re-order the query to instead of ordering by ID, order by NAME or something, then you can use PHP.

 

you could have in your address bar something like this: memberlist.php?order=name

 

so you would grab that from that address bar:

 

if(!isset($_GET['order'])){ $order = "id"; }else{ $order = $_GET['order']; }

 

then you can set your query up like this:

$query = mysql_query("SELECT * FROM `users` ORDER BY {$order}");

 

then for your drop-down box use a onchange event so that when they change the selected option it just changes the order in the address bar. Search www.javascriptkit.com for a script..

 

hope thats what you were after.

Link to comment
Share on other sites

ohh so basically I have an onchange listener on the drop down list which calls up a javascript function which changes the address bar and get the page to reload?

 

hm unfortunately the site you recommanded doesn't have anything that can change the address bar, and nothing useful is coming up on Google either.. one page actually said JavaScript can't change the address bar  ???

Link to comment
Share on other sites

I think what I want to do is similar to when you view emails you can click on either "date" or "from" or "subject" etc and it sorts your emails for you according to what you choose.

 

I want to do that to a table, the default is sorting by id, but if the user selects something from a drop down list (on the same page as the table) I want the table to change to have its rows sorted by what the user choose.

 

 

Link to comment
Share on other sites

ah ok, I think you may be able to do this with a switch statement to check what the user has chosen to sort by, and depending on what one they choose, you have the same MySQL query for each section inside the switch, only changing the ORDER BY part in each one.

 

Regards ACE

Link to comment
Share on other sites

here is how you would do the onchange with the drop down box..

 

echo <<<html
<select name="dropdown" id="dropdown" onchange="javascript:window.location='?order='+document.getElementById('dropdown').options[document.getElementById('dropdown').selectedIndex].value">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
html;

 

Customize to how you want...

Link to comment
Share on other sites

if your going to do that - (send the information to the browser anyway) try this instead - instead of a drop down to switch the order - they click on the table header and that sets the sort order.:

 

<?php
$link1 = "./?page=users";
$link1 = "./?page=users&sort=name";
$link2 = "./?page=users&sort=group";
$link3 = "./?page=users&sort=llogin";

//Determine the sort order
if (isset($_GET['sort'])) {
	// Use existing sorting order.
switch ($_GET['sort']) {
	case 'name':
		$order_by = 'name ASC';
		$link2= "./?page=user&sort=named";
		break;
	case 'named':
		$order_by = 'name DESC';
		$link2= "./?page=user&sort=name";
		break;
	case 'group':
		$order_by = 'group ASC';
		$link3 = "./?page=user&sort=groupd";
		break;
	case 'groupd':
		$order_by = 'group DESC';
		$link3 = "./?page=user&sort=group";
		break;
	case 'llogin':
		$order_by = 'login ASC';
		$link4 = "./?page=user&sort=llogind";
		break;
	case 'llogind':
		$order_by = 'login DESC';
		$link4 = "./?page=user&sort=llogin";
		break;
	default:
		$order_by = 'id DESC';
                        $link1="./?page=user";
		break;
}
$sort = $_GET['sort'];
} else { // Use the default sorting order.
$order_by = 'id ASC';
$sort = 'id';
}
if (!isset($_POST['submit'])) { //this just assumes you can do something more then view users with this form
$query = "SELECT user.*,CONCAT(user.first_name, ' ', user.last_name) as name FROM user ORDER BY $order_by;";
$result = mysql_query($query);

echo '
<table cellspacing="3">
	<tr>
		<td><b><a href="' . $link1 . '">ID</a></b></td>
		<td><b><a href="' . $link2 . '">Name</a></b></td>
		<td><b><a href="' . $link3 . '">Group</a></b></td>
		<td><b><a href="' . $link4 . '">Last Login</a></b></td>
	</tr>';
// Fetch and print all records
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	echo '
	<tr>
	<td>' . $row['id'] . '</td>
	<td>' . $row['name'] . '</td>
	<td>' . $row['group'] . '</td>
	<td>' . $row['last_login'] . '</td>
	</tr>';
} // end while
echo '</table>';
} // end if
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.