如何根据值扩展 implode() 以获取单独的字符串

发布于 2024-10-14 18:22:17 字数 913 浏览 3 评论 0原文

在我页面上的不同位置,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2024-10-21 18:22:17
$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]; }
foreach($roles as $role => $values) {
  echo "Role ".$role.": " implode(" ; ", $values);
}

不确定是不是你的意思?

$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]; }
foreach($roles as $role => $values) {
  echo "Role ".$role.": " implode(" ; ", $values);
}

Not sure if it is what you mean?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文