Jump to content


Sorting members by


5 replies to this topic

#1 Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 18 July 2007 - 06:19 AM

I have a member list page, called profile.php. It is something like this

# Username Registration date User's status
1 name 18th, Jul, 2007 12:25 (12:25 AM) Member

in a table. All is well, displays all the users on my site. But I thought:"If my site will have 100+ members, no one would like to search trough an assload of members, just to find one user. I need sorting. Yeah, good idea, but how?"

Tried some coding that didn't work (and I didn't think it would)...
Here be the code

<?php
session_start();
?>
<html>
<head>
<title>Techgeeks.oo.lv - from websites to graphics, to music</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<style type='text/css'>
a:hover { background-color: #666; }
a { text-decoration:none }
</style>
</head>
<body link=#000 alink=#666 vlink=#666>
<?php include('header.php'); include('config.php'); ?>
<div id=wrapper>
<div id=main>
Here, you can see what users have registered.
<?php //Dispaying the users
$query = "SELECT * FROM users"; //Selecting everything what you could want to show
$result = @mysql_query($query);
echo "<table>
<tr>
<th>#</th>
<th>Username</th>
<th>Registration date</th>
<th>User's status</th>
</tr>";

while($row = mysql_fetch_array($result)) //Stops the headers to show after each user
  { //Display all users
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['Username'] . "</td>";
  echo "<td>" . $row['Date'] . "</td>";
  echo "<td>" . $row['Status'] . "</td>";
  echo "</tr>";
  }
echo "</table>"; //Showing the users
?>
?>
</div>
</div>
</body>
</html>

So, I want it to be something like a form below the users "Sort by (members, administrators, VIP Members). And a submit button. The problem is, I don't know how to do that, and I am confused in what I need: $_GET or $_POST.

Then, if that wouldn't work, I could always try links (that is, click on User's status and it sorts by alphabet), but that seemed a lot of coding, which I don't know.




Also, what would be the correct code to make links like these profiles.php?id=1 display the users profile? And how to display the information on that user, not everyone?

Sorry for the questions, I am new to SQL + PHP.

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 18 July 2007 - 01:13 PM

Simple really.

<?php
session_start();
?>
<html>
<head>
<title>Techgeeks.oo.lv - from websites to graphics, to music</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<style type='text/css'>
a:hover { background-color: #666; }
a { text-decoration:none }
</style>
</head>
<body link=#000 alink=#666 vlink=#666>
<?php include('header.php'); include('config.php'); ?>
<div id=wrapper>
<div id=main>
Here, you can see what users have registered.
<?php //Dispaying the users
$query = 'SELECT * FROM `users`'; // Query base
if(isset($_GET['id']) && is_numeric($_GET['id']))
  $query .= ' WHERE `id` = '.(int)$_GET['id']; // Get individual user
elseif(isset($_GET['show'])){
  switch(strtolower($_GET['show'])){
	case 'members':
	  $query .= ' WHERE `status` = 'Member'; // Show members
	  break;
	case 'admin':
	  $query .= ' WHERE `status` = 'Administrator'; // Show admins
	  break;
	default:
	  break;
  }
}
if(isset($_GET['sort'])){
  switch(strtolower($_GET['sort'])){
	case 'id':
	  $query .= ' ORDER BY `id` ASC'; // Sort by id
	  break;
	case 'username':
	  $query .= ' ORDER BY `Username` ASC'; // Sort by username
	  break;
	case 'date':
	  $query .= ' ORDER BY `Date` ASC'; // Sort by date
	  break;
	case 'status':
	  $query .= ' ORDER BY `Status` DESC'; // Sort by status
	  break;
	default:
	  break;
  }
}

$result = mysql_query($query) or die(mysql_error());
echo "<table>
<tr>
<th><a href="?sort=id">#</a></th>
<th><a href="?sort=username">Username</a></th>
<th><a href="?sort=date">Registration date</a></th>
<th><a href="?sort=status">User's status</a></th>
</tr>";

while($row = mysql_fetch_array($result)){ //Stops the headers to show after each user
  //Display all users
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['Username'] . "</td>";
  echo "<td>" . $row['Date'] . "</td>";
  echo "<td>" . $row['Status'] . "</td>";
  echo "</tr>";
}
echo "</table>"; //Showing the users
?>
Show: <a href="?show=members">Members</a> | <a href="?show=admins">Administrators</a>
</div>
</div>
</body>
</html>

That will allow you to show an individual user, or to sort them all by whatever column by clicking on the table header.

Also, thel inks at the bottom will allow you to select a certain group. I don't know exactly how you name them in your database, so you will have to edit and continue the switch statement for adding the WHERE clause to the query.

Your next step would be to add pagination, which you can find PLENTY of tutorials about.

Edited by Demonslay, 18 July 2007 - 01:18 PM.


#3 Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 18 July 2007 - 03:01 PM

Wow, much and big thanks!! :P
It works with 110% power :D.

But, is there a way to make ?id=1 to display a profile page, not a list?

<?php
session_start();
?>
<html>


<head>
<title>Techgeeks.oo.lv - Profile pages concept</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<style type='text/css'>
a:hover { background-color: #666; }
a { text-decoration:none }
</style>
</head>

<body>
<?php include('header.php'); include('config.php'); ?>
<div id=wrapper>
<div id="head"><!-- Please assist! How to get the users name, which profile I am viewing? Something with $_GET['id'] I guess... -->'s profile</div>
<?php 
$query = mysql_query("SELECT * FROM `users`")or die(mysql_error()); //Selecting all the info user has given
$result = mysql_query($query) or die(mysql_error()); 
//Dispaying users profile
echo '<table>
	<tr><th>About the user</th><th>&nbsp;</th></tr>';
while($row = mysql_fetch_array($result)){
echo '<td>Username</td><td>'.$row['Username'].'</td>
	<td>Status</td><td>'.$row['Status'].'</td>
	<td>Registration date</td><td>'.$row['Date'].'</td>
	<td>Location</td><td>'.$row['Location'].'</td>
	<td>Interests</td><td>'.$row['Interests'].'</td></table>';
}
?>
</div>
</body>

</html>


The code, whilst wrong, is much like what I want to show.

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 18 July 2007 - 09:44 PM

Simple.

<?php
session_start();
?>
<html>
<head>
<title>Techgeeks.oo.lv - from websites to graphics, to music</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<style type='text/css'>
a:hover { background-color: #666; }
a { text-decoration:none }
</style>
</head>
<body link=#000 alink=#666 vlink=#666>
<?php include('header.php'); include('config.php'); ?>
<div id=wrapper>
<div id=main>
<?php
$query = 'SELECT * FROM `users`'; // Query base
if(isset($_GET['id']) && is_numeric($_GET['id'])){
  $query .= ' WHERE `id` = '.(int)$_GET['id']; // Get individual user
  $result = mysql_query($query) or die(mysql_error());
  $row = mysql_fetch_assoc($result);
  echo '<table>
    <tr><th>About the user</th><th>&nbsp;</th></tr>
    <td>Username</td><td>'.$row['Username'].'</td>
    <td>Status</td><td>'.$row['Status'].'</td>
    <td>Registration date</td><td>'.$row['Date'].'</td>
    <td>Location</td><td>'.$row['Location'].'</td>
    <td>Interests</td><td>'.$row['Interests'].'</td>
  </table>';
}
else{
  if(isset($_GET['show'])){
    switch(strtolower($_GET['show'])){
      case 'members':
        $query .= ' WHERE `status` = 'Member'; // Show members
        break;
      case 'admin':
        $query .= ' WHERE `status` = 'Administrator'; // Show admins
        break;
      default:
        break;
    }
  }
  if(isset($_GET['sort'])){
    switch(strtolower($_GET['sort'])){
      case 'id':
        $query .= ' ORDER BY `id` ASC'; // Sort by id
        break;
      case 'username':
        $query .= ' ORDER BY `Username` ASC'; // Sort by username
        break;
      case 'date':
        $query .= ' ORDER BY `Date` ASC'; // Sort by date
        break;
      case 'status':
        $query .= ' ORDER BY `Status` DESC'; // Sort by status
        break;
      default:
        break;
    }
  }

  $result = mysql_query($query) or die(mysql_error());
  echo "<table>
  <tr>
  <th><a href="?sort=id">#</a></th>
  <th><a href="?sort=username">Username</a></th>
  <th><a href="?sort=date">Registration date</a></th>
  <th><a href="?sort=status">User's status</a></th>
  </tr>";

  while($row = mysql_fetch_assoc($result)){
    //Display all users
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['Username'] . "</td>";
    echo "<td>" . $row['Date'] . "</td>";
    echo "<td>" . $row['Status'] . "</td>";
    echo "</tr>";
  }
  echo "</table>"; //Showing the users
?>
  Show: <a href="?show=members">Members</a> | <a href="?show=admins">Administrators</a>
<?php } ?>
</div>
</div>
</body>
</html>


#5 Edmachine

    Young Padawan

  • Members
  • Pip
  • 44 posts
  • Gender:Male
  • Location:Latvia

Posted 19 July 2007 - 01:47 PM

Thank you very, very, very, VERY much! :) :)

I can't beleive that it is actually simple... how didi you learn that stuff?

#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 19 July 2007 - 03:49 PM

All I really did was wrap the code in an extra set of if/else conditionals really. :)

And I learned PHP on my own, like most on this forum did (I assume). Really is a simple language to grasp compared to others, lol.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users