Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Questions and Supports (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=18)
-   -   Sort "myadopts" page (http://www.mysidiaadoptables.com/forum/showthread.php?t=2199)

SilverDragonTears 06-17-2011 02:33 PM

I'm selfish and don't like to share ;)

parayna 06-11-2017 05:50 PM

I know this is an old post (like a REALLY old one...) but how would I let users sort their pets? Through a preset list. I've been attempting to work it out but I don't have a good enough grasp of PHP... I can get the dropdown list with the button to show up but I don't know how to make it actually send the info to sort the pets that way..

I'd like them to be able to sort their pets by clicks, level, gender, name, class, and birthday (date adopted, which I have stored in the database as birthday in the format 'June 11th, 2017').

Any help would be much, MUCH, appreciated.. XD Once I see the code I'll know how to add more to it, probably, it's just having no idea where to start...

Dinocanid 06-11-2017 06:06 PM

I've haven't tried it, but I'm assuming this can be done by storing the user's choice somewhere and changing the sort order based on that. So in myadopts.php, there's this line:
PHP Code:

$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY totalclicks LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}"); 

Maybe make a column in phpMyAdmin called sort and put the stmt line in an if statement; sort of like this:
PHP Code:

$sort $mysidia->db->select("users", array("sort"), "username = '{$mysidia->user->username}'");

if(
$sort 'clicks'){
    
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY totalclicks LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
}
elseif(
$sort 'level'){
    
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY level LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
}
//etc... 

(This is just psuedo-code, so I doubt it would work as-is)

parayna 06-11-2017 06:28 PM

Thanks! I'll try it out tomorrow because I'm probably too tired to make sense of anything XD

parayna 06-12-2017 01:59 PM

Hmm it doesn't like the elseif parts o.O It basically ignores them and uses the last 'if' statement regardless of what $sort is set as. I also tried making them all 'if' statements just in case but then it just uses the last one as the default, regardless of what is input in the database... I also tried using them in the view file (just as a test XD) and it obviously didn't work lol XD

PHP Code:

    public function index(){
        
$mysidia Registry::get("mysidia");
        
$total $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}'")->rowCount();
        
$pagination = new Pagination($total9"myadopts");
        
$pagination->setPage($mysidia->input->get("page"));    
                
$sort $mysidia->db->select("users", array("sort"), "username = '{$mysidia->user->username}'");
        if(
$sort 'clicks'){
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY totalclicks LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        }
        
        elseif(
$sort 'gender'){
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY gender LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        }
        elseif(
$sort 'level'){
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY currentlevel LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        }
        elseif(
$sort 'name'){
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY name LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        }

        
$this->setField("pagination"$pagination);
        
$this->setField("stmt", new DatabaseStatement($stmt));
    } 


Dinocanid 06-12-2017 03:09 PM

I decided to play around with it and I got it working! The refresh is a bit strange, but it works well enough. Otherwise users would have to click on the page link again to see the changes.
For the column in phpMyAdmin, you're going to have to set the default 'as defined' to one of the choices or else you'll get an error.

PHP Code:

    public function index(){
        
$mysidia Registry::get("mysidia");
        
$total $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}'")->rowCount();
        
$pagination = new Pagination($total10"myadopts");
        
$pagination->setPage($mysidia->input->get("page"));    
        
        
//Attempting to sort!
        
$sort $mysidia->db->select("users_options", array("petsort"), "username = '{$mysidia->user->username}'")->fetchColumn();
            switch (
$sort){
    case 
'clicks'
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY totalclicks LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        break;
    case 
'gender'
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY gender LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}"); 
        break;
    case 
'level'
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY currentlevel LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        break;
    case 
'name'
        
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}' ORDER BY name LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        break;
}
        
        
//Sorting ends here!
        
$this->setField("pagination"$pagination);
        
$this->setField("stmt", new DatabaseStatement($stmt));
    } 

(myadopts.php)
PHP Code:

//This is for sorting!
         
if($mysidia->input->post("sortpets")){
$choice $mysidia->input->post("sortlist");
$mysidia->db->update("users_options", array("petsort" => $choice), "username = '{$mysidia->user->username}'");
$document->add(new Comment("<meta http-equiv='refresh' content='0;url=http://YOURSITE.com/myadopts' />")); 
}
         
        
$sortForm = new Form("sortform""""post");
        
$sort_list = new DropdownList("sortlist");
        
$sort_list->add(new Option("Clicks""clicks"));
        
$sort_list->add(new Option("Gender""gender"));
        
$sort_list->add(new Option("Level""level"));
        
$sort_list->add(new Option("Name""name"));
        
$sortForm->add(new Comment("<b>Sort by:</b>"FALSE));
        
$sortForm->add($sort_list);
        
$sortForm->add(new Button("Sort Pets""sortpets""submit"));
        
$document->add($sortForm);
        
        
//Sorting ends here!!! 

(myadoptsview.php)

I might add this to the tutorials (or mods) section since it seems to be in high demand.

parayna 06-12-2017 03:34 PM

Awesome! Thanks so much! And people would love it as a mod XD

Also, for the redirect, couldn't you use this instead?

PHP Code:

header("Location: $root/myadopts");
 
exit; 

That way the page doesn't stutter nor ask about a refresh, as my browser popped up saying about the page having to resend information and I had to click 'refresh' for it to go through. It might not be that clean but it works so far XD

And how would I get the button on the same line as the drop down box? It's for purely aesthetic reasons but I have the text as 'Sort by clicks', 'Sort by [whatever]', etc and want the button next to it. Thanks for your help :3

Dinocanid 06-12-2017 03:52 PM

I tried to do it myself, but couldn't figure it out. You can probably do it manually through css with a div element and positioning it next to the dropdown box, but that could lead to issues depending on the user's window size.

EDIT: I can't seem to get the refresh to work that way on my end, I just get an error:
Quote:

Warning: Cannot modify header information - headers already sent by (output started at /home/adopttes/public_html/myadopts.php:7) in /home/adopttes/public_html/view/myadoptsview.php on line 71

parayna 06-12-2017 04:17 PM

Okay thanks anyway! ^_^ I'm going to give it a shot!

And maybe try

PHP Code:

header("Location: http://adopttest.mysidiahost.com/myadopts");
 
exit; 

Probably a long shot but I'm thinking maybe it doesn't like the root bit. I'm by no means an experienced coder >.>As you can probably tell by my constant queries XD

All it would mean is you'd have to manually change the link if you change domains :ooo:

Dinocanid 06-12-2017 04:30 PM

Still won't work. I'll just stick with what I had xD


All times are GMT -5. The time now is 07:54 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.