Getting name of random item

Forum
Last Post
Threads / Messages

Dinocanid

Member
Member
Joined
Aug 30, 2016
Messages
520
Points
18
Age
23
Location
Maryland, USA
Mysidian Dollar
43,334
I'm having a problem with telling the user what item(s) they're getting. In short a random item is chosen from a list of items and given to the user. Whenever I try to get the name of the item I get an error. Here's the shortened version of the code:
PHP:
$random = mt_rand(1,3);

$Cnum = mt_rand(1,5);
                            switch ($Cnum){ 
                                case 1: $Citem = "ball"; break; //Common items
                                case 2: $Citem = "ball"; break; 
                                case 3: $Citem = "ball"; break; 
                                case 4: $Citem = "ball"; break; 
                                case 5: $Citem = "ball"; break; 
                            }
if($random == 1){
$newitem1 = new StockItem($Citem);
                        $newitem1->append(1, $mysidia->user->username);
                        $document->add(new Comment("Congratulations! You earned a {$newitem1}!"));
                        return;
}
(For testing purposes, the only item is a ball. The error also appears if the number is 2 or 3)

If I just get {$Citem} as the item name then it works fine, but I want the words to match the items given, not be randomly chosen.
 
Last edited:
Since I got error messages back, I was able to find this as the error:
Catchable fatal error: Method StockItem::__toString() must return a string value in /home/adopttes/public_html/view/treasureview.php on line 103
I tried using this like strval and (string) but that didn't work.

EDIT: I was able to fix it! I had to use this to get the name of the item:
$ni1_name = $newitem1->itemname;
$document->add(new Comment("Congratulations! You earned a {$ni1_name} !"));
 
Last edited:

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

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

Latest Threads

Latest Posts

Top