简体   繁体   English

将列从一个表连接到另一个表CodeIgniter中的另一列

[英]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.

相关问题 从其他表连接一列并回显它 - Join One Column From other Table and Echo it 将一列联接到另一张表 - Join in one column to another table Codeigniter-MySql:将一个表的三个列值与另一个表的ID连接起来 - Codeigniter - MySql : Join three column values of one table with the ID of another table 如何在PHP Codeigniter中从一个表中获取特定列,并从另一表中获取所有其他数据 - How to get specific column from one table and all other data from other table in php codeigniter 连接同一数据库中另一表的一个完整表和一列,并使用 PHP 显示 - Join one complete table and one column from another table inside the same database and display with PHP 是否可以通过使用外键使用连接子句将两个表中的其他列名显示到一个表中 - Is it possible to display other column names from two table to one table by using join clause by using foreign key PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组 - PHP Codeigniter MySQL query - join 4 tables together, with 3 tables using group by one column from each table Codeigniter - 将列的值传递给另一个表的列 - Codeigniter - Passing the value of column to the column of another table 如何根据另一个表列从一个表中选择一列? - how to select a column from one table according to another table column? SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM