dsa4214 2016-07-28 19:49
浏览 36
已采纳

如何使用HTML-> Link链接到另一个表

I am trying to set a link to view a user from the users table. I want this to appear in the index file of the articles template.

I have tried using:

$this->Users->Html->link($current->username, ['action' => 'view', $current->id]) ?>

and:

$this->$userstable->Html->link($current->username, ['action' => 'view' $current->id]) ?>

here is the index file:

<div class="col-lg-8">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 style="float:left;">Users</h4>
                <a href="http://localhost:8765/users/add"><Button class="btn btn-primary add-article">Add User</Button></a>
            </div>
            <table>
                <tr>
                    <th>Id</th>
                    <th>Userame</th>
                    <th>Role</th>
                    <th>Joined</th>


                </tr>

            <!-- Here's where we loop through our $articles query object, printing out article info -->

                <?php foreach ($userstable as $current): ?>
                <tr>
                    <td><?= $current->id ?></td>
                    <td>
                        <?= $this->Html->link($current->username, ['action' => 'view', $current->id]) ?>
                    </td>
                    <td>
                        <?= $current->role?>
                    </td>
                    <td>
                        <?= $current->created->format(DATE_RFC850) ?>
                    </td>
                    <td>
                        <?= $this->Form->postLink(
                            'Delete',
                            ['action' => 'delete', $current->id],
                            ['confirm' => 'Are you sure?'])
                        ?>
                        <?= $this->Html->link('Edit', ['action' => 'edit', $current->id]) ?>

                    </td>


                </tr>
                <?php endforeach; ?>

            </table>
        </div>
    </div>

here is the controller:

public function index()
{


    $this->paginate = [
    'contain'=>['Categories']
    ];
    $articles = $this->paginate($this->Articles);
    $this->set('articles', $this->Articles->find('all'));
    $this->set(compact('articles'));
    $this->set('_serialize', ['articles']);

    $article_count = $this->Articles->find('all')->count();
    $this->set('article_count',$article_count );

    /*$author_count = $this->Articles->find('all')
        ->distinct(['Articles.user_id'])
        ->count('user_id');
    $this->set('author_count',$author_count );*/

    $this->loadModel('Categories');
    $categoryCount = $this->Categories->find('all')->count();
    $this->set('categoryCount',$categoryCount );

    $this->loadModel('Users');
    $userstable = $this->Users->find('all');
    $this->set('userstable',$userstable );

    $usersCount = $this->Users->find('all')->count();
    $this->set('usersCount',$usersCount );

    $authorCount= $this->Users->find('all')->where(['role' => 'author'])->count();
    $this->set('authorCount',$authorCount );

    $adminCount= $this->Users->find('all')->where(['role' => 'admin'])->count();
    $this->set('adminCount',$adminCount );

    $subCount= $this->Users->find('all')->where(['role' => 'admin'])->count();
    $this->set('subCount',$subCount );

    $this->loadModel('Views');
    $views = $this->Views;

    $query = $views->find('all')->limit(7)->order(['date' => 'DESC']);;
    $results = $query->all();
    $data = $results->toArray();
    $results = $query->toArray();

    $result1 = $results[6]['amount'];
    $this->set('result1',$result1 );

    $result2 = $results[5]['amount'];
    $this->set('result2',$result2 );

    $result3 = $results[4]['amount'];
    $this->set('result3',$result3 );

    $result4 = $results[3]['amount'];
    $this->set('result4',$result4 );

    $result5 = $results[2]['amount'];
    $this->set('result5',$result5 );

    $result6 = $results[1]['amount'];
    $this->set('result6',$result6 );

    $result7 = $results[0]['amount'];
    $this->set('result7',$result7 );

    $date1 = $results[6]['date'];
    $this->set('date1',$date1 );

    $date2 = $results[5]['date'];
    $this->set('date2',$date2 );

    $date3 = $results[4]['date'];
    $this->set('date3',$date3 );

    $date4 = $results[3]['date'];
    $this->set('date4',$date4 );

    $date5 = $results[2]['date'];
    $this->set('date5',$date5 );

    $date6 = $results[1]['date'];
    $this->set('date6',$date6 );

    $date7 = $results[0]['date'];
    $this->set('date7',$date7 );




}

Any help you can give would be appreciated

  • 写回答

1条回答 默认 最新

  • doulianglou0898 2016-07-29 05:44
    关注

    simply:

    $this->Html->link($current->username, [
        'controller' => 'Users',
        'action' => 'view', 
        $current->id
    ]);
    

    http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-links

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常