Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Questions and Supports

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 12-27-2016, 09:57 AM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 516
Gender: Unknown/Other
Credits: 65,406
Dinocanid is on a distinguished road
Default "For loop" statements

I was wondering how it would be possible to use "for loop" statements with the mysidia script since it can run a single line of code multiple times. I've looked at a bunch of tutorials online and I'm still lost with what to do with it. I wanted to use it to clean up the raffle script since, at the moment, the same line of code is copy-pasted over and over to make multiple database entries, like so:

PHP Code:
public function index(){
        
$mysidia Registry::get("mysidia");
        
$document $this->document;        
            
$document->setTitle("Pet Outcomes");
                
$pool $mysidia->db->select("raffle", array("pool"))->fetchColumn();
$document->add(new Comment"There is currently {$pool} CURRENCY in the pool!"));
$document->add(new Paragraph());

//This section checks the date!
      
$currenttime time();
      
$timerange = array(strtotime($this->stopdate));
         if(!empty(
$timerange[1]) and $currenttime $timerange[1]){
             
// The raffle has ended!
             
$winner $mysidia->db->select("raffle_tickets", array("owner"), "1 ORDER BY RAND() DESC LIMIT 1")->fetchColumn();
                         
$document->add(new Comment"{$winner} won {$pool} CURRENCY from the raffle! Congratulations!"));    
                         
$endraffle TRUE;
         }
         else{
  
//This is the ticket form!
$ticketForm = new FormBuilder("ticketForm""""post");
$exampleDropdown = new DropdownList("myList");
$exampleDropdown->add(new Option("5 tickets""5")); 
$exampleDropdown->add(new Option("10 tickets""10"));
$exampleDropdown->add(new Option("15 tickets""15"));
$exampleDropdown->add(new Option("20 tickets""20"));
$ticketForm->buildComment("Amount: "FALSE);
$ticketForm->add($exampleDropdown);
$ticketForm->buildComment("<br></br>");
  
$ticketForm->buildButton("Buy tickets""buy""submit");
        
$document->add($ticketForm);
}
//This part adds the number of tickets the user buys to the database
if($mysidia->input->post("buy")){
$chosenOption $mysidia->input->post("myList");
    if(
$chosenOption == "5"){
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
           }
    elseif(
$chosenOption == "10"){
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
           }
    elseif(
$chosenOption == "15"){
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
           }           
    elseif(
$chosenOption == "20"){
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
        
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
           }  
        }
        
        
//This park picks a winner from the database
if($mysidia->input->post("win")){
$winner $mysidia->db->select("raffle_tickets", array("owner"), "1 ORDER BY RAND() DESC LIMIT 1")->fetchColumn();
$document->add(new Comment"{$winner} won {$pool} CURRENCY from the raffle!"));

It works, but it's not ideal and the code would get very lengthy if more tickets are wanted (like 50).
__________________
Reply With Quote
  #2  
Old 04-11-2017, 12:01 PM
ewe ewe is offline
Member
 
Join Date: Mar 2017
Posts: 8
Gender: Female
Credits: 1,758
ewe is on a distinguished road
Default

I realize this is a very old question but I peeked at your Raffle Mod and noticed it's still using the if statement so I'm sorry if you have already found your answer but in case you haven't (or someone in the future needs the answer) here it is -

You should just be able to do something like this:
PHP Code:
for ($i 1$i <= $chosenOption$i++) {
    
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));

I suspect you may have tried that already and had errors or weird results.

PHP is a loosely typed language and while that is usually convenient it can sometimes cause variables to misbehave and do strange things. Strongly typed languages require you to define what type of variable you're making when you name it. (ex: string, float, int, etc).
  Spoiler: PHP vs Java 
A for loop in Java is an easy way to demonstrate the difference as the code is almost exactly the same.
Code:
for(int i=1; i<11; i++) {
   System.out.println("Count is: " + i);
}


Data from $mysidia->input->post("stuff") behaves like a string but you can't check if $i is less than a string because a string could very well be something like "sheep" and it would be very difficult to count to sheep.

Luckily, it is very easy to turn a string into a number type of variables.
PHP Code:
$chosenOption $mysidia->input->post("myList");
$chosenOption $chosenOption 0
I've found PHP generally sees something like $var = 1; to actually be $var = TRUE; and does not recognize 1 as an integer. FALSE, 0, and '' (empty string) are also all the same thing but NULL is NULL except on a boolean check will return FALSE.

As an aisde, when checking something like the options from a dropdown menu you may be better off using a switch statement instead of a long if-else if-else. I find they're a bit easier to read (as long as you remember to include the break;!) and I believe they're a little less resource intense though that doesn't matter very much here.

Mysidia is written in PHP and PHP has a lot of interesting functions. Two great resources for looking things you need up (besides Google, Google will likely direct you to one of the two if you ask PHP questions and it's a great way to get where you're going faster) are the PHP Manual and W3Schools. I strongly prefer the PHP Manual (the comments are especially helpful) but I have friends who understand the W3School explanations better.
Reply With Quote
  #3  
Old 04-11-2017, 04:21 PM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 516
Gender: Unknown/Other
Credits: 65,406
Dinocanid is on a distinguished road
Default

I tried both resources (and google of course), but I found it hard to understand what does what and how to apply it. I'm kind of sick right now, but I'll try out what you posted once I'm better.
__________________
Reply With Quote
  #4  
Old 04-11-2017, 10:11 PM
ewe ewe is offline
Member
 
Join Date: Mar 2017
Posts: 8
Gender: Female
Credits: 1,758
ewe is on a distinguished road
Default

Oh, I'd be more than happy to explain how a for loop works! I think it's the easiest way to explain what a loop is to someone because they're pretty straightforward.

Let's say Bart Simpson had a brilliant idea to make school a little more exciting one day but Mrs. Crab Apple decided to ruin all the fun like always. In her characteristic punishment (because she's so creative) she orders Bart to write a correction for his behavior 100 times on the chalk board.

If Bart could enter the 21st century and use a computer instead of a chalkboard he could use a for loop and be done in a matter of minuets.

This example is very important and not just an excuse for me to include a picture with the word sheep. I swear.

A for loop is usually used to go through a set of actions an exact number of times. The syntax is similar to if(conditions) {do stuff;} so I'll explain what actually happens inside the parenthesis.

for ($i = 1;
Here you create a variable that acts as a counter. Usually it is set to 1 but you can actually start at any integer. You can reuse $i in different loops but if you have a for loop inside a for loop you need to use different counters. (I tend to use $i and $j)
$i <= $numberOfTimes;
Here you compare your counter to your target variable. You can use an integer like 42 or you can use a $variable. You shouldn't ever say $i = $target; because it is theoretically possible you could miss your target and end up with an infinite loop. If you use <= (or >=) you'll include the target value. If you want something to happen 10 times you can either set $i = 1 and $i <= 10 or you could set $i = 0 and $i < 10.
$i++) {
This is just shorthand for $i = $i + 1); If you wanted a for loop that counts down instead just use $i--!Note there is no ; here! I'm not sure why but all of the languages I've seen (like 3) use this format
&nbsp;&nbsp;&nbsp;stuff happens over and over;
}

  Spoiler: If you've taken Calculus it's a bit like summation notation 


x = 1 below the Sigma is like $i = 0, the number above the the sigma is the target value
Actually this site gives a decent explanation if you need a refresher or would just like another different explanation.
Reply With Quote
  #5  
Old 04-23-2017, 06:31 PM
p810 p810 is offline
Member
 
Join Date: Apr 2017
Posts: 6
Gender: Unknown/Other
Credits: 901
p810 is on a distinguished road
Default

Here is a simplified version.

PHP Code:
if ($mysidia->input->post('buy')) {
    
$chosenOption = (int) $mysidia->input->post('myList');

    for (
$i 1$i $chosenOption$i++) {
        
$mysidia->db->insert('raffle_tickets', array(
            
'owner' => $mysidia->user->username
        
));
    }

It casts the type of $chosenOption to an integer. Like explained above the for loop just iterates over the block of code until the condition is false; i.e. $i (the counter) is greater than $chosenOption.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:19 PM.

Currently Active Users: 9913 (0 members and 9913 guests)
Threads: 4,080, Posts: 32,024, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636