R
R
Ruslan NoFam2021-06-11 09:51:23
PHP
Ruslan NoFam, 2021-06-11 09:51:23

Why is the variable defined as unknown?

Good day!
The telegram bot connected with the store, the task of the bot is to notify about new orders. Faced a problem, an unknown variable.

public function actionCheckout()
    {
        if (empty($_SESSION['cart'])) {
            return $this->redirect(['site/error']);
        }
        $model = new Orders();
        if ($model->load(Yii::$app->request->post())) {
            $model->total_sum = $_SESSION['cart.sum'];
            if ($model->validate() && $model->save()) {
                $token = "1824070568:AAGZbzn4rlXr17YJWQkGWuWgJjyEXTE9gVU";
                $chat_id = "-548130760";
                $arr = array('<b><a href="https://asp-polimer.uz/admin/orders/view?id=' . $model->order_id . '"> Новый заказ #' . $model->order_id . '</a></b>' => '', '‍♂️ Имя:' => $model->costumer_name, '☎️ Телефон: ' => $model->costumer_phone, ' Дата: ' => date('d.m.Y в H:i'), ' Сумма: ' => number_format($model->total_sum, 2, '.', ' ') . ' сум',);
                foreach ($_SESSION['cart'] as $item) {
                    $ar[' Товар: '][] = $item['name'] . '  ' . $item['qty'] . ' шт * ' . number_format(ceil($item['price']), 0, '.', ' ') . ' ₸';
                    $order_items = new OrdersItems();
                    $order_items->order_id = $model->order_id;
                    $order_items->product_id = $item['product_id'];
                    $order_items->product_code = $item['product_code'];
                    $order_items->product = $item['name'];
                    $order_items->qty = $item['qty'];
                    $order_items->price = $item['price'];
                    $order_items->total_sum = $item['price'] * $item['qty'];
                    $order_items->save();
                }
                foreach ($arr as $key => $value) {
                    $txt .= "<b>" . $key . "</b> " . $value . "\n";
                };
                foreach ($ar as $key => $value) {
                    if (is_array($value)) {
                        foreach ($value as $tovs) {
                            $txt .= "<b>" . $key . "</b> " . $tovs . "\n";
                        }
                    }
                };
                $txt = rawurlencode($txt);
                $sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}", "r");
                $session = Yii::$app->session;
                $session->open();
                $session->remove('cart');
                $session->remove('cart.qty');
                $session->remove('cart.sum');
                return $this->redirect(['cart/order-success']);
            } else {
                Yii::$app->session->setFlash('error', "Ошибка! Что-то пошло не так...");
            }
        }
        return $this->render('checkout', ['model' => $model]);
    }


Error:
PHP Notice - yii\base\ErrorException
Undefined variable: txt

$txt .= " " . $key. " " . $value. "\n";

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2021-06-11
@Humon91072

Expand the record, and it will be clear:

$txt = $txt /* вот в этом месте $txt используется, но еще не существует */
     . "<b>" . $key . "</b> " . $value . "\n";

$txt = ''; // нужно сначала инициализировать
foreach ($arr as $key => $value) {
  $txt .= "<b>" . $key . "</b> " . $value . "\n";
};

S
sl0, 2021-06-11
@sl0

What's wrong with the error?
You are trying to concatenate some value to $txt without first defining it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question