Ascending Order and Descending Order?
Hi
I have a question regarding Ascending Order and Descending Order.
I want to learn how to write a Ascending Order and Descending Order on any list.
For example:
I want to create a booklist and I want to organized it by a library code system.
Category----ID-----Arthur
History-------123----Scholastic
Art------------456----Da Vinci
Math---------789----Newton
This is something I recently learn from another thread I created regarding about if & else statement.
Here is a code:
1) category.php
2) list.php
The 'category' is from the database and the script is called "category.php"
This script is called "list.php" which I'm trying to learn and understand how to pull the info.
( - ) : I'm not to sure how to write it or I don't know what to put in it!
<?php
$orderBy = 'Arthur';
$orderBy = 'Category ';
$orderBy = 'ID';
$orderSort = 'ASC';
$orderSort = 'DESC';
if (-){
$orderBy = (-);
$orderSort = (-) == 'ASC' ? 'ASC' : 'DESC';
}
?>
HTML:
<?php
if ( (-) =='Arthur' and (-) == 'ASC'):
<a href="category.php?ord=Arthur&se=DESC"><span class="style1">Arthur</span></a>
else
<a href="category.php?ord=Arthur&se=ASC"><span class="style1">Arthur</span></a>
endif;
?>
I really apprecaited if someone explain to me how to write it correctly.
Related Article: Email order problem
is a solved PHP discussion thread by didi00 that has 10 replies and was last updated 2 years ago.
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
Hi fobos,
Thank for the reply but I don't quite understand what you wrote? I don't want to be rude.
You have to specfic:
> if(!isset('something')) {
> mysql code ORDER BY ASC
> }
This
www.w3schools.com
good resource to learn the basic.
The issue I have is a bit more complicate. I have 2 different files. I wish it was 1 file that I can put everything in it but not in this case.
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
@ phorce
Thanks for the reply. I'll look over and see how it compile.
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
@ phorce
I already have database (it's on the server) I don't think I need to create another list?
$narray[0]="Alfred";
$narray[1]="Robert";
$narray[2]="Deepak";
$narray[3]="Teresa";
$narray[4]="Joshua";
$narray[5]="Chandni";
$narray[6]="Sadiq";
$narray[7]="Vladimir";
The script that you wrote doesn't really pull the data from the base.
<?php
if(!isset($_GET['se'])) die('Please enter the type of sort');
$type = $_GET['se'];
//$type = 'ASC';
// Or use a switch statement
if($type=='DESC'){
rsort($narray);
}else if($type=='ASC'){
sort($narray);
}for($i=0; $i<8; $i++){
print $narray[$i] . " ";}
?>
I really appreciated help.
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
Is Arthur the Author ?:)
//THESE ARE THE POSSIBLE VALUES OF YOUR GET VARIABLES TO CHECK AGAINST
$ordsby_allowed = array('Author','Category','ID'); //put these in order of importance
$ordse_allowed = array('DESC','ASC'); //put these in order of importance
//CHECK THAT THERE IS A URL QUERYSTRING
if(!empty($_GET)){
//CLEAN ALL QUERYSTRING VALUES
$get = array_map('mysql_real_escape_string',$_GET);
//CHECK FOR EXISTENCE OF VALID ord AND se - ACCEPT IF EXIST OR APPLY DEFAULT VALUE (1st VALUE IN ALLOWED ARRAY) IF NOT
$ord = (isset($get['ord']) && in_array($get['ord'],$ordsby_allowed)) ? $get['ord'] : $ordsby_allowed[0];
$se = (isset($get['se']) && in_array($get['se'],$ordse_allowed)) ? $get['se'] : $ordse_allowed[0];
}else{
//NO QUERYSTRING VALUES THEREFORE APPLY DEFAULTS
$ord = $ordsby_allowed[0];
$se = $ordse_allowed[0];
}
//RUN QUERY
$result = mysql_query("SELECT * FROM `booklist` ORDER BY `$ord` $se");
//...(carry on)...
I should point out that in_array() makes a case-sensitive comparison, so 'desc' and 'id' will NOT match. This is a trivial matter to overcome - you can apply the array_map to a user function and apply both mysql_real_escape_string and strtolower and then make all data in the allowed arrays lowercase. MYSQL statements are not case-sensitive.
diafol
Keep Smiling
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61
If I understand correctly: you want to construct an SQL statement depending on a field to be sorted on and the sort direction (ASC, DESC)?
If the above is true then the simplest option is to have the field to be sorted on and the sort direction in a querystring (using a form with method="get" - I hope you know how to do this).
Then just check whether both values have been passed and construct a query:
// if field to sort and sort order have been set
if(isset($_GET['sortfield']) and isset($_GET['sortdir']) {
// assign querystring values to variables
// all the security stuff is omitted here for simplicity
// but do not to forget to check for correct values yourself!!!
$sortfield = $_GET['sortfield'];
$sortdir = $_GET['sortdir']
} else {
// prepare default values
$sortfield = 'Category';
$sortdir = 'ASC';
}
$query = "SELECT Category, ID, Arthur FROM booklist ORDER BY $sortfield $sortdir"
// do connecting to the db, reading from the table, displaying results etc
// ...
broj1
Nearly a Posting Virtuoso
1,215 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
I havent noticed ardav has already replied with more detailed example some minutes before me so do not pay too much attention to my reply :-)
broj1
Nearly a Posting Virtuoso
1,215 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
@ broj1
Thanks for the reply! I'll look over it anyway and see how it works. Thanks!
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
@ ardav
I never knew your were Dudley Moore fan? You know if you have some hair on your profile you defintely look like him!
Well, all joking aside ...
Thanks for the reply! I really appreciated that you took your time again write and post another script!
I will take a look at it and configure it to the script I have.
You mention:
I should point out that in_array() makes a case-sensitive comparison, so 'desc' and 'id' will NOT match.
I will take note of that if that comes up! Thanks!
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
You know if you have some hair on your profile
That's my grandad. I'm 17 and still in school. :)
diafol
Keep Smiling
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61
@ broj1
You're right about the "Ardav" example! It does have more detailed than the one you have and you have the same if & else statement as "Ardav"! I appreciated that you took time writing! Thanks!
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
A further impovement would be to use Ajax for sorting the data but only if you have significant amount of other content on the page. When changing sort order or field to sort by, using Ajax, only the sorted data would be refreshed not the whole page. If you have only the booklist data on the page it is not worth bothering with Ajax.
If you are familiar with jQuery, I guess Ajax is quite simple to implement. See http://api.jquery.com/jQuery.ajax/.
broj1
Nearly a Posting Virtuoso
1,215 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
@ broj1
I used Ajax a couple for times. I'm not a big fan of it. The script I used was a image script that is in Ajax. Unfortunately, it doesn't work in IE and Firefox (the image script that is in Ajax). I never used other scripts in Ajax. But thanks for the suggestion.
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
@ ardav
That's my grandad. I'm 17 and still in school. :)
You and "Veedeoo" are PenPals!
Actually you look like "Jason Statham", it won't shock me if a bunch of ladies come up to you and ask if you are "Jason Statham! Most likely you say "YES" and you are a dog~!
But all joking a side,
This is script is bit challenging. I try to compile it, it comes up nothing. I'll still work on it a bit and get back to you or maybe just post the errors I'm getting. But thanks.
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
I apologies for taking so long to reply back.
This is the error I'm keep getting:
"Fatal error: Can't use function return value in write context in ..."
I try to dump it (I learn this from "Biiim" a few threads ago)
<?php
$expected_array_got_string = 'somestring';
var_dump(isset($expected_array_got_string['some_key']));
var_dump(isset($expected_array_got_string[0]));
?>
This is what I got:
bool(false)
bool(true)
bool(true)
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
I try to use this to test out the isset
<?php
function isset_or(&$check, $alternate = NULL)
{
return (isset($check)) ? (empty($check) ? $alternate : $check) : $alternate;
}
?>
isset() returns TRUE if a value is NULL? That does sound right to anyone?
I should point out that in_array() makes a case-sensitive comparison, so 'desc' and 'id' will NOT match.
I kept in mind what "Ardav" mention a few post ago that it might be case sensitive?
LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47
isset() function returns either true or false. If the checked variable is NULL or is not set , isset() returns false. That is what the PHP manual claims.
Maybe you should post the last version of your code. It would be easier to try to find the bug.
broj1
Nearly a Posting Virtuoso
1,215 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
isset is fine a lot of the time, but $_GET and $_POST are always set - not the individual array items, but the superglobals themselves. so testing for isset($_POST) should always give a true.
Try this out in a blank page to see what I mean:
session_start();
echo "GET:<br />";
var_dump($_GET);
echo "<br />POST:<br /> ";
var_dump($_POST);
echo "<br />SESSION:<br />";
var_dump($_SESSION);
echo "<br />COOKIE:<br />";
var_dump($_COOKIE);
So testing for empty() may be safer. Of course an isset() will still work with a specified superglobal item. AS broj1 states - there are slight differences between isset and empty - it depends on the test conditions that you apply, e.g. what do you expect for 'false',0,NULL etc...
AFAIK and you'll need to check the manual:
isset() will return true for everything except for: var not set (obviously) and var = NULL
empty() will return true for when var = 0, var = NULL, var = false, var not set
diafol
Keep Smiling
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61