2016-01-08 23 views
1

我正在使用Yii2-advanced模板。我想從我的'index.php'中取'img_id'中的siteController的功能。Yii2:從控制器的html中獲取圖像ID

我在 '的index.php' 代碼如下:

<?php 
    $m = $dataProvider->getModels(); 
    foreach ($m as $dp) { 
     echo '<a id ="img_id" class="" href="http://localhost/efa-webv1/frontend/web/index.php?r=site/subcat&id='.$dp['bmc_id'].'" method="post">'; 
     echo "<img src = '"."http://localhost/efa-webv1/backend/web/".$dp['bmc_image']."' />"; 
     echo '<center><font color = "white">'.$dp['bmc_name'].'<font/></center>'; 
     echo '</a>'; 
    } 
?> 

而我試圖把它在我的siteController爲:

public function actionSubcat() { 

    $searchModel = new BusinessMainCategoriesSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
    $dataProvider->pagination->pageSize = $dataProvider->getTotalCount(); 
    $query = new \yii\db\Query; 
    $query->select('*')->from('business_sub_categories')->where(['bmc_id' => $_POST["img_id"]]); //trying to get img_id using this 
    $query->createCommand(); 

    $dataProvider2 = new ActiveDataProvider([ 
     'query' => $query, 
     'pagination' => false, 
    ]); 
    return $this->render('subcat', [ 
     'dataProvider' => $dataProvider, 'dataProvider2' => $dataProvider2]); 
    } 

但如果我用「$ _ POST [「img_id」]',它表示未定義索引「img_id」。

所以,請幫助我讓我理解它。現在

If I used Yii2 ancher tag, I get unexpected result as -

+0

使用[yii2錨標籤](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#a%28%29-detail),它會比這更容易。 –

+0

首先檢查你會得到img_id in $ _POST – jilesh

+0

檢查它在函數print_r($ _ POST)的啓動中使用; – msvairam

回答

0

,我修改的index.php是:

<?php 
        $m = $dataProvider->getModels(); 
        foreach ($m as $dp) { 
         echo '<a id ="img_id" class="" href="http://localhost/efa-webv1/frontend/web/index.php?r=site/subcat&img_id='.$dp['bmc_id'].'" method="get">'; 
         echo "<img src = '"."http://localhost/efa-webv1/backend/web/".$dp['bmc_image']."' />"; 
         echo '<center><font color = "white">'.$dp['bmc_name'].'<font/></center>'; 
         echo '</a>'; 
        } ?> 

我正在& ID而不是& img_id(以URL)中稱爲控制器,這是錯誤的。現在我的控制器代碼是這樣的 -

$query->select('*')->from('business_sub_categories')->where(['bmc_id' => $_GET['img_id']]); 

請注意,當我使用POST方法從url傳遞值時,它不起作用。所以,我用GET方法&搞定了。

無論如何,謝謝大家!

相關問題