vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   PHP exploding data fields in template (https://vborg.vbsupport.ru/showthread.php?t=315505)

Morrus 11-19-2014 05:05 PM

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!

vBNinja 11-19-2014 05:13 PM

What template are you trying to use?

Dave 11-19-2014 05:19 PM

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.

Morrus 11-19-2014 06:15 PM

Thanks! Where you have the names there, can I replace that with $product['authors']? That was just an example.

Dave 11-19-2014 06:39 PM

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.

kh99 11-19-2014 06:42 PM

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.

Morrus 11-19-2014 08:03 PM

Worked absolutely perfectly. Thank you!


All times are GMT. The time now is 10:39 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01094 seconds
  • Memory Usage 1,726KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete