/    Sign up×
Community /Pin to ProfileBookmark

Help w/ Revising PHP Combine-Into-String Script?

I have a script I’ve been using to take values from checkboxes on a form and create a string to be saved as a field in a MySQL database. The only problem is it stops using values if there is a break in the field order. For instance, if you post something like:

[CODE]priceOption8=275&priceOption9=24&priceOption10=798[/CODE]

It works fine and creates the string “275 24 798”. BUT, if you post something like:

[CODE]priceOption8=298&priceOption9=58&priceOption12=334[/CODE]

It only creates the string “298 58”.

Does anyone know how to revise this script to work when every option isn’t passing a value? Here is the current script:

[code=php]// Create one options string from multiple fields
$count = 1;
$priceOptions = ”;
$v_name = “priceOption” . $count;

while (${$v_name} <> ”) {
$priceOptions = $priceOptions . ” ” . ${$v_name};
$count ++;
$v_name = “priceOption” . $count;
}

$priceOptions = trim($priceOptions);[/code]

Any help would be greatly appreciated. Thank you!

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@BoarsHellMar 25.2007 — do this instead...

[code=php]
foreach ($_GET as $key => $value) {
echo "Key: $key; Value: $value<br />n";
}
[/code]


For more information about foreach... http://us2.php.net/foreach
Copy linkTweet thisAlerts:
@NightShift58Mar 25.2007 — [code=php]<?php
// Create one options string from multiple fields
$priceOptions = '';
foreach ($_GET as $key => $thisOption) {
if (substr($key,0,11) == "priceOption") {
$priceOptions .= " " . $thisOption;
}
}
?>[/code]
Copy linkTweet thisAlerts:
@msmith29063authorMar 25.2007 — My apologies. I didn't think to include some things. First of all, I'm using POST -- not a big deal. BUT, I am passing a lot of other values from the form as well. So these revisions won't work. Any other ideas? Thank you for you help!
Copy linkTweet thisAlerts:
@BoarsHellMar 25.2007 — Can you explain why NightShift58's revision would not work?
Copy linkTweet thisAlerts:
@msmith29063authorMar 25.2007 — Correct me if I'm wrong -- but wouldn't his solution take ALL values passed from the form and use them in the new string? I just need the values that are named priceOption# (i.e. priceOption1, priceOption3, etc.) I'm passing other values from the form -- like priceList, pricePrice, etc. -- that I do not need to be added to the newly created string. Does this make sense?
Copy linkTweet thisAlerts:
@BoarsHellMar 25.2007 — Try the code, it will work. The if statement inside the foreach loop checks to see if the passed value's name starts with the substring [COLOR="DarkRed"]"priceOption"[/COLOR] and if it does, the value is added to the string [COLOR="Blue"]$priceOptions[/COLOR]
Copy linkTweet thisAlerts:
@msmith29063authorMar 25.2007 — Yes. You were correct! It does work. I apologize. At quick glance, I thought it was using all values. Thank you for your help, guys. I didn't think of doing it this way. Great job!
Copy linkTweet thisAlerts:
@NightShift58Mar 26.2007 — You had me worried for a while...
Copy linkTweet thisAlerts:
@msmith29063authorMar 26.2007 — Sorry, Nightshift. Let me know if I owe you $ for your time.
×

Success!

Help @msmith29063 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...