Ok- Who knows how to do this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Ok- Who knows how to do this

Post by mohson »

I have noticed on a few other developer sites that alot of them have the functionality of being able to select the order of data ina coloum by clcicking on its heading - i.e lcoulum lists firstnames and when u clcick the coloum title FIRST NAME it automaticall arranges all the data in Ascending alphabetical order.

I want to do the same with all my coloum headers, but How would I go about doing this??

heres my code:

Code: Select all

// Define your colors for the alternating rows

$color1 = "#ADD8E6";$color2 = "#E0FFFF";

"<table width="50%" border="0" cellpadding="2" cellspacing="2">
    <tr>
        <td><b><small>RecNo</small></b></td>
        <td><b><small>ID</small></b></td>
        <td><b><small>Title</small></b></td>
        <td><b><small>First Name</small></b></td>
        <td><b><small>Surname</small></b></td>
        <td><b><small>Organisation</small></b></td>
        <td><b><center><small>Role</small></center></b></td>
        <td><b><small>Address(1)</small></b></td>
        <td><b><small>Address(2)</small></b></td>
        <td><b><small>City</small></b></td>
        <td><b><small>Post Code</small></b></td>
        <td><b><small>Telephone</small></b></td>
        <td><b><small>Mobile</small></b></td>
        <td><b><small>Fax</small></b></td>
        <td><b><small>Last Contact</small></b></td>
        <td><b><small>Contact Again</small></b></td>
        <td><b><small>Email</small></b></td>
        <td><b><small>Notes</small></b></td>";while ($row =
mysql_fetch_object($sql)) 

{($color==$color2)? $color = $color1 : $color = $color2;


echo "<tr bgcolor="$color"><td>".$count . '</td><td> ' . $row->person_id .'</td><td>'.        
    $row->salutation .'</td><td>'.         
    $row->firstname .'</td><td>'.         
    $row->surname .'</td><td>'.        
     $row->organisation .'</td><td>'.        
    $row->role.'</td><td>'.       
     $row->address1 .'</td><td>'.        
    $row->address2 .'</td><td>'.       
     $row->city .'</td><td>'.       
     $row->postcode .'</td><td>'.        
    $row->telephone .'</td><td>'.        
    $row->mobile .'</td><td>'.        
    $row->fax .'</td><td>'.        
    $row->dateoflastcontact.'</td><td>'.        
    $row->datecontactagain  .'</td><td>'.       
     $row->email .'</td><td>'.    
    $row->notes .'</td></tr>';

$count += 1;
}

echo "</table>";
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Make each of your column headers a link to the same page but set a post variable. For example, say you have column header "firstname" you do <a href="currentpage.php?sortBy=firstname">Firstname</a>.

Then, you tell your sql query to sort by that variable ($_POST['sortBy']). There are a few other things you'll want to do but that's the basics.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

This is another option which I found to be extremely cool. It's written in JavaScript, but does what you are looking for:

http://www.kryogenix.org/code/browser/sorttable/
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

protokol, you beat me to it. i was gonna post the exact same link lol
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

just be aware that if you combine pagination and sorttable, you might be displaying "incorrect" rows at a given time.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

ok,

so these are the 3 steps quoted on the site you guys have suggested:
1. Include the Javascript library, by putting a link to it in the HEAD of your page, like so:

<script src="sorttable.js">

2. Mark your table as a sortable one by giving it a class of "sortable":

<table class="sortable">

3. Ensure that your table has an ID:

<table class="sortable" id="unique_id">
I tried to incoporate this but the screen went blank


Can anyone give me any tips on how I put the above into my code:

Code: Select all

<?php


// config-------------------------------------
$host = "**********8"; //your database host
$user = "*******"; // your database user name
$pass = "********"; // your database password
$db = "contact_management_system"; // your database name

$filename = "people.html"; // name of this file
$option = array (5, 10, 20, 50, 100, 200);
$default = 10; // default number of records per page
$action = $_SERVER['PHP_SELF']; // if this doesn't work, enter the filename
$query = "SELECT * FROM people ORDER BY firstname";  // database query. Enter your query here
// end config---------------------------------

    
$opt_cnt = count ($option);

$go = $_GET['go'];
// paranoid
if ($go == "") {
$go = $default;
}
elseif (!in_array ($go, $option)) {
$go = $default;
}
elseif (!is_numeric ($go)) {
$go = $default;
}
$nol = $go;
$limit = "0, $nol";
$count = 1; 

echo "<form name="form1" id="form1" method="get" action="$action">\r\n";
echo "<select name="go" id="go">\r\n";

for ($i = 0; $i <= $opt_cnt; $i ++) {
if ($option[$i] == $go) {
echo "<option value="".$option[$i]."" selected="selected">".$option[$i]."</option>\r\n";
} else {
echo "<option value="".$option[$i]."">".$option[$i]."</option>\r\n";
}
}

echo "</select>\r\n";
echo "<input type="submit" name="Submit" id="Submit" value="Go" />\r\n";
echo "</form>\r\n";

$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect");
mysql_select_db ($db) or die ("Unable to select database $db");



// control query------------------------------
/* this query checks how many records you have in your table.
I created this query so we could be able to check if user is
trying to append number larger than the number of records
to the query string.*/
$off_sql = mysql_query ("$query") or die ("Error in query: $off_sql".mysql_error());
$off_pag = ceil (mysql_num_rows($off_sql) / $nol);
//-------------------------------------------- 


$off = $_GET['offset'];
//paranoid
if (get_magic_quotes_gpc() == 0) {
$off = addslashes ($off);
}
if (!is_numeric ($off)) {
$off = 1;
}
// this checks if user is trying to put something stupid in query string
if ($off > $off_pag) {
$off = 1;
}

if ($off == "1") {
$limit = "0, $nol";
}
elseif ($off <> "") {
for ($i = 0; $i <= ($off - 1) * $nol; $i ++) {
$limit = "$i, $nol";
$count = $i + 1;
}
} 




// Query to extract records from database.
$sql = mysql_query ("$query LIMIT $limit") or die ("Error in query: $sql".mysql_error()); 



// Define your colors for the alternating rows

$color1 = "#ADD8E6";$color2 = "#E0FFFF";
$color = $color2;echo 

"<table width="50%" border="0" cellpadding="2" cellspacing="2">
    <tr>
        <td><b><small>RecNo</small></b></td>
        <td><b><small>ID</small></b></td>
        <td><b><small>Title</small></b></td>
        <td><b><small>First Name</small></b></td>
        <td><b><small>Surname</small></b></td>
        <td><b><small>Organisation</small></b></td>
        <td><b><center><small>Role</small></center></b></td>
        <td><b><small>Address(1)</small></b></td>
        <td><b><small>Address(2)</small></b></td>
        <td><b><small>City</small></b></td>
        <td><b><small>Post Code</small></b></td>
        <td><b><small>Telephone</small></b></td>
        <td><b><small>Mobile</small></b></td>
        <td><b><small>Fax</small></b></td>
        <td><b><small>Last Contact</small></b></td>
        <td><b><small>Contact Again</small></b></td>
        <td><b><small>Notes</small></b></td>";while ($row = mysql_fetch_object($sql)) 






{($color==$color2)? $color = $color1 : $color = $color2;


echo "<tr bgcolor="$color"><td>".$count . '</td><td> ' . $row->person_id .'</td><td>'.        
    
    $row->salutation .'</td><td>'.         
    
    '<a href=mailto:'.$row->email.'>'.$row->firstname .'</a></td><td>'.
    '<a href=mailto:'.$row->email.'>'.$row->surname .'</a></td><td>'.       
     
    $row->organisation.'</td><td>'.   
    $row->role.'</td><td>'.       
     $row->address1 .'</td><td>'.        
    $row->address2 .'</td><td>'.       
     $row->city .'</td><td>'.       
     $row->postcode .'</td><td>'.        
    $row->telephone .'</td><td>'.        
    $row->mobile .'</td><td>'.        
    $row->fax .'</td><td>'.        
    $row->dateoflastcontact.'</td><td>'.        
    $row->datecontactagain  .'</td><td>'.          
    $row->notes .'</td></tr>';

$count += 1;
}

echo "</table>"; 

echo "<br /><br />\r\n";
if ($off <> 1) {
$prev = $off - 1;
echo "[ < <a href="$filename?offset=$prev&go=$go">prev</a> ] \r\n";
}
for ($i = 1; $i <= $off_pag; $i ++) {
if ($i == $off) {
echo "[<b> $i </b>] \r\n";
} else {
echo "[ <a href="$filename?offset=$i&go=$go">$i</a> ] \r\n";
}
}
if ($off < $off_pag) {
$next = $off + 1;
echo "[ <a href="$filename?offset=$next&go=$go">next</a> > ] \r\n";
}

echo "<br /><br />\r\n";
echo "Page $off of $off_pag<br />\r\n";

?>
[/quote]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

protokol wrote:This is another option which I found to be extremely cool. It's written in JavaScript, but does what you are looking for:

http://www.kryogenix.org/code/browser/sorttable/
Thats pretty nice, leanr something new every day!
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

NOT USING JAVA SCRIPT

Post by mohson »

Guys I want to do this no using javascript as it acts funny with different browsers and I dont want to rely on a client side javascript - someone must know a way to do this USING PHP???
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

I think u should update ur code where it retrieves info frm db
to incorporate a session var which will be the column to sort on .
get it. :P

Bye 4 now!
Post Reply