Simple PHP/MYSQL

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
76
i have a database that I outputted via php and mysql... now is there an easy way to put a link on the top of each column without using a form to have it execute SQL (ie same SQL query but order by column)?
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
just pass in a variable called 'orderBy' and the value equal to the column name. Then just order by that column when a request comes in. When you output the table also set another variable on the column that you just ordered by called 'desc' with value=1 so you know next time you're going to order by ASC if desc=1
 

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
76
$query = "SELECTblahblahblahblah";
?> <table border=1> <tr><td>Player Name</td><td>Team</td><td>Position</td><td>Option?</td></tr><?
$result = mysql_query($query) or die('Query failed: '.mysql_error());
while ($line = @mysql_fetch_array($result)) {
$fullname=$line['fulln'];
$team=$line['teamname'];
$pos=$line['position'];
$opt=$line['options'];
echo "<TR>";
echo "<TD>";
echo $fullname;
echo "</TD><TD>";
echo $team;
echo "</TD><TD>";
echo $pos;
echo "</TD><TD>";
echo $opt;
echo "</TD></TR>";
//$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
echo "</TABLE>";

what link can i put on the titles of the columns to make them sortable? i thought of a few ways but theyd be complicated... is there an easy way to do that?
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Pretty much what fs5 said.

Add a variable called 'orderBy' to your SQL statement when you do the query. The value will be the name of the column you wish to order by.

For example, make the Player Name text in that column a link to http://www.yourdomain.com/pagename.php?orderBy=Player%20Name.

orderBy should be a GET variable, and when you do the SQL query it would be something like:
$orderBy = $_GET['orderBy'];
$query = "SELECT * FROM table ORDER BY $orderBy";

Then when you click that link in the Player Name column, it will refresh the page, send it Player Name as the sort option, and display your table sorted by Player Name.
 

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
76
right but how do i set the variable when someone clicks on say, name or team?
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
database.php?orderBy=name&direction=asc

In your code, you would grab those values by doing this:

$orderBy = $_GET["orderBy"];
$direction = $_GET["direction"];
 

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
76
hmm i tried:

if(isset($_POST['orderBy'])){
$orderby=$_POST['orderBy'];
$addon="ORDER BY $orderby";
}
$query = "SELECTblahblahblahblah".$addon;
?> <table border=1> <tr><td><a href="index.php?orderBy=player.lastn">Player Name</a></td><td>Team</td><td>Position</td><td>Option?</td></tr><?
$result = mysql_query($query) or die('Query failed: '.mysql_error());
while ($line = @mysql_fetch_array($result)) {
$fullname=$line['fulln'];
$team=$line['teamname'];
$pos=$line['position'];
$opt=$line['options'];
echo "<TR>";
echo "<TD>";
echo $fullname;
echo "</TD><TD>";
echo $team;
echo "</TD><TD>";
echo $pos;
echo "</TD><TD>";
echo $opt;
echo "</TD></TR>";
//$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
echo "</TABLE>";

i tried clicking on it and echoing $addon is empty... what am i dont wrong
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Btw, just a small addition to the code above.
Make sure you do error checking on what value was passed as the value of orderBy
This is to avoid the "Bad Guys" from injecting silly codes to your SQL query.

$allowedList = array ('fulln', 'teamname', 'position'); # Basically the ordering that you want to allow

if (isset ($_GET['orderBy']))
{
...$orderBy = $_GET['orderBy'];

...if (! in_array ($orderBy, $allowedList))
...{
......die ('Bad request - or put something more fancy here');
...}
}

# connect to your database here and do the rest of stuffs as above
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |