Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:42:26+00:00 2026-06-09T04:42:26+00:00

I have a MySQL database that holds a list of classes in columns, and

  • 0

I have a MySQL database that holds a list of classes in columns, and each class has a “level” which is the value of the row, which changes for each person. So, math, for example, has 0, 1, 2, and 3 as possible values, 0 being not selected as a class, and 1, 2, and 3 being high, medium, and low, respectively.

I have a MySQL query which pulls just the classes from a user’s database row.

$result = mysql_query("SELECT math, physics, biology, chemistry, english, spanish, history, economics, art, theoryofknowledge, extendedessay FROM users WHERE username = '". $login_session ."'") or die(mysql_error());  

        while($row = mysql_fetch_array( $result )) {
            echo "Math:". $row['math'] ."<br />";
            echo "Physics:". $row['physics'] ."<br />";
            echo "Biology:". $row['biology'] ."<br />";
            echo "Chemistry:". $row['chemistry'] ."<br />";
            echo "English:". $row['english'] ."<br />";
            echo "Spanish:". $row['spanish'] ."<br />";
            echo "History:". $row['history'] ."<br />";
            echo "Economics:". $row['economics'] ."<br />";
            echo "Art:". $row['art'] ."<br />";
            echo "Theory of Knowledge:". $row['theoryofknowledge'] ."<br />";
            echo "Extended Essay:". $row['extendedessay'];
        }

Here is the output for a user:

Math:1
Physics:1
Biology:0
Chemistry:2
English:2
Spanish:3
History:0
Economics:1
Art:0
Theory of Knowledge:1
Extended Essay:1

How could I determine which level a user has, without doing an if statement for every single time the class is called? I need to call the classes in multiple places on the site, and I’d like an easy way to check which classes, and level, the user has.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:42:27+00:00Added an answer on June 9, 2026 at 4:42 am

    I think the best approach would be to build a PHP class that loads the user in and inside it have a function that can accept a class (Biology, Math etc) and return the level of the user. If you wanted to, you could write something simple that performs a check of the level required and returns a true or false depending on if the user is a high enough level.

    I even slapped together some super basic structure you might want to extend upon:

    <?php
    
        class myUser
        // You are making an object here that stores information about your user.
        // This will mean you only need to query that data once from the DB, then
        // you can use it anywhere on the page without needing to do more queries.
        {
            public $math;
            public $biology;
            // I am making public variables here based on your columns
            // You cuold just as easily make an array for example to store them in.
    
            public function __construct($userID)
            // Making a construct class - meaning you will be able to write a snippet
            // like this: $currentUser = new myUser(6);
            // and the user information will be loaded nicely for you
            {
                $query="select math, biology from users where ID=$userID";
                // database stuff ....
                // this is where you would write your actual code to get the info
                // from the database and populate it properly, not like I did
                // below for this example
                $this->math=4;
                $this->biology=2;
            }
    
            public function checkUserLevel($myTopic, $reqLevel)
            // Making use of a few things here that I should ntoe:
            // This is a function you can call from the main code below
            // like this: $currentUser->checkUserLevel('math',3)
            // it will return either true or false.
            // I have used variable variables here for the $myTopic to
            // make it easier. You normally access an element differently
            // normally it is like: echo $this->math;  // output 4
            // Also I am using a ternary operator to return the data,
            // which is just a shortcut.
            {
                return ($this->$myTopic>=$reqLevel)? true : false;
            }
    
            public function returnUserLevel($myTopic)
            {
                return $this->$myTopic;
            }
    
        }
    
        $currentUser = new myUser(6);
        // This is creating a new user object based on the class we made above.
        // Further Explanation:
        // We have a class called myUser, but a class is just a schematic.
        // The $currenUser bit defines a new variable in our code.
        // the "= new myUser" bit says that we want to use the schematic above for this variable
        // the "myUser(6)" basically gives the constructor function an ID to get from
        // the database for the user. Because we defined a constructor class that expects
        // an ID, we need to supply it one, else we will get an error.
        // So $currentUser = new myUser(6)
        // really means "Make me a new variable called $currentUser and make it of the myUser
        // class type, and when making it, fetch me the details of student ID 6 and populate it
        // with their data.
    
        if($currentUser->checkUserLevel('math',3))
        // Now I am using one of the functions called checkUserLevel, supplying it
        // with the two arguments it needs and depending on it it returns true or false
        // doing one action or another.
        {
            echo "The user is at least level 3.\n";
        }
        else
        {
            echo "The user is lower than level 3.\n";
        }
    
        if($currentUser->checkUserLevel('biology',12))
        // same check, differnt inputs
        {
            echo "The user is at least level 3.\n";
        }
        else
        {
            echo "The user is lower than level 3.\n";
        }
    
    
        // You can output like this for example:
        echo "The users math is at: ".$currentUser->math;
    
        // and I added a little function that will simply return the level for you of the subject you enter.
        echo "The user is at math level ".$currentUser->returnUserLevel('math');
    
        // lastly you can do something like this:
        $allSubjects=array('math','physics','biology');
        for($i=0;$i<count($allSubjects);$i++)
        {
            echo "The users ".$allSubjects[$i]." level is at ".$currentUser->returnUserLevel($allSubjects[$i])."<br><br>";
        }
    
    ?>
    

    The output of code is:

    The user is at least level 3.
    
    The user is lower than level 3.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql database that has text stored in it with non encoded
I have a MySql database that stores a timestamp for each record I insert.
I have several columns in a MySQL database that I would like to add
I inherited a mysql database that has a table with columns like this: object_id,
I have a database that has two tables, one of which contains a foreign
I have a mysql database that holds content as a blob, for whatever reason
I have a MySql database that holds datetime in one field, in this format:
I'm really upset with Hibernate! I have a database table (mysql) that holds parent-child
I have a database (MySQL) that holds cost records for some of our customers
I have a database table that holds the number of points a user has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.