need help with this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

need help with this code

Post by cdoyle »

Hi again,
I hope I'm on the right track with this, and just missing something small.

I'm trying to create for my game a piece of code that checks the players education history, and then verify that 1. They didn't already take the class in question, and 2. Make sure they are not already enrolled in another class. If they meet both those requirements it will then enroll them in the class the chose.

What I have so far, it checks both requirements fine but I can't seem to get the final step to work. For testing I was just trying to get it to display the welcome message, but it doesn't display. It's just blank, so I think the else is in the wrong spot, or I have coded this all wrong.

I was trying to use a 'while' to check all the rows to make sure the player hasn't already taken the class, or enrolled in any other classes. The way I was coding worked fine, until the player passes all the requirements and ready to enroll, then nothing happens.

Here is the code where the checks and then final else after.

Code: Select all

 
           $educationhistory = $db->execute("SELECT `classes_taken`, `player_id`, `days_left` FROM `education` WHERE `player_id`=?", array($player->id));
            while ($educationhistory1 = $educationhistory->fetchrow())
            {
                if ($educationhistory1['classes_taken'] == $classid)
                {
                    echo "You've already taken this class, apparently you didn't learn much";
                    break;
                }
                //check to verify not currently taking a class
                else if ($educationhistory1['days_left'] > 0)
 
                {
                    echo "Sorry you are currently already enrolled in a class<br>";
                    echo "Do you really think you're fooling anyone, you won't pass that one either";
                    echo "<a href=\"home.php\">home</a>";
                    break;
                }
                else
                {
                    //player meets requirements update gold and enroll in class
 
                    echo "Thank You and Welcome<br>";
                    echo "<a href=\"home.php\">Home</a>";
                }
            }
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: need help with this code

Post by requinix »

Your code is a bit off. You can only allow the enrollment if the requirements are met for all classes so does it make sense to put the code to enroll inside the loop? While in there you don't know if all the classes have been examined yet.

I'd propose you do it a bit differently.
Run a query that only looks for classes that (a) match the class ID or (b) aren't over yet. If there are any you know immediately that they can't enroll. You could give a simple "You can't" explanation, or you could go through the results and explain (for each one) why.

Code: Select all

$educationhistory = $db->execute("SELECT `classes_taken`, `player_id`, `days_left` FROM `education` WHERE `player_id`=? AND (`classes_taken`!=? OR `days_left`>0)", array($player->id, $classid));
(From there it depends what DAO you're using)
cdoyle
Forum Contributor
Posts: 102
Joined: Wed Feb 13, 2008 7:26 pm

Re: need help with this code

Post by cdoyle »

Thank You!
This is much simpler then what I was trying to do.

Thanks again!
Chris.
Post Reply