问题/问题:我正在使用CodeIgniter来构建活动日历,并且我已经包含了一个共享选项。此选项适用于基本级别,但仅在<ul>
中显示用户的ID(主键)。这不太理想,我想在<ul>
中显示用户的名字和姓氏。我认为创建一个关联数组会起作用,但我收到了时髦的结果。当页面加载时,名字和姓氏将作为“数组阵列”回显,当您选择“阵列阵列”链接时,URL ID将显示为“数组”。我想知道我的逻辑有什么问题。
视图中生成的时髦链接:
Array Array
链接到“数组阵列”的虚拟网址:
http://example.com/user/shared/view/Array
修改后的控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shared extends Common_Auth_Controller {
private $end_user;
public function __construct()
{
parent::__construct();
$this->end_user = $this->ion_auth->user()->row();
$data['end_user'] = $this->end_user;
$this->load->vars($data);
$this->load->model('events_model', 'events');
}
public function index()
{
$title['title'] = 'Shared';
$this->load->model('shared_model','shared');
$data['sharers'][] = array(
'shared_owner_id' => $this->shared->get($this->end_user->id),
'owner_first_name' => $this->shared->get($this->end_user->first_name),
'owner_last_name' => $this->shared->get($this->end_user->last_name),
);
$this->load->view('public/head_view', $title);
$this->load->view('user/header_view');
$this->load->view('user/shared_view', $data);
$this->load->view('user/footer_view');
}
修改后的视图:
<div class="hide-on-phones">
<ul>
<?php foreach($sharers as $key => $value): ?>
<li><a href="<?php echo base_url('user/shared/view/'.$value['shared_owner_id']) ?>"><?php echo $value['owner_first_name']." ".$value['owner_last_name'] ?></a></li>
<?php endforeach; ?>
</ul>
</div>
模型:
class Shared_model extends crud_model {
public function __construct()
{
parent::__construct();
$this->pk = 'id';
$this->table_name = 'shared';
}
public function get($shared_to_user_id)
{
$this->db->where('shared_to_id', $shared_to_user_id);
$ids = parent::get_all();
$users = array();
foreach ($ids as $id)
{
$users[] = $id->owner_id;
}
return $users;
}
}
非常感谢您的帮助,如果有更多可能需要的信息,请告诉我。下面是原始视图和控制器,但不是更好。
原始控制器:
public function index()
{
$title['title'] = 'Shared';
$this->load->model('shared_model','shared');
$data['sharers'] = $this->shared->get($this->end_user->id);
$this->load->view('user/head_view', $title);
$this->load->view('user/header_view');
$this->load->view('user/navigation_view');
$this->load->view('user/shared_view', $data);
$this->load->view('user/footer_view');
}
原始视图:
<?php foreach($sharers as $s): ?>
<li><a href="<?php echo base_url('user/shared/mobile/'.$s) ?>"><?php echo $s ?></a></li>
<?php endforeach; ?>
免责声明:我是网络开发的新手,而且我很熟悉关联数组(显然)。
答案 0 :(得分:2)
您的$data['sharers']
数组没有$values
数组。因此,您在$value[]'s
中调用foreach
的方式无效。此时您没有理由调用foreach
。
<div class="hide-on-phones">
<ul>
<li><a href="<?=base_url('user/shared/view/'.$sharers['shared_pk_id'])?>">
<?=$sharers['first_name'] . ' ' . $sharers['last_name']?>
</a>
</li>
</ul>
</div>
我希望您稍后会用数据填充数组,在这种情况下,您可以将其填充为
$data['sharers'][] = array(
'shared_pk' => $this->shared->get($this->end_user->id),
'first_name' => $this->events->get($this->end_user->first_name),
'last_name' => $this->events->get($this->end_user->last_name)
);
反过来可以使用
循环<div class="hide-on-phones">
<ul>
<? foreach($sharers as $sharer): ?>
<li>
<a href="<?=base_url('user/shared/view/' . $sharer['shared_pk'])?>">
<?=$sharer['first_name'] . ' ' . $sharer['last_name']?>
</a>
</li>
<? endforeach; ?>
</ul>
</div>
答案 1 :(得分:1)
我已经弄清楚了。我需要在我的模型中创建一个关联数组;不是我的控制者。感谢大家的帮助!
<强>模型强>:
class Shares_model extends crud_model {
public function __construct()
{
parent::__construct();
$this->pk = 'id';
$this->table_name = 'shares';
}
public function get($shared_to_user_id)
{
$this->db->where('shared_to_id', $shared_to_user_id);
$ids = parent::get_all();
$users = array();
foreach ($ids as $id)
{
$users[$id->owner_id]['owner_id'] = $id->owner_id;
$users[$id->owner_id]['owner_first_name'] = $id->owner_first_name;
$users[$id->owner_id]['owner_last_name'] = $id->owner_last_name;
}
return $users;
}
}
查看强>:
<ul>
<?php foreach($sharers as $s): ?>
<li><a href="<?php echo base_url('user/shared/view/'.$s['owner_id']) ?>"><?php echo $s['owner_first_name']." ".$s['owner_last_name'] ?></a></li>
<?php endforeach; ?>
</ul>
<强>控制器强>:
public function index()
{
$title['title'] = 'Shared';
$this->load->model('shares_model','shares');
$data['sharers'] = $this->shares->get($this->end_user->id);
$this->load->view('public/head_view', $title);
$this->load->view('user/header_view');
$this->load->view('user/shared_view', $data);
$this->load->view('user/footer_view');
}
答案 2 :(得分:0)
如果我们使用你的$data['sharers']
数组并输入php的值,那么我们会得到这样的结果:
$data['sharers'] = array(
0 => array(
'id' => 1234,
'first_name' => 'John',
'last_name' => 'Doe'
),
1 => array(
'id' => 1235,
'first_name' => 'Jane',
'last_name' => 'Doe'
)
);
因此,当您在视图中循环使用它时,您需要正确访问它们:
foreach($sharers as $key => $value):
// at this point, in the first iteration of the loop
// $key is 'id', or 'first_name', or 'last_name'
// and $value is 1234, or 'John', or 'Doe'
endforeach;
因此,要更轻松地遍历共享者,您只需要更新一下foreach。此时,请将您的视图更改为:
<div class="hide-on-phones">
<ul>
<?php foreach($sharers as $sharer): ?>
<li>
<a href="<?php echo base_url('user/shared/view/'.$sharer['shared_pk_id']) ?>">
<?php echo $sharer['first_name']. " " .$sharer['last_name'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>