[英]Join column from one table to another column in other table CodeIgniter
I've got 2 Tables that I want some of the columns to auto update when the other one gets data from user. 我有2个表,我希望一些列在另一个从用户获取数据时自动更新。
Here is my scenario: User registers and his details go to "users" Table. 这是我的场景:用户注册和他的详细信息转到“用户”表。 The registered user can now make a post.
注册用户现在可以发帖了。 When he makes a post his post goes to "posts" table.
当他发帖时他的帖子转到“帖子”表。
Now I want to display the users "username" when he/she makes a post. 现在我想在他/她发帖时显示用户“用户名”。
So, conclusion is that I want the column "username" from table "users" to automatically synchronize with column "username" from table "posts". 因此,结论是我希望表“users”中的“username”列自动与表“posts”中的列“username”同步。 So that once a user makes a post, it will see that a specific user made a post.
因此,一旦用户发帖,就会看到特定用户发帖。
Here's how i will implement it 这是我将如何实现它
<?php foreach ($posts as $post) : ?>
<div class="row">
<div class="col-md-3">
<img class="post-thumb img-fluid" src="<?php echo site_url();
?>assets/images/posts/<?php echo $post['post_image']; ?>">
</div>
<div class="col-md-9">
<h3><?php echo $post['title'];?></h3>
<small class="post-date">Posted on: <?php echo
$post['created_at']; ?>
in <strong><?php echo $post['category_name']?></strong> by
<strong><?php echo $post['username']?></strong></small><br>
<?php echo word_limiter($post['body'], 50); ?>
<br><br>
<p><a class="btn btn-info" href="<?php echo
site_url('/posts/'.$post['slug']);?>"
>Read More</a></p>
</div>
</div>
<?php endforeach; ?>
Here is a function I tried, but it doesn't update my "posts" table column "username" when I make a post. 这是我尝试过的一个功能,但是当我发帖时,它不会更新我的“帖子”表格列“用户名”。
public function get_posts($slug = FALSE){
if ($slug === FALSE){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id =
posts.category_id');
$this->db->join('users', 'users.username = posts.username');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
Here are the DB tables 这是数据库表
CREATE TABLE `posts` (
`id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`username` varchar(255) NOT NULL,
`posted_by` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL,
`body` text NOT NULL,
`post_image` varchar(255) NOT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`register_date` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
There is no error, just no result. 没有错误,只是没有结果。
Any advice or help will be appreciated. 任何建议或帮助将不胜感激。
EDIT: 编辑:
I've managed to make a "get_username" function. 我设法制作了“get_username”函数。 But I'm not sure why its not working and how to create a loop for it to loop through posts that have been made.
但我不确定为什么它不起作用,以及如何创建一个循环来循环通过已经发布的帖子。 Please see code below
请参阅下面的代码
public function get_username(){
$username = $this->db->query("
SELECT u.username AS username
FROM users u
LEFT JOIN posts p
ON u.id = p.user_id
WHERE p.id = ?"
, array('id')
)->row_array();
return $username['username'];
}
I get error saying: 我得到错误说:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: username
Filename: posts/index.php
Line Number: 19
Your get_username()
method takes no parameters - what if you wanted to get id
of 5? 你的
get_username()
方法没有参数 - 如果你想得到5的id
怎么办? or 20
? 还是
20
? Currently you have no ability to specify what id
to query. 目前,你有没有指定什么能力
id
查询。 The current query, results in: 当前查询,结果如下:
LEFT JOIN posts p ON u.id = p.user_id WHERE p.id = 'id'
As it's passing a string literally called id
, whereas what you actually want, is to pass a parameter with the associated ID to query. 因为它传递一个字面上称为
id
的字符串,而你实际想要的是传递一个带有相关ID的参数来进行查询。
/**
*
**/
public function get_username( int $userId )
{
$username = $this->db->query("
SELECT u.username AS username
FROM users u
LEFT JOIN posts p
ON u.id = p.user_id
WHERE p.id = ?"
, array( $userId )
)->row_array();
return $username['username'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.