(Yii2) how to pass value from table to an HTML button
I'm new to Yii2 and currently I'm working on the admin section in which admin could update a typical webpage, in this case, the "About us" page. The application will check if there's already an existing row in the "About" table. If so, an "Update" button will be shown right in the view's index page as only one content is allowed in the "About us" section. Else, a "create" button will be shown. The update in the gridview works well but I wonder how to pass the "id" value in the table into the update button because I keep getting the error message "missing required parameters id". I've spent a whole day but stil stuck. Thank you in advance for your kind help.
This is how my Index looks like :
<?php
if(!$dataProvider)
echo Html::a('Create', ['create'], ['class' => 'btn btn-success']) ;
else
echo Html::a('Update-about', ['update'], ['class' => 'btn btn-success']);
?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yiigridSerialColumn'],
'Tite',
'Content:ntext',
'Date',
'ImageID',
['class' => 'yiigridActionColumn'],
],
]); ?>
And this is the controller:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
public function actionUpdate($id)
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save())
return $this->redirect(['view', 'id' => $model->ID]);
else
return $this->render('update', [
'model' => $model,
]);
yii2
add a comment |
I'm new to Yii2 and currently I'm working on the admin section in which admin could update a typical webpage, in this case, the "About us" page. The application will check if there's already an existing row in the "About" table. If so, an "Update" button will be shown right in the view's index page as only one content is allowed in the "About us" section. Else, a "create" button will be shown. The update in the gridview works well but I wonder how to pass the "id" value in the table into the update button because I keep getting the error message "missing required parameters id". I've spent a whole day but stil stuck. Thank you in advance for your kind help.
This is how my Index looks like :
<?php
if(!$dataProvider)
echo Html::a('Create', ['create'], ['class' => 'btn btn-success']) ;
else
echo Html::a('Update-about', ['update'], ['class' => 'btn btn-success']);
?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yiigridSerialColumn'],
'Tite',
'Content:ntext',
'Date',
'ImageID',
['class' => 'yiigridActionColumn'],
],
]); ?>
And this is the controller:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
public function actionUpdate($id)
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save())
return $this->redirect(['view', 'id' => $model->ID]);
else
return $this->render('update', [
'model' => $model,
]);
yii2
Rfere Yii2 Html Hyperlinks
– vishuB
Nov 14 '18 at 8:05
add a comment |
I'm new to Yii2 and currently I'm working on the admin section in which admin could update a typical webpage, in this case, the "About us" page. The application will check if there's already an existing row in the "About" table. If so, an "Update" button will be shown right in the view's index page as only one content is allowed in the "About us" section. Else, a "create" button will be shown. The update in the gridview works well but I wonder how to pass the "id" value in the table into the update button because I keep getting the error message "missing required parameters id". I've spent a whole day but stil stuck. Thank you in advance for your kind help.
This is how my Index looks like :
<?php
if(!$dataProvider)
echo Html::a('Create', ['create'], ['class' => 'btn btn-success']) ;
else
echo Html::a('Update-about', ['update'], ['class' => 'btn btn-success']);
?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yiigridSerialColumn'],
'Tite',
'Content:ntext',
'Date',
'ImageID',
['class' => 'yiigridActionColumn'],
],
]); ?>
And this is the controller:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
public function actionUpdate($id)
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save())
return $this->redirect(['view', 'id' => $model->ID]);
else
return $this->render('update', [
'model' => $model,
]);
yii2
I'm new to Yii2 and currently I'm working on the admin section in which admin could update a typical webpage, in this case, the "About us" page. The application will check if there's already an existing row in the "About" table. If so, an "Update" button will be shown right in the view's index page as only one content is allowed in the "About us" section. Else, a "create" button will be shown. The update in the gridview works well but I wonder how to pass the "id" value in the table into the update button because I keep getting the error message "missing required parameters id". I've spent a whole day but stil stuck. Thank you in advance for your kind help.
This is how my Index looks like :
<?php
if(!$dataProvider)
echo Html::a('Create', ['create'], ['class' => 'btn btn-success']) ;
else
echo Html::a('Update-about', ['update'], ['class' => 'btn btn-success']);
?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yiigridSerialColumn'],
'Tite',
'Content:ntext',
'Date',
'ImageID',
['class' => 'yiigridActionColumn'],
],
]); ?>
And this is the controller:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
public function actionUpdate($id)
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save())
return $this->redirect(['view', 'id' => $model->ID]);
else
return $this->render('update', [
'model' => $model,
]);
yii2
yii2
asked Nov 14 '18 at 8:00
codejunkiecodejunkie
31
31
Rfere Yii2 Html Hyperlinks
– vishuB
Nov 14 '18 at 8:05
add a comment |
Rfere Yii2 Html Hyperlinks
– vishuB
Nov 14 '18 at 8:05
Rfere Yii2 Html Hyperlinks
– vishuB
Nov 14 '18 at 8:05
Rfere Yii2 Html Hyperlinks
– vishuB
Nov 14 '18 at 8:05
add a comment |
1 Answer
1
active
oldest
votes
Link should be
Html::a('Update-about', ['update', 'id' => $id], ['class' => 'btn btn-success']);
And you should pass ID of the row to index
view:
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $id,
]);
So the main question is - what row of table About
you want to change by update action? If this is the first available record then:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$about = About::find()->one(); // get first record found
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $about->id, // pass record id to view
]);
Or find the row you need by passing some condition to where
method: About::find()->where(...)->one()
;
Thanks for your answer! Yes, I want to change the first available record. It saves my life to know there's a way to get a row by using $about = About::find()->one();
– codejunkie
Nov 14 '18 at 13:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295451%2fyii2-how-to-pass-value-from-table-to-an-html-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Link should be
Html::a('Update-about', ['update', 'id' => $id], ['class' => 'btn btn-success']);
And you should pass ID of the row to index
view:
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $id,
]);
So the main question is - what row of table About
you want to change by update action? If this is the first available record then:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$about = About::find()->one(); // get first record found
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $about->id, // pass record id to view
]);
Or find the row you need by passing some condition to where
method: About::find()->where(...)->one()
;
Thanks for your answer! Yes, I want to change the first available record. It saves my life to know there's a way to get a row by using $about = About::find()->one();
– codejunkie
Nov 14 '18 at 13:11
add a comment |
Link should be
Html::a('Update-about', ['update', 'id' => $id], ['class' => 'btn btn-success']);
And you should pass ID of the row to index
view:
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $id,
]);
So the main question is - what row of table About
you want to change by update action? If this is the first available record then:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$about = About::find()->one(); // get first record found
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $about->id, // pass record id to view
]);
Or find the row you need by passing some condition to where
method: About::find()->where(...)->one()
;
Thanks for your answer! Yes, I want to change the first available record. It saves my life to know there's a way to get a row by using $about = About::find()->one();
– codejunkie
Nov 14 '18 at 13:11
add a comment |
Link should be
Html::a('Update-about', ['update', 'id' => $id], ['class' => 'btn btn-success']);
And you should pass ID of the row to index
view:
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $id,
]);
So the main question is - what row of table About
you want to change by update action? If this is the first available record then:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$about = About::find()->one(); // get first record found
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $about->id, // pass record id to view
]);
Or find the row you need by passing some condition to where
method: About::find()->where(...)->one()
;
Link should be
Html::a('Update-about', ['update', 'id' => $id], ['class' => 'btn btn-success']);
And you should pass ID of the row to index
view:
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $id,
]);
So the main question is - what row of table About
you want to change by update action? If this is the first available record then:
public function actionIndex()
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$about = About::find()->one(); // get first record found
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $about->id, // pass record id to view
]);
Or find the row you need by passing some condition to where
method: About::find()->where(...)->one()
;
edited Nov 16 '18 at 8:15
answered Nov 14 '18 at 9:23
Anton RybalkoAnton Rybalko
719816
719816
Thanks for your answer! Yes, I want to change the first available record. It saves my life to know there's a way to get a row by using $about = About::find()->one();
– codejunkie
Nov 14 '18 at 13:11
add a comment |
Thanks for your answer! Yes, I want to change the first available record. It saves my life to know there's a way to get a row by using $about = About::find()->one();
– codejunkie
Nov 14 '18 at 13:11
Thanks for your answer! Yes, I want to change the first available record. It saves my life to know there's a way to get a row by using $about = About::find()->one();
– codejunkie
Nov 14 '18 at 13:11
Thanks for your answer! Yes, I want to change the first available record. It saves my life to know there's a way to get a row by using $about = About::find()->one();
– codejunkie
Nov 14 '18 at 13:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295451%2fyii2-how-to-pass-value-from-table-to-an-html-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Rfere Yii2 Html Hyperlinks
– vishuB
Nov 14 '18 at 8:05