Unstoppable Domains โ€” Get your daily AI drops report

PHP and how to sort a HTML header by clicking on it

SpaceshipSpaceship
Watch
Impact
0
Hi folks,

I'm really new on coding PHP and spend a lot of time scanning various PHP forums to find a solution for the followings problem.

I've a small MySQL table called USER. Only 4 fields ar in this table:
1. id -> smallint(6)
2. lastname -> varchar(30)
3. firstname -> varchar(30)
4. userid -> varchar(30)

I fully managed to create a HTML page and I fully managed to select the content of the MySQL table.

The "key" now is that I would like to click on the HEADER of the HTML table in order to SORT the CONTENT of the table.

But before I post my available PHP-code, I have a first question: this forum has EXACTLY WHAT I AM LOOKING for, I can click an any HEADER entry and the table gets sorted by this column.

Can I get this piece of PHP-code to study how it is solved? It would simply be great after all these terrible long nights of scanning forums ... :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Hi there,

I'm not exactly sure what you're wanting to do, but I think I have an idea. Are you talking about where you see:

Web Design Talk: Web Design Forums > Web Design Forums > Programming > PHP and how to sort a HTML header by

Is this what you're talking about ?
 
0
•••
ok i kinda didnt get what u want

but

this might be healpfull

$sql = "SELECT * FROM `users` ORDER BY `id` DESC";

will order them like

4
3
2
1

or

$sql = "SELECT * FROM `users` ORDER BY `id` ASC";

will sort them in to

1
2
3
4
 
0
•••
Hi folks,

this is correct, I exactly want to implement what I can find here in this forum by going to
-> Web Design Forums
-> Web Design Forums
-> Programming.

Being on that page, I do have the entire content of the forum in front of me and I can click on any entry of the TABLE HEADER and the entire table gets sorted by this HEADER entry (e.g. let's say you click on REPLIES or VIEWS or RATING, the table gets sorted by THIS ENTRY and it gets sorted ASC or DESC.

Since I usually don't use a forum this might look a bit confusing to you .... but I'm sure I'll make it soon :).

By the way (Adam_UK), I made it already to select the apropriate fields from the table (i.e. using the SELECT you are proposing). This all works fine. But think about the situation where you want to sort the HTML-table by a different column and also "flipping" the sort order from ASC to DESC and vice versa, depending on what's already selected.

And - you guys are fast in answering - amazing.
 
0
•••
You can do this using the query string. You'll probably need a few if statements or a switch statement and possibly a while loop or two depending on how you choose to do it and such.

You could so something like:

if ($action == 'desc') {
$sql = "SELECT * FROM `users` ORDER BY `id` DESC";
} elseif ($action == 'asc') {
$sql = "SELECT * FROM `users` ORDER BY `id` ASC";
} else {
Some other sql statement or code
}

Then you can print out your links like

<a href="/page.php?action=asc">Sort by ascending</a>

That's just an example and would take a bit more than that I'm sure, but should give you an idea.

There's many possibilities, you could use a switch statement or while loop or both etc. You can take a look at some examples here on using the query string. http://www.webdesigntalk.net/showthread/t-495.html
 
0
•••
Originally posted by deadserious
You can do this using the query string. You'll probably need a few if statements or a switch statement and possibly a while loop or two depending on how you choose to do it and such.

You could so something like:

if ($action == 'desc') {
$sql = "SELECT * FROM `users` ORDER BY `id` DESC";
} elseif ($action == 'asc') {
$sql = "SELECT * FROM `users` ORDER BY `id` ASC";
} else {
Some other sql statement or code
}

Then you can print out your links like

<a href="/page.php?action=asc">Sort by ascending</a>

That's just an example and would take a bit more than that I'm sure, but should give you an idea.

There's many possibilities, you could use a switch statement or while loop or both etc. You can take a look at some examples here on using the query string. http://www.webdesigntalk.net/showthread/t-495.html

on think i will add to that code if u want at the top of it (if thats the correct code add this

$action = $_GET['action'];


:)
 
0
•••
Hi deadserious and adam_uk,

thanks for your input. I'll check it out today and respond to your input.

As it looks to me there is no chance for me to "steal" the PHP-code that is used in this forum and which allows the table-header sorting (well, that's life).

Talk to you soon ....
 
0
•••
Here is the content of the PHP-file I talk abou. I paste the code below, but I seem to fail to do this in the proper or required way .... sorry for this, it doesn't look really fency.

PHP:
<?php

// THIS FILE IS FOR DEMO PURPOSES ONLY. I'VE REMOVED ALL CSS-STUFF AND ANY
// FORMATTING TO REDUCE THE TEST.PHP TO IT'S MINIMUM CONTENT.

// The table "USER" which I use in the MySQL-DB "TEST" does have these fields:
// id -> smallint(6) <--- AUTO INCREMENT
// lastname -> varchar(30)
// firstname -> varchar(30)
// userid -> varchar(30)


$DB_HOST = "localhost";
$DB_USER = "root";
$DB_PASS = "";
$DB_NAME = "test";
$DB_USER_TABLE = "user";

$DB_CONNECT_ERROR = "<b>Can't connect DB.</b>";
$DB_SELECT_ERROR = "<b>DB not found.</b>";

MYSQL_CONNECT("$DB_HOST","$DB_USER","$DB_PASS")or die($DB_CONNECT_ERROR);
MYSQL_SELECT_DB($DB_NAME)or die($DB_SELECT_ERROR);

// === Here I usually insert the HEADER ------------------------------------ ->
// include("includes/incl_header.php");


// How to sort, ASC or DESC? -------------------------------------------------->
// --> INTERESTING IS THAT THIS WORKS FINE, I CAN SORT ASC OR DESC DEPENING ON
// --> WHAT WAS USED RIGHT BEFORE I CLICK AGAIN ON THE HEADER TO SORT AGAIN.
$SortByOrder = $_REQUEST['SortByOrder'];
if ($SortByOrder == "desc")
{
$SortByOrder = "asc";
}
else
$SortByOrder = "desc";


// Which TABLE FIELD do I use to SORT? ---------------------------------------->
// --> HERE I DO HAVE A REAL PROBLEM. I DON'T KNOW HOW TO FIGURE OUT HOW TO
// --> MANAGE TO CHECK OUT WHICH FIELDS IS USED (either User Id, Last Name,
// --> First Name or ID. I JUST LEAVE THIS CODE BECAUSE IT IS THE LAST ONE
// --> I USED AND WHERE IS STUCK NOW.)
$SortByField = $_REQUEST['SortByField'];
if ($SortByField == "User ID")
{
$SortByField = "userid";
}
elseif ($SortByField == "Last Name")
{
$SortByField = "lastname";
}
elseif ($SortByField == "First Name")
{
$SortByField = "firstname";
}
elseif ($SortByField == "ID")
{
$SortByField = "id";
}
else $SortByField = "id";


$SELECT_ALL_RECORDS=MYSQL_QUERY("SELECT * FROM $DB_USER_TABLE ORDER by $SortByField $SortByOrder");


// ========================================================================= -->
// START THE CONTENT OF THE SITE. ------------------------------------------ -->
echo "<table>n";
echo "<tbody>n";

// Make the table header "sortable". Just click on any header in the table and
// the table gets sorted again.
// I made it to pass the "SORT-ORDER" (ASC or DESC) along, but I fail to pass along
// the "SORTBY" information.
// QUESTION: HOW would a WORKING LINK look like? I STUCK HERE ................
echo "
<tr>
<th><a href='test.php?SortByOrder=".$SortByOrder."?SortByField=".$SortByField."'>ID</a></th>
<th><a href='test.php?SortByOrder=".$SortByOrder."?SortByField=".$SortByField."'>Last Name</a></th>
<th><a href='test.php?SortByOrder=".$SortByOrder."?SortByField=".$SortByField."'>First Name</a></th>
<th><a href='test.php?SortByOrder=".$SortByOrder."?SortByField=".$SortByField."'>User ID</a></th>
<th>Edit</th>
</tr>n";

while($DISPLAY_RECORD=MYSQL_FETCH_ROW($SELECT_ALL_RECORDS))
{
echo "
<tr>
<td>$DISPLAY_RECORD[0]</td>
<td>$DISPLAY_RECORD[1]</td>
<td>$DISPLAY_RECORD[2]</td>
<td>$DISPLAY_RECORD[3]</td>
</tr>n";
}

echo "</tbody>n";
echo "</table>n";
echo "<p>n";


// --- Here usually I insert the FOOTER ------------------------------------ -->
// include("includes/incl_footer.php");
?>
 
0
•••
Problem is solved ...

Hi all,

I found a solution for my problem. Thanks fo any help I received from you guys.....
 
0
•••
CatchedCatched
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back