(Yii2) how to pass value from table to an HTML button










0















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,

]);











share|improve this question






















  • Rfere Yii2 Html Hyperlinks

    – vishuB
    Nov 14 '18 at 8:05















0















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,

]);











share|improve this question






















  • Rfere Yii2 Html Hyperlinks

    – vishuB
    Nov 14 '18 at 8:05













0












0








0








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,

]);











share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 8:00









codejunkiecodejunkie

31




31












  • 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





Rfere Yii2 Html Hyperlinks

– vishuB
Nov 14 '18 at 8:05












1 Answer
1






active

oldest

votes


















0














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();






share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









0














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();






share|improve this answer

























  • 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















0














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();






share|improve this answer

























  • 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













0












0








0







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();






share|improve this answer















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();







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3