简体   繁体   中英

PHP data selection for odd and even numbers

I have tables with ID in my database. I'm trying to make sure that one API is used for an even number, so that another API is used for an odd number.

The variable that I need to use for different numbers protected $apiKey = '5cef0578-acbc-4523-88c8-b47634ca3ba6';

My code:

class RandomOrgClient
{

    protected $url = 'https://api.random.org/json-rpc/2/invoke';
    protected $apiKey = 'MY KEY';
    protected $timeLimit = 300;


    function __construct()
    {
        $this->setTimelimit($this->timeLimit);
    }

I tried to do something like:

class RandomOrgClient
{

    protected $url = 'https://api.random.org/json-rpc/2/invoke';

    if ($games_count %2 == 0) {
        protected $apiKey = 'KEY FOR ODD NUMBER';
    } else { 
    protected $apiKey = 'KEY FOR EVEN NUMBER';
    }
    protected $timeLimit = 300;


    function __construct()
    {
        $this->setTimelimit($this->timeLimit);
        $games_count = DB::table('game_double')->count();

    }

But every time i received error Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) or const (T_CONST) .

What am I doing wrong? How can I fix a mistake?

UPD:

I made changes as suggested by 2 people, but now there is another problem. 1 time the code generated, the second time does not want, it's a blank page. Most likely due to the fact that I am use not correctly request at $games_count variable . As I understand it, even or odd should take from ID the table. I little changed variable to $games_count = DB::table('game_double')->where('id')->get(); , and I get a white page in the browser, where my mistake?

You need to move that conditional code into the constructor.

class RandomOrgClient
{
    protected $url = 'https://api.random.org/json-rpc/2/invoke';
    protected $apiKey;
    protected $timeLimit = 300;

    function __construct()
    {
        $this->setTimelimit($this->timeLimit);
        $games_count = DB::table('game_double')->count();
        if ($games_count %2 == 0) 
        {
            $this->apiKey = 'KEY FOR ODD NUMBER';
        }
        else 
        { 
            $this->apiKey = 'KEY FOR EVEN NUMBER';
        }
    }
    // ...
}

You can't put code in a class without it being in a function. Initialization, yes, but no conditions or the like. Move that all to your __construct function:

class RandomOrgClient
{
    protected $url = 'https://api.random.org/json-rpc/2/invoke';
    protected $apiKey = 'KEY FOR EVEN NUMBER';
    protected $timeLimit = 300;

    function __construct()
    {
        $this->setTimelimit($this->timeLimit);
        $games_count = DB::table('game_double')->count();
        if ($games_count %2 == 0) {
            $this->apiKey = 'KEY FOR ODD NUMBER';
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM