如何根据值扩展 implode() 以获取单独的字符串
在我页面上的不同位置,我在 while 循环中使用了 mysql_fetch_array() 将值复制到新的 PHP 数组中。从那里我使用 implode() 将值写入字符串中以显示给用户。当我只处理一列或两列(可以合并到一行上进行显示)时,这对我来说效果很好。
现在我正在处理一个包含两部分数据的数组:角色和名称。我想根据角色的值在不同的行上显示输出字符串。这是一个图书馆数据库,因此我想要与一行中的作者角色相关的所有名称,另一行中与出版商角色相关的名称,依此类推。
$sql = mysql_query("select role, party_name from vw_roles where publication_id =
$id order by role", $link);
while($role = mysql_fetch_array($sql)) {
$roles[] = $role[role] . ": ". $role[party_name]; }
$the_roles = implode(" ; ", $roles);
输出: 作者:奥利金;书商:古列尔穆斯·莫登;文字评论员:Tarin, Jean;书面文字评论员:Hoeschel, David;编辑:斯宾塞,威廉;印刷者:琼. Hayes
但随后我需要尝试将字符串拆分为各个子字符串,以便在标题 Author 下的输出表中仅输出 Origen。遍历数组 $roles[] 并将名称分隔到该角色的新数组中是否更好?那么
if ($roles[role] == "Author")
$author[] = $roles[party_name];
每个角色都有某种形式的迭代吗?但随后我需要指定角色的每个可能值,以防它包含在该特定记录中,这会使代码变得很长。
肯定有更好的方法来继续这里,只是我还没有找到它。有人有什么想法吗?
In various places on my page I've used mysql_fetch_array() within a while loop to copy values into a new PHP array. From there I've used implode() to write the values into a string for display to the user. This has worked fine for me when I've only been dealing with one column or with two columns which can be combined onto one line for display.
Now I'm dealing with an array with two pieces of data, role and name. I want to display the output strings on different lines dependent on the value of role. It's a library database so I want all the names which relate the role of Author on one line, the names for the role of Publisher on another and so on.
$sql = mysql_query("select role, party_name from vw_roles where publication_id =
$id order by role", $link);
while($role = mysql_fetch_array($sql)) {
$roles[] = $role[role] . ": ". $role[party_name]; }
$the_roles = implode(" ; ", $roles);
Outputs:
Author: Origen ; Bookseller: Gulielmus Morden ; Commentator for written text: Tarin, Jean ; Commentator for written text: Hoeschel, David ; Editor: Spencer, William ; Printer: Joan. Hayes
But then I'd need to try and split up the string into the various substrings, so that in my output table under the heading Author only Origen would be output. Is it better to go through the array $roles[] and separate the names into a new array for that role? So
if ($roles[role] == "Author")
$author[] = $roles[party_name];
with some form of iteration for each role? But then I'd need to specify every possible value for role in case it's been included for this specific record, which would make the code very long.
There's bound to be a better way to proceed here, I just haven't been able to find it yet. Anyone any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定是不是你的意思?
Not sure if it is what you mean?