Thread: Explore Help?
View Single Post
  #2  
Old 04-09-2015, 05:31 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 836
Gender: Female
Credits: 33,136
Kyttias is on a distinguished road
Send a message via AIM to Kyttias
Default

Ok, I'll begin with more of a general how-to guide to making your own pages before moving on to your specific questions at the end. It's good to have all this sort of in one place -- it'll help you do other stuff in the future, yeah?

Pages can be found in the root directory - or, well, their hard-coded side is there, anywhere. The view side is found in the view folder. It is necessary, to make a page, to make a file in each, following the same naming conventions as the others. I'd observe donate and donateview as an example, since they're pretty simple files. Usually I've been keeping my own 'page' files nearly empty, save for what needs to be there for it to operate, and only putting code into their 'pageview' file counterparts. Both exist in cases where you'd need to handle things like form submission.

Basics
As an example, for myself, I'll be creating a new page for pet missions. It will be accessible at "mysite.com/missions":

/missions.php
PHP Code:
<?php
class MissionsController extends AppController{

    public function 
__construct(){
        
parent::__construct("member");
    }
    
    public function 
index(){
        
$mysidia Registry::get("mysidia");
    }
    
}
?>
/view/missionsview.php
PHP Code:
<?php
class MissionsView extends View{

    public function 
index(){
        
$mysidia Registry::get("mysidia");
        
$document $this->document;        
        
$document->setTitle("Mission List");    
        
$document->add(new Comment("This page will contain missions you can send your pets on."FALSE));
    }
    
}
?>
PHP is case-sensitive language. This means "Catfish" is different than "catfish" and most definitely different than "cAtFiSh". Name variables and documents the same everywhere - best just to use all lowercase all the time if you're a novice so you can't go wrong.

Pretty basic thus far. You can see that a document in view needs its title set. Then you can see I'm adding text to the page by adding a new Comment to the $document. The comment is in double quotes, and can contain HTML. Any HTML inside it that may need double quotes needs them replaced instead with single quotes. When you absolutely need to use double quotes, escape them by placing a backslash before them:

PHP Code:
$document->add(new Comment("<div class='example'>This div a class of \".example\" associated with it.</div>"FALSE)); 
The real syntax here is just:

PHP Code:
$document->add(new Comment("")); 
And by default, all new Comments will have a linebreak after them unless you add a comma and the parameter FALSE. See above? It is my personal preference to always use the FALSE parameter because I want control of when there are linebreaks. If I want to write solid blocks of HTML, well, darn right I'm going to.

Preventing Users Not Logged In
In missions.php, this is one of the functions I included:
PHP Code:
public function __construct(){
    
parent::__construct("member");

This function will prevent anyone not logged in from accessing the page. It will display only a generic error. If you want specific information to display, then remove the above function, and over in missionsview.php, add this (for clarity's sake, I recommend right after $document = $this->document;), it's an if statement reading "if the mysidia user is NOT logged in", the NOT bit coming from the !:

PHP Code:
if (!$mysidia->user->isloggedin) {
    
$document->setTitle("Feature: Mission List");    
    
$document->add(new Comment("Logged in users can send their pets on missions!"FALSE));

User Variables
Alright, let's talk about what sort of stuff you have access to. Well, anything, actually, so long as you know how! This framework is 'object oriented'. Object oriented PHP differs from standard, procedural PHP only in that it adds some new features. To a beginner, it's a lot of overwhelming, confusing new syntax. I'm new at this myself, but I'll try my best to explain.

Let's start with this magical, important line (you'll see I've already included it in the files above):
PHP Code:
$mysidia Registry::get("mysidia"); 
We need this so we can have the variable '$mysidia' contain a dynamic, yet static, link to the parent class called "mysidia", so we can invoke methods and properties from it.You only need to define $mysidia once inside of a function, so best to do it very early on.

Let's keep our view/missionsview.php file open (or whatever you chose to name yours), but also go open up our database. (On your hosting look for phpMyAdmin, it'll probably prompt you to log in.) Our first stop will be the table adopts_users.

For ease of access, all of the columns in this particular table are accessible with the keyword 'user'. The framework already knows which user is logged in, and will pull data from the current user in the form of a variable. If you want to insert a variable into your page, you can wrap it in curly braces:

PHP Code:
$document->add(new Comment("
    User ID#: 
{$mysidia->user->uid} <br/>
    Username: 
{$mysidia->user->username} <br/>
    Birthday: 
{$mysidia->user->birthday} <br/>
    Member Since: 
{$mysidia->user->membersince} <br/>
"
FALSE)); 
Accessing The Database
You'll notice not all of a user's data was stored in adopts_users. There's some more stored in adopts_users_profile. But how to get at it? Well, we can fetch the whole row relating to a current user like this:

PHP Code:
$row $mysidia->db->select("users_profile", array(), "uid ='{$mysidia->user->uid}'")->fetchObject();
foreach(
$row as $key => $val){
    
$this->$key $val;
}
$document->add(new Comment("
    Gender: 
{$this->gender} <br/>
    Nickname: 
{$this->nickname} <br/>
"
FALSE)); 
We're storing the row as an object attached to the variable $row. We then need to iterate through each of the columns in that row, so we're attaching them to key value pairs and making them global to this file. Meaning, accessible with the keyword 'this'.

What if we wanted to do something crazier, like count the total number of pets this user owns? Or find out what pet type they own the most of, and just how many of that pet they own?

PHP Code:
$totalpets $mysidia->db->select("owned_adoptables", array(), "owner = '{$mysidia->user->username}'")->rowCount();    
$most_owned $mysidia->db->query("SELECT type, COUNT(type) AS favpet FROM adopts_owned_adoptables WHERE owner = '{$mysidia->user->username}' GROUP BY type ORDER BY COUNT(*) DESC LIMIT 1")->fetchObject();
$document->add(new Comment("            
    Total Pets Owned: 
{$totalpets} <br/>
    Most Owned Pet: 
{$most_owned->type} ({$most_owned->favpet})<br/>
"
FALSE)); 
I'd like to bring special attention to $most_owned above, because the query string it uses is a normal, raw SQL string. If all else fails after you've read up on SQL queries, try using $mysidia->db->query() like above. Usually select() will do for pulling information, but if you need complicated selectors like that last example, you still can do it in a semi-standard fashion!

For more information on database-related stuff, I'm going to direct you to this other post I made, and some more examples of queries I made to add to my stats page.

Classes
In object oriented programming, classes exist. Classes hold functions as 'methods' that can be reused again and again, to prevent writing redundant code. There's actually some pre-constructed functions you can use with $mysidia->user as before - thanks to the Member class! (Jeez, why didn't you say so, Kyt?)

It's not immediately obvious - if you open up classes/class_user.php, you don't actually see much going on. But go open up classes/class_member.php - whaaaat, class Member extends User? Let's test one out

PHP Code:
$document->add(new Comment("
    Total Pets Owned: 
{$mysidia->user->getalladopts()}
"
FALSE)); 
(But we already did that! You're telling me we didn't even have to?!) Nope, you didn't. Notice that it's getalladopts(). It's a function, not a variable, so it needs those parentheses.

Ok, but what if we're not dealing with an user, but a pet? Well, we can instantiate a pet by it's id number, if we know it, as being a 'new OwnedAdoptable' - and then we'll have access to all the properties and functions available from classes/class_ownedadoptable.php. The OwnedAdoptable class takes the pet's adoptable id, it's aid, as a parameter.

So, let's pick a random pet owned by the user:
PHP Code:
$random_pet $mysidia->db->select("owned_adoptables", array(), "owner='{$mysidia->user->username}' ORDER BY RAND() LIMIT 1")->fetchObject();
$chosen_pet = new OwnedAdoptable($random_pet->aid);
$document->add(new Comment("            
    Pet Name: 
{$chosen_pet->getName()} <br/>
    Pet Level: 
{$chosen_pet->getCurrentLevel()}<br/>
"
FALSE)); 
What Now?
You want to build an explore page, right? Well, now you know how to insert HTML into a page. I assume you've read up on plenty of tutorials on imagemaps, so you have a pretty good idea of what you're doing, so I'll jump right to your specific questions.
  • Give a user some currency:
    PHP Code:
    $amount 1000;
    $mysidia->user->changecash($amount); 
  • Give a user a pet:
    PHP Code:
    $species "Dezh";
    $newadopt = new StockAdopt($species);
    $newadopt->append($mysidia->user->username); 
  • Give a user an item:
    PHP Code:
    $item "Beta Cupcake";
    $qty 1;
    $newitem = new StockItem($item);
    $newitem->append($qty$mysidia->user->username); 
  • Retrieve New Database Properties:
    The good news is, the Member class automatically iterates all the columns inside adopts_users, so since your plan was to add columns there, you can literally access your new properties as variables with $mysidia->user->lastday and $mysidia->user->exploretimes - neat, huh?
  • Check Time Difference:
    PHP Code:
    if (strtotime($mysidia->user->lastday) < strtotime("-1 day")) {
        
    $document->add(new Comment("You explored last over a day ago."));

  • Update The Database:
    PHP Code:
    $now = new DateTime();
    $today $now->format('Y-m-d');
    $mysidia->db->update("users", array("lastday" => $today), "username = '{$mysidia->user->username}'");
    $mysidia->db->update("users", array("exploretimes" => ($mysidia->user->exploretimes 1)), "username = '{$mysidia->user->username}'"); 
  • Premium Only Content:
    In your database, go ahead and change the structure of adopts_users_status to add a column to contain a VARCHAR 3 with Default 'As defined:' No. Then you can perform this check with ease:
    PHP Code:
    if($mysidia->user->status->premium != "yes"){
        
    $document->add(new Comment("No, you're not a premium member!"));
    } else {
        
    $document->add(new Comment("Welcome to this premium area!"));

  • Checking if user has an item:
    PHP Code:
    $item "Beta Cupcake";
    $hasitem $mysidia->db->select("inventory", array("quantity"), "itemname ='{$item}' and owner='{$mysidia->user->username}'")->fetchColumn();
    if(
    $hasitem){
        
    $document->add(new Comment("You have a {$item}!"));
    } else {
        
    $document->add(new Comment("Sorry, you do not have a {$item}!"));

  • Check the season:
    PHP Code:
    $currentMonth date("m");
    if (
    $currentMonth >= "03" && $currentMonth <= "05"){$season "spring";}
    elseif (
    $currentMonth >= "06" && $currentMonth <= "08"){$season "summer";}
    elseif (
    $currentMonth >= "09" && $currentMonth <= "11"){$season "fall";}
    else {
    $season "winter";}
    $document->add(new Comment("It is {$season}!")); 

Whew, that was fun. Let me know if there's anything else!

Last edited by Kyttias; 02-17-2016 at 02:06 PM.
Reply With Quote