PHP不在

时间:2017-10-30 08:22:36

标签: php

此代码适用于Telegram bot。 所以当用户输入“类别检查”时,我试图传递该值,这将返回一个新闻标题和问题,询问它是否是正确的类别。 但是在用户回答“是”之后,由于某种原因,变量被重置,因此$ title,$ cat和$ check是空白的。

我该如何解决这个问题?

function processMessage($message) {
  // process incoming message
  $message_id = $message['message_id'];
  $chat_id = $message['chat']['id'];
  $sender_id = $message['from']['id'];
  $sender_first_name = $message['from']['first_name'];
  $sender_last_name = $message['from']['last_name'];
  $message_date = $message['date'];

  if (isset($message['text'])) {
    // incoming text message
    $text = $message['text'];

//when user starts the bot
    if (strpos($text, "/start") === 0) {
      apiRequestJson("sendmessage", array('chat_id' => $chat_id, "text" => 'Please select an option:', 'reply_markup' => array(
        'keyboard' => array(array('Category Check', 'Uncategorized')),
        'one_time_keyboard' => false,
        'resize_keyboard' => true)));

    }

    //category check that returns news title and category question to user
    else if ($text === "Category Check") {

      //MySQL Connection Details
        $servername = "localhost";
        $username = "user";
        $password = "pass";
        $dbname = "dbname";
        $conn = new mysqli($servername, $username, $password, $dbname);

        $sql = "SELECT * FROM `newsdata` WHERE `nb_count` OR `svm_count` <> 0 ORDER BY RAND() LIMIT 1 ";

        $row = $result->fetch_assoc();

        $nb_business = $row["nb_business"];
        $nb_entertainment = $row["nb_entertainment"];
        $nb_health = $row["nb_health"];
        $nb_politics = $row["nb_politics"];
        $nb_science = $row["nb_science"];
        $nb_technology = $row["nb_technology"];
        $nb_world = $row["nb_world"];
        $svm_business = $row["svm_business"];
        $svm_entertainment = $row["svm_entertainment"];
        $svm_health = $row["svm_health"];
        $svm_politics = $row["svm_politics"];
        $svm_science = $row["svm_science"];
        $svm_technology = $row["svm_technology"];
        $svm_world = $row["svm_world"];
        $title=$row["title"];
        $nb_count=$row["nb_count"];
        $svm_count=$row["svm_count"];

        if($nb_business!=0)
        {$cat='BUSINESS'; $track=1;}
        else if ($nb_entertainment!=0)
        {$cat='ENTERTAINMENT'; $track=1;}
        else if ($nb_health!=0)
        {$cat='HEALTH'; $track=1;}
        else if ($nb_politics!=0)
        {$cat='POLITICS'; $track=1;}
        else if ($nb_science!=0)
        {$cat='SCIENCE'; $track=1;}
        else if ($nb_technology!=0)
        {$cat='TECHNOLOGY'; $track=1;}
        else if ($nb_world!=0)
        {$cat='WORLD'; $track=1;}

        else if ($svm_business!=0)
        {$cat='BUSINESS'; $track=0;}
        else if ($svm_entertainment!=0)
        {$cat='ENTERTAINMENT'; $track=0;}
        else if ($svm_health!=0)
        {$cat='HEALTH'; $track=0;}
        else if ($svm_politics!=0)
        {$cat='POLITICS'; $track=0;}
        else if ($svm_science!=0)
        {$cat='SCIENCE'; $track=0;}
        else if ($svm_technology!=0)
        {$cat='TECHNOLOGY'; $track=0;}
        else if ($svm_world!=0)
        {$cat='WORLD'; $track=0;}
          // output data of each row

//asking user if the category is correct
          apiRequestJson("sendmessage", array('chat_id' => $chat_id, "text" => 'Is this news belongs to '.
          $cat.' category? '.
          $title.' '.$row["link"],
          'reply_markup' => array(
          'keyboard' => array(array('yes', 'no')),
          'one_time_keyboard' => false,
          'resize_keyboard' => true)));
    }
    //user answers
    if ($text == "yes" || $text == "no"){
          switch ($text) {
            case "yes":
                $nb_count=$nb_count--;
                $svm_count=$svm_count--;
                apiRequestJson("sendmessage", array('chat_id' => $chat_id, "text" => $cat.$track.$title."variable check")); //broken here, output: variable check

                break;
            case "no":
                //dothat; break;
          }

1 个答案:

答案 0 :(得分:0)

根据您重新调用该函数的方式,我可以考虑这个问题的3种可能的解决方案。

使用静态或全局变量

如果在再次调用函数之前会话未结束,则可以使用函数内部的静态变量或全局变量来存储数据。如果使用全局变量解决方案,请记住在函数中将变量定义为“global $ your_variable_name”。

使用会话变量或COOKIE

如果会话将在函数调用之前结束但同一用户将再次调用,那么您可以使用浏览器cookie的会话变量来存储您希望保持持久性的数据。

在数据库中存储持久值

从您问题的标题我认为您可能需要在来自多个用户的呼叫之间保持持久的值。如果是这种情况,那么您需要将持久值存储在数据库中,例如mySQL。