Answer the question
In order to leave comments, you need to log in
How to change a parameter in database via button in Yii2?
There is Yii2 Advanced, as well as a standard template. I'm trying to edit it for my tasks, namely a test freelance site.
At the moment, a database has been created and tasks are being displayed on the page. This is represented as follows:
Only jobs that have an available item in the database equal to zero are displayed. If it is equal to one, then the task has an executor.
When the user clicks on "Accept", ideally a signal should be sent to the controller, where the database on the corresponding order changes available by one.
The problem is that I can't pass the order id through the button click in any way. Initially, I tried to work with it as a button, tried to use ActiveForm and so on, but nothing came of it. It was possible to display the order id in Url, but could not accept it in the controller. I will be grateful for any help.
Controller code:
<?php
namespace frontend\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Orders;
use yii\helpers\Url;
use yii\web\Request;
class OrdersController extends Controller
{
public function actionIndex()
{
$query = Orders::find();
$pagination = new Pagination([
'defaultPageSize' => 5,
'totalCount' => $query->count(),
]);
$orders = $query->orderBy('available')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index', [
'orders' => $orders,
'pagination' => $pagination,
]);
}
public function actionTakeOrder()
{ $query = Orders::find();
$request = Yii::$app->request;
$get = $request->get(value);
$query = orders::findOne($get);
$Orders->available = 1;
$customer->update();
}
}
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\bootstrap\ActiveForm;
$this->title = 'Поиск заказа';
?>
<h1>Доступные заказы</h1>
<ul>
<form action="OrdersController.php" method="post">
<table>
<?php foreach ($orders as $orders): ?>
<tr>
<td><?= Html::encode("{$orders->header}") ?></td>
<td><?= $orders->text ?></td>
<td><?= $orders->size ?></td>
<fieldset><td>
<?= Html::a('Принять', ['orders/index','value' => ($orders->id)], ['class'=>'btn btn-primary']) ;?>
</td>
</fieldset>
</tr>
<?php endforeach; ?>
</form>
</ul>
</table>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
<?php
namespace app\models;
use Yii;
class Orders extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'orders';
}
public function rules()
{
return [
[['user', 'header', 'text', 'size', 'available'], 'required'],
[['user', 'size', 'available'], 'integer'],
[['header'], 'string', 'max' => 60],
[['text'], 'string', 'max' => 255]
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'user' => 'User',
'header' => 'Header',
'text' => 'Text',
'size' => 'Size',
'available' => 'Available',
];
}
}
Answer the question
In order to leave comments, you need to log in
1. Your TakeOrder action is responsible for accepting an order, and the "accept" link leads to the Index, now it's like this:
<?= Html::a('Принять', ['orders/index','value' => ($orders->id)], ['class'=>'btn btn-primary']) ;?>
public function actionTakeOrder($value) {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question