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)
-   -   Seeking basic run down of php and database (http://www.mysidiaadoptables.com/forum/showthread.php?t=4757)

MikiHeart 01-13-2015 03:40 AM

Seeking basic run down of php and database
 
I'm trying to my hardest to change my way of coding to suit the site, but the one thing I can't seem to get a grasp of, is how all the database queries are handled.

Can someone please give me a basic run down, since we don't have any documentation(As far as I know).

$mysidia->db->select("table", array(), "thing = '$thing'")->fetchColumn();

I can assume that this part "$mysidia->db->select" is used to select the database information and so on. Instead of like "mysqli_query()", which is what I'm use to.

I don't understand why the array is there, and the "->fetchColumn()" part. I also don't understand why there is like "$result" variable after the MySql codes.

So I just need a basic run down, a little bit of help to understand, then I can start coding and working on features.

(I'm working on a few things. Like displaying a user's favourite pet in the sidebar)

Please and thank yous D=!!!

MikiHeart 01-13-2015 06:15 AM

Okay. So I think I've got it. After fiddling and being impatient for a reply.

But I would like a second pair of eyes to look at my code. (Instead of adding the php in the module area, I prefer to add it to the class_sidebar.php file. It's easier for coding and checking backwards and forwards.

The main code:

Code:

protected function setFavPetSB(){
$mysidia = Registry::get("mysidia");

$userfavpet = $mysidia->db->select("users_profile", array("favpet"), "username = '{$mysidia->user->username}'")->fetchColumn();
if ($this->userfavpet == "0"){
$this->FavPetSB = new Paragraph;
$this->FavPetSB->add(new Comment("<b>No Favorite Pet Set</b>"));
}
else{
$this->FavPetSB = new Paragraph;
$this->FavPetSB->add(new Comment("<b>Favorite Pet!</b>"));
$this->FavPetSB->add(new Link("levelup/click/{$userfavpet}", new Image("levelup/siggy/{$userfavpet}"), TRUE));
}

$this->setDivision($this->FavPetSB);         

}


Kyttias 01-13-2015 09:34 AM

If you like, I've been slowly working on this guide.

Or, in short, for just your question:

Database SQL Statements
You’ll need to have access to the registry first, in whatever function you're dealing with, of course.
PHP Code:

$mysidia Registry::get("mysidia"); 

Counting Rows
Count how many rows match a specific query:
PHP Code:

$mysidia->db->select("Table", array("Column"), "Row='{$somevalue}' and Row >= {$somevalue}")->rowCount(); 

Compare to:
Code:

SELECT COUNT(Row) FROM Column WHERE Row='$somevalue' and Row >= {$somevalue};
Where Data
Fetching an exact value in a specified column:
PHP Code:

$mysidia->db->select("Table", array("Column"), "Row = '$somevalue'")->fetchColumn(); 

Compare to:
Code:

SELECT Column FROM Table WHERE Row='$somevalue';
Fetching Row Data as an Object
Example:
PHP Code:

$pet $mysidia->db->select("owned_adoptables", array(), "owner='{$mysidia->user->username}' ORDER BY RAND() LIMIT 1")->fetchObject(); 

You could now use {$pet->name} and {$pet->currentlevel}, etc. (This example chooses a random pet owned by the user.)
Creating a New Row of Data
Example:
PHP Code:

$mysidia->db->insert("Table", array("Column" => $somevalue"Column" => 'somevalue')); 

Compare to:
Code:

INSERT INTO table_name (Column1, Column2, Column3) VALUES ($somevalue1, $somevalue2, $somevalue3);
Updating Data
PHP Code:

$mysidia->db->update("Table", array("Column" => '$somevalue'), "Row = '$somevalue'"); 

Compare to:
Code:

UPDATE Table SET Column='$somevalue' WHERE Row='$somevalue';

Kyttias 01-13-2015 09:57 AM

As for your code above, because the Sidebar widget is partially controlled on the AdminCP you will also have to make changes there. AdminCP > Module > Create New Module:
http://fc03.deviantart.net/fs71/f/20...as-d8dr0j8.png

You could also probably manually do this in the database (in the _modules table), but it's just not being done with code alone. (Note: the Module Order slot controls the order in which the modules in the sidebar will appear, lower numbers are higher.)

I also notice your code above is only a setter but there is no getter. The money bar has a getter, so just add one similar above your code:
PHP Code:

public function getFavPetSB(){
        return 
$this->FavPetSB;
    } 

I also propose the following changes in your code:

PHP Code:

protected function setFavPetSB(){
        
$mysidia Registry::get("mysidia");

        
$userfavpet $mysidia->db->select("users_profile", array("favpet"), "username = '{$mysidia->user->username}'")->fetchColumn(); 
        if (
$this->userfavpet == "0"){
            
$this->FavPetSB = new Paragraph
            
$this->FavPetSB->add(new Comment("<b>No Favorite Pet Set</b>"));
        }
        else{
            
$adopt = new OwnedAdoptable($userfavpet);
            
$this->FavPetSB = new Paragraph
            
$this->FavPetSB->add(new Comment("<b>Favorite Pet!</b> <br/>
                <a href='/myadopts/manage/
{$userfavpet}'><img src='{$adopt->getImage()}'></a>
                "
));
        }

        
$this->setDivision($this->FavPetSB);
    } 

Using "$adopt = new OwnedAdoptable($userfavpet);" you can also pull up data like {$adopt->getName()} and {$adopt->getCurrentLevel()}.

Lastly, the code you had looked familiar, but it's something that's slightly broken in the code itself, currently. Inside class_userprofile.php I'd replace the getFavpet function:

PHP Code:

 public function getFavpet(){ 
      if(
is_numeric($this->favpet)){ 
          
$adopt = new OwnedAdoptable($this->favpet); 
          
$favimg $adopt->getImage(); 
          
$this->favpet = ($this->favpet == 0)?new Comment("None Selected"):new Link("levelup/click/{$this->favpet}", new Image($favimg), TRUE);  
      }  
      return 
$this->favpet;       
  } 

Because unfortunately, levelup/siggy/{$numberhere} is not an image, not even if you add a file extension to it.

MikiHeart 01-14-2015 05:01 AM

Thank you so much for the basic run down and the guide.

As for the code, thank you for the edits :) I already have the getter, and the module set up.

The code works, I'm just rusty and needed a second pair of eyes to make sure.

I know that levelup/siggy/{$numberhere} isn't an image, yet it still works, oddly enough. But you're right, it should be changed.

Thank you again for the help. I can finally start working on editing the current features and adding my own.

(I plan to add my own item rarity system, but also add a rarity system for pets. Because I want them to be random in what they adopt. Since our site is focused on collecting)

Kyttias 01-14-2015 10:45 AM

Nice! I also added an item rarity to my site. It has little use right now, but combined with a shop loyalty system, I'll be withholding some items from shops until a user has 'levelled up' a shop, thus, rarer items will be in shops after a user has gained enough shop loyalty. I'll also be having item capsule ball machines and drop chances while exploring, these effected by arrays of different rarities of items. ^^

MikiHeart 01-15-2015 01:29 AM

Yes, I saw that in your thread! I love that idea so much.
I also love the capsule ball machines. Gacha as they call it in Japan.

I want to have items drop as well, but I want to make it so they have to claim it. So that people can't use refreshers or auto scripts and stuff.

I also want to work on adding a stocking and restocking system for my stores.

My site is all to do with collecting you see XD Collecting pets, and items, and all kinds of goodies. Because I love collecting when it comes to websites.

MikiHeart 01-15-2015 07:52 AM

I'm trying to build a custom page, but it kinda breaks the theme. It makes the background on the default theme white, and in the bootstrap one, it changes the theme colours around (To colours that are in the theme, but different ones) and it makes the text bigger.

test.php
Code:

<?php


class TestController extends AppController{
       
        public function index(){
          $mysidia = Registry::get("mysidia");

        }
}
?>

testview.php
Code:

<?php

class TestView extends View{
   
       
        public function index(){
            $mysidia = Registry::get("mysidia");
        $document = $this->document;
        $document->setTitle("Test");
        $document->add(new Comment("Test"));
               
        }
}
?>

Edit: I don't get it. It's the same as in blank.php and blackview.php (Not sure why misspelled..) Changing blackview.php to blankview.php and adding a title and the theme works fine..

Kyttias 01-15-2015 09:54 AM

Well, it definitely shouldn't be misspelled. The view should match the controller. Blank always gave me troubles, too... I ended up using the Donate page as my base - it also helped me understand how form data was being tossed from the view to control and back.

MikiHeart 01-15-2015 11:07 PM

It's weird, because the blank page actually works. It's weird.

I will try the donate page as a base. If it still doesn't work, I'll post a new topic and hopefully HoF can give some insight.


All times are GMT -5. The time now is 06:54 AM.

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