vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   remove extra commas from loop (https://vborg.vbsupport.ru/showthread.php?t=125262)

harmor19 08-29-2006 07:04 PM

remove extra commas from loop
 
I have a loop where I add a comma after every result. How can I only have it so a comma is placed after a string?

I want
Code:

boo,foo,testing,123
I don't want
Code:

boo,,testing,123,

Code Monkey 08-29-2006 07:21 PM

I can only guess since you posted no code.
There are many simple ways to avoid this depending on the situation. Why don't you explain more what you are doing and post some code.

harmor19 08-29-2006 10:12 PM

Well I'm making multiple drop down menus generated by a query which will be placed on the registration page.
The user can select an option from any or all of the drop down menus.
To avoid having to create potentially 100 or more rows in the database I thought I would insert them all into one field then use "explode".

The code below inserts the data.
PHP Code:

foreach ($vbulletin->GPC['favoriteteam'] as $team)
{
     
$teams .= "$team,";
}

$userdata->set('favoriteteam'$teams); 


This code retrieves the data
PHP Code:

$teams explode(","$post['favoriteteam']);
  
foreach(
$teams as $key => $val
{  
    
$sportlogos .= "<img src='images/sports/$val.gif' width='30' height='30' /> ";




Code Monkey 08-29-2006 10:48 PM

This will take your original array and create a comma seperated string of the array values.
PHP Code:


$teams 
implode(','$vbulletin->GPC['favoriteteam']); 


harmor19 08-29-2006 11:24 PM

I thought up this code.

PHP Code:

foreach ($vbulletin->GPC['favoriteteam'] as $team)
{
     
$teams .= "$team,";
}
$teams $teams.",0";
$teams str_replace(",,"","$teams);
$teams str_replace(",0"""$teams); 


Code Monkey 08-30-2006 12:39 AM

Why would you do all that?

The code I gave you will turn any array into a string of values seperated by commas.

harmor19 08-30-2006 12:48 AM

It lets you know that I'm trying.

For your code I can just use?
PHP Code:

$teams implode(','$vbulletin->GPC['favoriteteam']);
$userdata->set('favoriteteam'$teams); 


Antivirus 08-31-2006 02:01 AM

I think he wants your code to do what it dies, but NOT append the comma after the last result

Code Monkey 08-31-2006 03:27 AM

Implode does not put a comma at the end.


All times are GMT. The time now is 03:08 AM.

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.01574 seconds
  • Memory Usage 1,734KB
  • 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_code_printable
  • (5)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)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