I’m trying to display an array of href links as a string with the results separated by commas so as to display:-
I Am The Walrus, Hello, Goodbye, The Fool On The Hill, Magical Mystery Tour, Lady Madonna
(NB: Some results will contain commas (Hello, Goodbye) so I’ll need to use the 'implode' function rather than 'preg_replace').
The links are comprised of three different values from each result, i.e. 'id' and 'url' for the URL and 'name' for the name to be displayed.
My current code:
Index:-
Display page:-PHP Code:<?php
while ($row = mysqli_fetch_array($result)) //Creates array.
{
$songs[] = array('id' => $row['id'], 'url' => $row['url'], 'name' => $row['name']);
}
?>
Outputs:-PHP Code:<?php foreach ($songs as $song)
{
$song = '<a href="/song/' . $song['id'] . '/' . $song['url'] . '">' . $song['name'] . '</a>';
}
echo implode(", ", $songs);
?>
Notice is repeated x 5 times (i.e. however many results there are)Notice: Array to string conversion in C:\...
Followed by the below:-
Again, repeated according to number of results.Array, Array, Array, Array, Array
The method was taken from this problem: PHP Separate comma list with links, but it only requires the one value to create the link; mine needs three.
I feel all I require to make this work is a minor tweak but I just can’t figure it out – any suggestions?



Reply With Quote

Bookmarks