我必须在我的视图中使用user_id获取user_name。 以下是我的模式:
//型号
class main_model extends CI_Model {
public function getOrders() {
$this->db->where( 'real', 1 );
$this->db->ORDER_BY( 'id', 'DESC' );
$query = $this->db->get('orders',10);
return $query->result();
}
public function getUsername($id) {
$this->db->where( 'id', $id );
$query = $this->db->get('users');
return $query->result();
}
//我的控制器如下:
public function index() {
$this->load->model('main_model');
$data['orders'] = $this->main_model->getOrders();
$this->load->view('index', $data);
}
//以下是我的查看文件。
<div class="container-fluid">
<div class="row">
<table class="table table-responsive" border="3">
<tr>
<th>OID</th>
<th>user_id</th>
<th>Username</th>
</tr>
<?php foreach($orders as $order): ?>
<tr>
<td> <?= $order->id;?> </td>
<td> <?= $order->uid;?> </td>
<td> ? </td> <!-- Want to get user name using uid. -->
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
我的订单表中有user_id,用户名在我的用户表上,因此我必须从那里获取它。任何人都可以帮忙。
答案 0 :(得分:1)
首先,从getOrders()
电话中获取您的用户ID,然后循环执行您的订单以获取您的用户。然后从模型中的getUsername()
函数返回一个用户对象,因此在控制器中调用它并存储变量:
$orders = $this->main_model->getOrders();
foreach($orders as $order) {
$user = $this->main_model->getUsername($uid);
$order->username = $user->username;
}
$data['orders'] = $orders;
现在用户名在您的订单数组中,只需添加<td> <?= $order->username;?> </td>
答案 1 :(得分:1)
您可以通过单个查询获取用户名 对main_model中的getOrders()函数进行简单的更改
<tr>
<td> <?= $order->id;?> </td>
<td> <?= $order->uid;?> </td>
<td> <?= $order->user_name;?></td>
</tr>
import {NgModule, Component} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {MomentModule} from 'angular2-moment/moment.module';
@Component({
selector: 'timeago-demo'
template: `
{{moment}}
`
})
class TimeagoDemoComponent {
moment('2015').weekdayCalc([1,2]); //104
moment('2015').isoWeekdayCalc([1,2]); //104
}
@NgModule({
imports: [
BrowserModule,
MomentModule
],
declarations: [ TimeagoDemoComponent ]
bootstrap: [ TimeagoDemoComponent ]
})
class TimeagoDemoModule {}
platformBrowserDynamic().bootstrapModule(TimeagoDemoModule);
答案 2 :(得分:1)
也许是这样的:
的 MODEL 强>
public function getUsername($id) {
$this->db->where( 'id', $id );
$query = $this->db->get('users');
return $query->row();
}
查看强>
<?php foreach($orders as $order):
$username = $this->main_model->getUsername($order->uid);
?>
<tr>
<td> <?= $order->id; ?> </td>
<td> <?= $order->uid; ?> </td>
<td> <?= $username->user_name; ?> </td>
</tr>
<?php endforeach; ?>