Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-13-2012, 09:21 PM   PM User | #1
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
displaying a list from db, allowing users to 'sort by':...

Hello,

I am creating a site and want to display the members list on a separate page. I want the user to be able to re-order this list by alphabet, by join date etc etc but I am not sure how to go about creating it.

Do I create a html form or do I need to use javascript? Or neither?

I guess I would have to create different functions to deal with the re-ordering? One to re-order to alphabetical, one to re-order to join date?

Can anyone guide me on the best way to achieve this?

I'm really stumped at the moment been trying to think of a way for a good hour now but I find a potential issue with everything I have thought of.

Thanks for any help in advance.

Kind regards,

LC.

Last edited by LearningCoder; 09-13-2012 at 09:22 PM.. Reason: added questions.
LearningCoder is offline   Reply With Quote
Old 09-13-2012, 09:34 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
You'll have two links at the top of the column: asc | desc

<a href="sort.php?o=asc">asc</a> | a href="sort.php?o=desc">desc</a>

sort.php simply saves their choice as a cookie and returns back to the list.

Each time the list is displayed, it looks at the sort cookie and depending on
which one they pick, the script will change the ORDER BY in the query.
mlseim is offline   Reply With Quote
Old 09-13-2012, 09:53 PM   PM User | #3
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Ah thank you very much.

So I only need 1 set of asc|desc links? Do I need to have a drop down box with the values Join Date and Alphabetical?

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-13-2012, 10:40 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
ASC/DESC can be set toggled when using a link to draw your data. Typically you sort only by one specific type, so my querystring would include something like sort=field&dir=desc, where if no direction is set it assumes ASC.
Then you construct your links for each sortable header, and always provide a sort=fieldname. Then with the direction, you can either add it as whichever default you want, but the trick is to check and see if a sort is already selected, then invert the direction it goes for when you create that link.

Datagridviews are also used exclusively for these purposes. They are handled with a lot of ajax as well as PHP. You may be able to google up a generic data grid view for PHP.
Fou-Lu is offline   Reply With Quote
Old 09-14-2012, 12:31 AM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Is that like drastic data grid?

http://www.drasticdata.nl/DrasticTools/ExampleGrid1.php

PHP/AJAX/MySQL?
mlseim is offline   Reply With Quote
Old 09-14-2012, 01:06 AM   PM User | #6
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
So, simply put, I need to use AJAX to retrieve the data from the database so then it can dynamically reload within the page?

That link is pretty much exactly what I am trying to achieve, apart from I only want to display the username and join date.

Regards,

LC.

Last edited by LearningCoder; 09-14-2012 at 01:08 AM..
LearningCoder is offline   Reply With Quote
Old 09-14-2012, 04:37 AM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by LearningCoder View Post
So, simply put, I need to use AJAX to retrieve the data from the database so then it can dynamically reload within the page?

That link is pretty much exactly what I am trying to achieve, apart from I only want to display the username and join date.

Regards,

LC.
You don't need AJAX. You only need it if you want it to load in the background. The same effect can be created statically with using just links and the querystring.

Quote:
Originally Posted by mlseim View Post
Is that like drastic data grid?

http://www.drasticdata.nl/DrasticTools/ExampleGrid1.php

PHP/AJAX/MySQL?
Yep, that's exactly what it is. Datagrid views are most common in C#/ASP.NET, as its built into the language. I'd assume that is created to mimic it in PHP.
Fou-Lu is offline   Reply With Quote
Old 09-14-2012, 12:49 PM   PM User | #8
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Yea, i want it to dynamically load without having to reload the page. I'll read into retrieving data with AJAX, then I'll look into AJAX with PHP. I read my JS book last night which has a whole chapter dedicated to AJAX. I know the basic syntax to setup the data retrieval, but it's just working with the AJAX and PHP which I know absolutely nothing about.

Also, because I want the data to display dynamically, do I display the results with PHP first, then use the AJAX to change it? Or do I have to dynamically load the PHP with AJAX first, then change again with AJAX?

Here is the code I have at the moment:
PHP Code:
<div id="content">

   <div id="order_links_div">
       <a href="sort.php?o=asc">ASC</a>|<a href="sort.php?o=desc">DESC</a>
   </div>

<table id="members_table" name="members_table">
  <tr>
     <th>Member No.</th>  
     <th>Username</th>  
     <th>Join Date</th>
  </tr>     
  <?php
  
require ("connectdb.php");
  
$sql mysql_query("SELECT * FROM members");
  if(!
$sql){
     echo 
"There was an error retrieving the data.";
     exit(
0);
  }
  
  while(
$row mysql_fetch_array($sql)){
     echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
  }
  
  
?>
</table>

</div>
I'm guessing this is not the right way because I don't think this will change the data around because it is coded to display anything retrieved from the database in that order.....

My question is, do I have to load the database results as soon as the 'members.php' page loads using PHP, or do I use AJAX from the start?

Hope you can understand my issue.

Thanks for all your replies.

Regards,

LC.

Last edited by LearningCoder; 09-14-2012 at 12:52 PM..
LearningCoder is offline   Reply With Quote
Old 09-14-2012, 07:03 PM   PM User | #9
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Here is my HTML:
Code:
 <a href="sort.phpo=asc" onclick="get_file()">ASC</a>|
	   <a href="sort.php?o=desc" onclick="javascripts/ajax_db_call.js">DESC</a>
Here is my ajax_db_call.js file:
Code:
function get_file(){

var xmlhttp;

if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

xmlhttp.open("GET","sort.php",true);
xmlhttp.send();
}
Here is my sort.php:
PHP Code:
<?php

$order 
$_GET['o'];

if(!isset(
$order)){
   exit(
0);
}

require(
"connectdb.php");


if(
$order == "asc"){
   
$sql mysql_query("SELECT * FROM members ORDER BY username");
   
   if(!
$sql){
      echo 
"error retrieving data";
      exit(
0);
   }
   
   while (
$row mysql_fetch_array($sql)){
   
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   
   
   }
   
   
}

?>
Anyone know what I am doing wrong?

Regards,

LC.
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:56 AM.


Advertisement
Log in to turn off these ads.