Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
  #1  
Old 11-19-2014, 05:05 PM
Morrus Morrus is offline
 
Join Date: Jan 2002
Posts: 494
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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!
Reply With Quote
  #2  
Old 11-19-2014, 05:13 PM
vBNinja's Avatar
vBNinja vBNinja is offline
 
Join Date: May 2011
Location: USA
Posts: 239
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What template are you trying to use?
Reply With Quote
  #3  
Old 11-19-2014, 05:19 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
<?php

$authors 
explode(', ''Bob Shakespeare, Arnold Willis, Bill Dickins');
$output '';
foreach(
$authors as $value){
    
$output .= '<a href="products.php?authors=' $value '">' $value '</a>, ';
}

// Use this in the part where the script is assigning the template variables.
$templater->register('authors_list'$output);
You'll be able to use {vb:raw authors_list} in the template.
Reply With Quote
2 благодарности(ей) от:
kh99, Lynne
  #4  
Old 11-19-2014, 06:15 PM
Morrus Morrus is offline
 
Join Date: Jan 2002
Posts: 494
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks! Where you have the names there, can I replace that with $product['authors']? That was just an example.
Reply With Quote
  #5  
Old 11-19-2014, 06:39 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yes, so it'll be:

PHP Code:
$authors explode(', '$product['authors']); 
This will work when there's a comma and a space between every author.
Reply With Quote
  #6  
Old 11-19-2014, 06:42 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 11-19-2014, 08:03 PM
Morrus Morrus is offline
 
Join Date: Jan 2002
Posts: 494
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Worked absolutely perfectly. Thank you!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:54 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04205 seconds
  • Memory Usage 2,220KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (2)post_thanks_box_bit
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete