I'm trying to get 2 fields from 2 rows of a table and combine them to be a string. For example:
我试图从一个表的2行中获取2个字段并将它们组合成一个字符串。例如:
username id text
row 1 Jason 1 ......
row 2 Lass 2 ......
Basically I would need to get $usernames=Jason, Lass
.
基本上我需要得到$ usernames = Jason,Lass。
What I've tried:
我尝试过的:
$username_raw=$this->db->select('username')->get('user', 2);
foreach ($username_raw->result() as $row):
echo $username
endforeach;
But the last 3 rows turned out not to be working and i'm kinda stuck here. Any advice is appreciated.
但最后3排结果不起作用,我有点卡在这里。任何建议表示赞赏。
3 个解决方案
#1
3
Stab in the dark tells me this is a CodeIgniter question - you should tag that so people know how your query is working. Anyway, your $username
variable is not initialized. You'll need to access it by $row->username
.
在黑暗中刺伤告诉我这是一个CodeIgniter问题 - 你应该标记这一点,以便人们知道你的查询是如何工作的。无论如何,你的$ username变量没有初始化。您需要通过$ row-> username访问它。
I suggest you add the usernames to an array, then implode()
the results:
我建议你将用户名添加到数组中,然后implode()结果:
$username_raw=$this->db->select('username')->get('user', 2);
$usernames = array();
foreach ($username_raw->result() as $row):
$usernames[] = $row->username;
endforeach;
$your_usernames_combined = implode(', ', $usernames);
echo $your_usernames_combined; // e.g. Jason, Lass
If you want the period at the end like your example, add $your_usernames_combined .= '.';
to the end.
如果你想像你的例子那样结束句号,添加$ your_usernames_combined。='。';;到最后。
#2
-1
Assuming you're using the Codeigniter framework, you need to use $row->username
instead.
假设您正在使用Codeigniter框架,则需要使用$ row-> username。
Then you need to use an array to store the usernames.
然后,您需要使用数组来存储用户名。
#3
-2
I would probably use the "sprintf" function, and the code will look like this
我可能会使用“sprintf”函数,代码看起来像这样
$username = sprintf("%s, %s",$value1,$value2);
Your question is pretty unclear, and i dont completely understand where you make calls to your MySQL database, i can only see you calling for the 2nd username
你的问题很不清楚,我不完全明白你在哪里调用MySQL数据库,我只能看到你要求第二个用户名
#1
3
Stab in the dark tells me this is a CodeIgniter question - you should tag that so people know how your query is working. Anyway, your $username
variable is not initialized. You'll need to access it by $row->username
.
在黑暗中刺伤告诉我这是一个CodeIgniter问题 - 你应该标记这一点,以便人们知道你的查询是如何工作的。无论如何,你的$ username变量没有初始化。您需要通过$ row-> username访问它。
I suggest you add the usernames to an array, then implode()
the results:
我建议你将用户名添加到数组中,然后implode()结果:
$username_raw=$this->db->select('username')->get('user', 2);
$usernames = array();
foreach ($username_raw->result() as $row):
$usernames[] = $row->username;
endforeach;
$your_usernames_combined = implode(', ', $usernames);
echo $your_usernames_combined; // e.g. Jason, Lass
If you want the period at the end like your example, add $your_usernames_combined .= '.';
to the end.
如果你想像你的例子那样结束句号,添加$ your_usernames_combined。='。';;到最后。
#2
-1
Assuming you're using the Codeigniter framework, you need to use $row->username
instead.
假设您正在使用Codeigniter框架,则需要使用$ row-> username。
Then you need to use an array to store the usernames.
然后,您需要使用数组来存储用户名。
#3
-2
I would probably use the "sprintf" function, and the code will look like this
我可能会使用“sprintf”函数,代码看起来像这样
$username = sprintf("%s, %s",$value1,$value2);
Your question is pretty unclear, and i dont completely understand where you make calls to your MySQL database, i can only see you calling for the 2nd username
你的问题很不清楚,我不完全明白你在哪里调用MySQL数据库,我只能看到你要求第二个用户名