The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
PHP exploding data fields in template
I have a database field called "authors" which includes comma delineated lists of authors. The db looks like this, basically:
title - Big book of Elephants authors - Bob Shakespeare, Arnold Willis, Bill Dickins etc. The authors display on the product page. I also have a working search of the author field using a LIKE %string% query. So all that works just fine. (I understand I'm better off with a separate authors table, but for now that's not feasible). The hard part comes next: On the product page, I want each author to be a link to a search URL to find more by that author. So it'll be products.php?authors=Shakespeare. The URLs work fine already. I understand the EXPLODE function for PHP is the one I need. However, my limited coding is really based on finding snippets on the web and copying them, rather than fully understanding them. The function doesn't work in a template, so it has to be in the PHP file. The only examples I can find of this include the output echoed directly in the PHP file. That's not what I need - it need to pass it to the template somehow and output as a comma delineated list where each item has its own hyperlinked URL. I haven't the foggiest how I'd do that. I've seen mentions on here that others have managed to do it, but not how they did it. I'm sure it's simple if you know what you're doing, but I don't. So. How do I take a database field of comma separated items and output it into my existing template as a list of comma separated items each with its own hyperlink? Thanks for any help in advance, and apologies for the length of the post. I wanted it to be clear! |
#2
|
||||
|
||||
What template are you trying to use?
|
#3
|
|||
|
|||
There are 2 options:
1) Create an array in PHP and assign it to a template variable, then loop through the authors in the template. 2) Loop through the array in PHP and create the HTML in the PHP script, then assign it to a template variable and just use ir directly in the template. Example for the second option: PHP Code:
|
#4
|
|||
|
|||
Thanks! Where you have the names there, can I replace that with $product['authors']? That was just an example.
|
#5
|
|||
|
|||
Yes, so it'll be:
PHP Code:
|
#6
|
|||
|
|||
Good answer Dave, as usual. But yeah, I might have gone with exploding on ',' and then using trim(), just in case the spacing isn't always right. Also, I think I'd use urlencode() on that first $value.
|
#7
|
|||
|
|||
Worked absolutely perfectly. Thank you!
|
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|