-1

In my database field, authors of a particular item are listed by ID like this:

AuthorID: 1,4,7

Since there is no space between comma-separated values in the DB, my page would display this:- Jeff Smith,Peter Thompson,Euphegenia Doubtfire

How do I edit my code to show this instead?:- Jeff Smith, Peter Thompson, Euphegenia Doubtfire

For some reason I cannot crack this, many thanks for your help.

$authors = explode(",", $value );

$value = '';
foreach ($authors as $au){
   $elements = ((strpos($au, ",") === false) ? array(0 => $au) : (explode(",", $au)));
   $index = 0;
   foreach ($elements as $element) {$value .= (($index++ === 0) ? '' : ',') . '<a href="/site/tracks_list.php?q=(AuthorID~contains~'. $element . ')">'. $element . '</a>';
   }
}
  • `implode(", ", $elements);` ? – nice_dev Apr 21 '22 at 15:26
  • 1
    I provided a possible solution below (assuming I understood correctly what's in your `$value` string, which you didn't show us)... but this problem seems to be rooted in a database design error - please read [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – ADyson Apr 21 '22 at 15:29
  • Oh I know it is horrible to store like that, but I am trying to keep my webpage very very simple. – x87-Learner Apr 22 '22 at 10:47
  • Well as you can see from that link, by keeping it "simple" you've actually made it a lot more complicated. You've created a lot more problems than you've solved. There's a reason the standard convention and design pattern has persisted for decades... – ADyson Apr 22 '22 at 10:48
  • Anyway can you look at the answer below and see if it's relevant. If not, please [edit] your question to clarify what's in the `$value` variable, then we can work on this more accurately. – ADyson Apr 22 '22 at 10:50
  • Also your stated desired output is simply a list of comma-separate names, but your PHP seems to generate a list of hyperlinks, which is a rather different thing. So you perhaps need to clarify the precise output you're looking for too. – ADyson Apr 22 '22 at 10:51
  • I am using software to create the webpage. When I create a box as a lookup field with multiple selections possible, it just combines them into a comma separated string. $value is basically just the array as it is stored in the database. Saying $value = xxxxx allows you to format the output. – x87-Learner Apr 22 '22 at 10:53
  • That doesn't answer my query. Edit the question to include an actual sample of some real data so that we are closer to a [mre] of your issue. – ADyson Apr 22 '22 at 10:56
  • Okay, please wait for a short time. I am testing your solution below first. – x87-Learner Apr 22 '22 at 10:57

1 Answers1

-1

You can just replace the comma with a comma and a space:

$value = "Jeff Smith,Peter Thompson,Euphegenia Doubtfire";
echo str_replace(",", ", ", $value);
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    Actually thanks, this worked in the end. It took me a while to find the right place to copy and paste it. Thanks for your help! – x87-Learner Apr 22 '22 at 11:18