Thread: Mys 1.3.1 Group sort
View Single Post
  #53  
Old 12-16-2017, 08:59 PM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 516
Gender: Unknown/Other
Credits: 63,666
Dinocanid is on a distinguished road
Default

I managed to convert the main part of the script, which involves creating groups and sorting pets on the myadopts index.

That's the table setup for adopts_pet_groups. You need new column in owned_adoptables called "pet_group". (Not "group! "group" is a reserved name in phpMyAdmin.) Also make sure that "default as defined" is set to 0.
I did the entire thing in blank.php/blankview.php for testing purposes, so that's why it looks the way it does.
PHP Code:
<?php
class BlankView extends View{
    
            
    public function 
index(){
        
$mysidia Registry::get("mysidia");
        
$document $this->document;
        
$document->setTitle("<center>Sort Testing!</center>");
        
        
//pet group variables? I think?
        
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        
$group $_REQUEST["group"];
        }
        
//var end
        
        //convenient vars
        
$username $mysidia->user->username;
        
$uid $mysidia->user->uid;
        
//var end
        
        
        //Pet sorting!      
                
if(!$group == ""){
                    if(!
is_numeric($group)){
                            
$document->setTitle("No group");
                            
$document->add(new Comment("Group does not exist."));
                            return;
                    }
                    else{
                            
$row $mysidia->db->select("pet_groups", array(), "uid='{$uid}' and id='{$group}'")->fetchObject();    
                            if(
$row->id == $group and $row->uid == $uid){
                                
$groupquery "AND pet_group = '{$group}'";
                            }    
                            else{ 
// Invalid group
                                
$article_title "No group";
                                
$article_content "Group does not exist.";
                            }
                        }
               }
        else{
            
$groupquery "AND pet_group = '0'";
        }      
        
        
// We need to get all of the user's adoptables from the database and display them...                                                       
            
$rowsperpage 10;
            
//$pagination = new Pagination($adopts, $query, $rowsperpage, "http://".constant("DOMAIN").constant("SCRIPTPATH")."/stable.php?group={$group}");
                //$pagination->setPage($_GET[page]);
                
$pagination = new Pagination($total10"myadopts");
                
$pagination->setPage($mysidia->input->get("page"));    
                
//$stmt = $mysidia->db->query($query);
                
$stmt $mysidia->db->select("owned_adoptables", array("aid"), "owner = '{$mysidia->user->username}{$groupquery} ORDER BY totalclicks LIMIT {$pagination->getLimit()},{$rowsperpage}");
            
$groups ""// Get groups
            
$stmt2 $mysidia->db->select("pet_groups", array(), "uid='{$uid}'");
            
            
$document->add(new Comment("
                <b>Group:</b><br>
                   <form method='post' action='blank'>
                        <select name='group' id='group'>
                    <option value='0'>Ungrouped</option>"
));
            
            while(
$row $stmt2->fetchObject()) {
                  
$id $row->id;
                  
$name $row->name;
                  
$document->add(new Comment("<option value='{$id}'>{$name}</option>"));
            }
            
$document->add(new Comment("
                        </select><br>
                        <input type='submit' value='Go'>
                    </form>
                    <a href='blank/creategroup'>Create a new group</a> | <a href='blank/deletegroup'>Delete a group</a>
                    <br><br>
                    <table class='table table-bordered table-dark'>
                      <thead>
                            <tr>
                                  <th>Image</th>
                                 <th>Info</th>
                                  <th>Action</th>
                            </tr>
                      </thead>
                      <tbody>"
FALSE));
                while(
$row $stmt->fetchObject()){
                    
$adopt = new OwnedAdoptable($row->aid);
                        
$image $adopt->getImage(); 
                        
$document->add(new Comment("
                            <tr>
                              <td><img src='
{$adopt->getImage()}'></td>
                              <td><b><em>
{$adopt->getName()}</b></em><br></br> Level {$adopt->getCurrentLevel()}<br></br> {$Gender}</td>
                              <td><a href='/myadopts/manage/
{$row->aid}' class='btn btn-primary' style='width:200px; height:auto;'>Manage</a></td>
                        </tr>
                        "
FALSE));
                }
                
$document->add(new Comment("</table><br /><br />{$pagination->showPage()}"));         
        
//Sorting end   
    
}
    
    public function 
creategroup(){
    
$mysidia Registry::get("mysidia");
    
$document $this->document;
    
$username $mysidia->user->username;
    
$uid $mysidia->user->uid;    
    
//$document->setTitle("<center>Sort Testing!</center>");
                  
$groupCount $mysidia->db->select("pet_groups", array("uid"), "uid = '{$mysidia->user->uid}'")->rowCount();
                if(
$groupCount >= 5) {
                    
$document->setTitle("<center>Sorry!</center>");
                    
$document->add(new Comment("You can only have 5 groups. <br><br> <a href='myadopts'>Go back?</a>"));
                }
                else{
                 
$document->setTitle("<center>Create a new group</center>");
                 
$document->add(new Comment("
                     <form method='post' action='creategroup'>
                        <b>Group Name:</b> <br>
                        Max length is 15.<br>
                        <input type='text' name='groupname' id='groupname' maxlength='15' /><br>
                        <input type='submit' name='submit'>
                    </form>
                    <a href='/blank'>Go back</a>"
));
            }
            if (
$_SERVER['REQUEST_METHOD'] == 'POST') {
                
$groupCount $mysidia->db->select("pet_groups", array("uid"), "uid = '{$mysidia->user->uid}'")->rowCount();
                    if(
$groupCount >= 5) {
                        
$document->setTitle("<center>Sorry!</center>");
                        
$document->add(new Comment("You can only have 5 groups. <br><br> <a href='myadopts'>Go back?</a>"));
                    }
                    else{
                        
$groupname $_REQUEST["groupname"];
                    
$mysidia->db->insert("pet_groups", array("username" => $username"uid" => $uid"name" => $groupname));
                    
$group $mysidia->db->select("pet_groups", array(), "name='{$groupname}' and uid='{$uid}'")->fetchObject();    
                    
$id $group->id;
                    
$document->setTitle("Created group ''{$groupname}''");
                    
$document->add(new Comment("
                         You created a new group called '
{$groupname}'.<br><br>
                        <a href='/blank'>Go back</a>"
));
                    }
            }
    }
    
    public function 
deletegroup(){
    
$mysidia Registry::get("mysidia");
    
$document $this->document;
    
$username $mysidia->user->username;
    
$uid $mysidia->user->uid;    
    
$stmt2 $mysidia->db->select("pet_groups", array(), "uid='{$uid}'");
    
//$document->setTitle("<center>Sort Testing!</center>");
                  
$groupCount $mysidia->db->select("pet_groups", array("uid"), "uid = '{$mysidia->user->uid}'")->rowCount();
                if(
$groupCount 0) {
                    
$document->setTitle("<center>Sorry!</center>");
                    
$document->add(new Comment("You don't have any groups. <br><br> <a href='/blank'>Go back?</a>"));
                }
                else{
                 
$document->setTitle("<center>Delete a group</center>");
                 
$document->add(new Comment("
                <b>Group:</b><br>
                   <form method='post' action='deletegroup'>
                        <select name='group' id='group'>
                    <option value='0' 
{$selected}>Ungrouped</option>"));
            
                while(
$row $stmt2->fetchObject()) {
                      
$id $row->id;
                      
$name $row->name;
                      
$document->add(new Comment("<option value='{$id}'>{$name}</option>"));
                }
                
$document->add(new Comment("
                        </select><br>
                        <input type='submit' value='Go'>
                        </form>
                        <a href='/blank'>Go back</a>"
));
            }
            
            if (
$_SERVER['REQUEST_METHOD'] == 'POST') {
                        
$group $_REQUEST["group"];
                    
$group_info $mysidia->db->select("pet_groups", array(), "id='{$group}' and uid='{$uid}'")->fetchObject();    
                    
$document->setTitle("Deleted ''{$group_info->name}''");
                    
$document->add(new Comment("
                         '
{$group_info->name}' has been deleted. All pets organized under it are now ''ungrouped''.<br><br>
                        <a href='/blank'>Go back</a>"
));
                    
$mysidia->db->update("owned_adoptables", array("pet_group" => 0), "pet_group = '{$group}'");
                           
$mysidia->db->delete("pet_groups""uid='{$uid}' AND id = '{$group}'");    
            }
    }
    
}
?>
PHP Code:
<?php

class BlankController extends AppController{

    
    public function 
index(){        
    
$mysidia Registry::get("mysidia");
    
    }
    
    public function 
creategroup(){        
    
$mysidia Registry::get("mysidia");
    
    }
    
    public function 
deletegroup(){        
    
$mysidia Registry::get("mysidia");
    
    }
}
?>
This version doesn't use AJAX, but it basically works the same. It just handles the variables internally instead of passing it to the URL.
__________________
Reply With Quote