-1

I am building a template for another program I use, and have spent many many hours trying to figure this out.

I have HTML checkboxes that are pulling in data using

selection[]

As the name.

Basically, the input data is formed into an array, which includes a number two times. This number needs to be dynamic but listed in order... 1, 2, 3 etc.

An example input list would be something like

This is $number example and $number is cool.
This $number is great but $number is better.

The proper output would be:

This is 1 example and 1 is cool.
This 2 is great but 2 is better.

Every solution I've found requires the numbers to be static, like for example exploding a list of numbers and using...

$numbers = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";
$numex = explode(" ", $numbers);
echo $number[0];

But since each selection is different, and every time different checkboxes will be selected, I need PHP to somehow insert the numbers dynamically.

Is this possible? I'm driving myself crazy trying to figure it out.

Update:

Here are samples of inputs:

$game1 = "C:\\Program Files (x86)\\games\\1\\template\\1";
$game2 = "C:\\Program Files (x86)\\games\\2\\template\\2";
$game3 = "C:\\Program Files (x86)\\games\\3\\template\\3";
$game4 = "C:\\Program Files (x86)\\games\\4\\template\\4";

Now, where the "1, 2, 3, 4" is, I need those numbers to be automatically inserted after the form is submitted. So for example, if only

$game3
$game4

Are selected, the output would be:

$game3 = "C:\\Program Files (x86)\\games\\1\\template\\1";
$game4 = "C:\\Program Files (x86)\\games\\2\\template\\2";

Does this make more sense?

Thank you for your time.

Joe
  • 143
  • 10
  • I can't figure out what you're doing. Show a sample of the HTML form and how the output should be related to what the user selects. – Barmar Aug 23 '17 at 23:37
  • do a var_export($_GET) or var_export($_POST) depending on what you submitting with. This will show you what you are working with. you should be able to just use a loop. – mickmackusa Aug 23 '17 at 23:38
  • `foreach ($_POST['selection'] as $checkbox_value)` – Barmar Aug 23 '17 at 23:39
  • This is likely to be a duplicate question. Have you researched on SO? – mickmackusa Aug 23 '17 at 23:39
  • It's a localhost script I'm making to form templates for another program. Inside the templates, there is a comma separated list of words that are used depending on the number they are assigned. So the 1st word in the comma separated list, corresponds to the "1" above. This is why the numbers have to be dynamic, as the comma separated list will change. – Joe Aug 23 '17 at 23:39
  • I have literally looked at 100+ answers on SO, and tested like 50 different solutions but none of them worked. – Joe Aug 23 '17 at 23:40
  • https://stackoverflow.com/questions/33434201/how-to-array-key-start-from-1-instead-of-0 and https://stackoverflow.com/a/45797344/2943403 Show us your coding attempt, some sample data, and your expected result. This task is a combination of simple techniques -- all of which will be duplicate questions on SO. – mickmackusa Aug 23 '17 at 23:41
  • Neither of the items you linked are able to dynamically insert numbers, the numbers always remain static. In my example, the numbers need to be dynamically listed as 1,2,3 without me having to indicate their positions. The code should always know that 1 is first, followed by 2, then 3. – Joe Aug 23 '17 at 23:43
  • Both of those links show you how to add an extra element to the front of your array, then remove it and preserve the adjusted keys. This will make the keys align the way you like. – mickmackusa Aug 23 '17 at 23:45
  • @mickmackusa What part of this question is about adding an extra element to an array and than removing it? – Barmar Aug 23 '17 at 23:47
  • You can hard code the keys in your form as 1, 2, 3, etc. – mickmackusa Aug 23 '17 at 23:47
  • This question is unclear and does not provide an actual coding attempt or html form. – mickmackusa Aug 23 '17 at 23:48
  • @mickmackusa Did you even read my question? The point is that I do not want the numbers hard coded. I need the opposite. Dynamic insertion of the numbers. If 1 isn't checked, and 2 is, then 2 becomes 1 in the output. – Joe Aug 23 '17 at 23:48
  • Right, then the suggestions in my links are the correct technique. Pad the array by shifting a new element to the front of your $_GET/POST['selection] array and then `unset()` it, this start your keys at `1` – mickmackusa Aug 23 '17 at 23:50

1 Answers1

2

Don't use variables like that. Any time you find yourself creating numbered variables like $game1, $game2, etc. you should be using an array: $game1 is $games[0], $game2 is $games[1], and so on.

Give the inputs values that select the array index. Then you can loop over the inputs.

Option 1 <input type="checkbox" name="selection[]" value="0,CSGO">
Option 2 <input type="checkbox" name="selection[]" value="1,DOTA">
Option 3 <input type="checkbox" name="selection[]" value="2,BLAH">
Option 4 <input type="checkbox" name="selection[]" value="3,XXXX">
...

Then you can process them like this:

$games = array();
$names = array();
foreach ($_POST['selection'] as $index => $value) {
    list($number, $name) = explode(',', $value);
    $gamenum = $index + 1;
    $games[$number] = "C:\\Program Files (x86)\\games\\$gamenum\\template\\$gamenum";
    $names[] = $name;
}

So if the user checks options 3 and 4, the arrays will be:

$games = array(
    2 => "C:\\Program Files (x86)\\games\\1\\template\\1",
    3 => "C:\\Program Files (x86)\\games\\2\\template\\2"
)

$names = array("BLAH", "XXXX");

You can then turn $names into a comma-separated list with:

$all_names = implode(',', $names);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    I noticed all the comments were deleted from here, I wonder why "all"? In any case Barmar, I revisited the question and remembered the OP stated they couldn't accept your answer because the question was closed. I voted to reopen. Edit: The reason I originally voted to close as unclear, was because it just was. – Funk Forty Niner Aug 24 '17 at 19:29
  • @Fred-ii- A moderator must have done it, because they don't want extended discussions in comments. – Barmar Aug 24 '17 at 19:30
  • That I can understand, however there were relevant comments that IMHO should have been left intact. Edit: The question is now reopened. – Funk Forty Niner Aug 24 '17 at 19:31
  • @Fred-ii- I guess they didn't feel like reading them all to figure out which were necessary. – Barmar Aug 24 '17 at 19:35
  • @Barmar FYI I accepted the answer, the option seems to be available now. Thanks again for this script, saved me countless hours of trying to figure it out myself. Also taught me a lot about associative arrays. – Joe Aug 29 '17 at 08:29