Seeking basic run down of php and database

Forum
Last Post
Threads / Messages

MikiHeart

Premium Member
Premium Member
Joined
Apr 26, 2009
Messages
186
Points
0
Age
32
Mysidian Dollar
13,385
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=!!!
 
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);           

}
 
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:
$mysidia = Registry::get("mysidia");

Counting Rows
Count how many rows match a specific query:
PHP:
$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:
$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:
$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:
$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:
$mysidia->db->update("Table", array("Column" => '$somevalue'), "Row = '$somevalue'");
Compare to:
Code:
UPDATE Table SET Column='$somevalue' WHERE Row='$somevalue';
 
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:
sc_by_kyttias-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:
public function getFavPetSB(){
		return $this->FavPetSB;
    }

I also propose the following changes in your code:

PHP:
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:
 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.
 
Last edited:
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)
 
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. ^^
 
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.
 
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..
 
Last edited:
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.
 
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.
 
Hey, a little update on the script I was working on. If the user updates their profile, and select no pet, it becomes set to 0, this this happens:

Code:
Fatal error: Uncaught exception 'AdoptNotfoundException' with message 'Adoptable ID 0 does not exist or does not belong to the owner specified...' in C:\wamp\www\micronoms\classes\class_ownedadoptable.php on line 26
( ! ) AdoptNotfoundException: Adoptable ID 0 does not exist or does not belong to the owner specified... in C:\wamp\www\micronoms\classes\class_ownedadoptable.php on line 26

I'll do some more work on it.

Edit:

I fixed it XD With one minor change!

From:
Code:
if ($this->userfavpet == "0"){

To:
Code:
if ($this->userfavpet == "0" or $this->userfavpet == ""){
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,119
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Top